Register update endpoint, use new form type, add error handling
This commit is contained in:
22
src/db.rs
22
src/db.rs
@ -55,12 +55,17 @@ pub async fn teams(pool: &Pool) -> Result<HashMap<String, Team>, Error> {
|
||||
.map_err(error::ErrorInternalServerError)
|
||||
}
|
||||
|
||||
pub async fn update_game(pool: &Pool, game: Game) -> Result<(), Error> {
|
||||
pub async fn update_game(
|
||||
pool: &Pool,
|
||||
code: String,
|
||||
start_time: String,
|
||||
end_time: String,
|
||||
) -> Result<(), Error> {
|
||||
let pool = pool.clone();
|
||||
let conn = web::block(move || pool.get())
|
||||
.await?
|
||||
.map_err(error::ErrorInternalServerError)?;
|
||||
web::block(move || update_game_sqlite(conn, game))
|
||||
web::block(move || update_game_sqlite(conn, code, start_time, end_time))
|
||||
.await?
|
||||
.map_err(error::ErrorInternalServerError)
|
||||
}
|
||||
@ -83,14 +88,19 @@ fn get_all_games(conn: Connection, all_teams: HashMap<String, Team>) -> GamesRes
|
||||
.and_then(Iterator::collect)
|
||||
}
|
||||
|
||||
fn update_game_sqlite(conn: Connection, game: Game) -> Result<(), rusqlite::Error> {
|
||||
fn update_game_sqlite(
|
||||
conn: Connection,
|
||||
code: String,
|
||||
start_time: String,
|
||||
end_time: String,
|
||||
) -> Result<(), rusqlite::Error> {
|
||||
let update_query = Query::update()
|
||||
.table("games")
|
||||
.values([
|
||||
("start_time", game.start_time.into()),
|
||||
("end_time", game.end_time.into()),
|
||||
("start_time", start_time.into()),
|
||||
("end_time", end_time.into()),
|
||||
])
|
||||
.and_where(Expr::col("code").eq(game.code))
|
||||
.and_where(Expr::col("code").eq(code))
|
||||
.to_owned();
|
||||
let mut update_stmt = conn.prepare(&update_query.to_string(SqliteQueryBuilder))?;
|
||||
|
||||
|
||||
27
src/main.rs
27
src/main.rs
@ -67,6 +67,13 @@ struct QueryForm {
|
||||
query: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct UpdateGameForm {
|
||||
code: String,
|
||||
start_time: String,
|
||||
end_time: String,
|
||||
}
|
||||
|
||||
impl Actor for GamesActor {
|
||||
type Context = actix::Context<Self>;
|
||||
}
|
||||
@ -193,11 +200,20 @@ async fn list_of_games(games: web::Data<Arc<Mutex<Vec<Game>>>>) -> impl Responde
|
||||
.body(games_json)
|
||||
}
|
||||
|
||||
#[post("/admin/game/{game_code}")]
|
||||
async fn update_game(pool: web::Data<Pool>, form: web::Form<Game>) -> impl Responder {
|
||||
db::update_game(&pool, form.into_inner()).await.unwrap();
|
||||
|
||||
HttpResponse::Ok()
|
||||
#[post("/admin/game/update")]
|
||||
async fn update_game(pool: web::Data<Pool>, form: web::Form<UpdateGameForm>) -> impl Responder {
|
||||
println!("{form:?}");
|
||||
match db::update_game(
|
||||
&pool,
|
||||
form.code.clone(),
|
||||
form.start_time.clone(),
|
||||
form.end_time.clone(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => HttpResponse::Ok().body("{}"),
|
||||
Err(err) => HttpResponse::InternalServerError().body(format!("{err:?}")),
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/admin/query")]
|
||||
@ -239,6 +255,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(admin_query)
|
||||
.service(list_of_games)
|
||||
.service(get_territories)
|
||||
.service(update_game)
|
||||
})
|
||||
.bind(("0.0.0.0", 8080))?
|
||||
.run()
|
||||
|
||||
Reference in New Issue
Block a user