From 1df44a3e35559607f60d4ff3c92be2a549ed207d Mon Sep 17 00:00:00 2001 From: Zigzagill Date: Sat, 23 May 2026 21:27:02 -0700 Subject: [PATCH] Register update endpoint, use new form type, add error handling --- src/db.rs | 22 ++++++++++++++++------ src/main.rs | 27 ++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/db.rs b/src/db.rs index f383324..0167a12 100644 --- a/src/db.rs +++ b/src/db.rs @@ -55,12 +55,17 @@ pub async fn teams(pool: &Pool) -> Result, 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) } @@ -88,14 +93,19 @@ fn get_all_games(conn: Connection, all_teams: HashMap) -> 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))?; diff --git a/src/main.rs b/src/main.rs index 7e40122..8824820 100644 --- a/src/main.rs +++ b/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; } @@ -177,11 +184,20 @@ async fn list_of_games(games: web::Data>>>) -> impl Responde .body(games_json) } -#[post("/admin/game/{game_code}")] -async fn update_game(pool: web::Data, form: web::Form) -> impl Responder { - db::update_game(&pool, form.into_inner()).await.unwrap(); - - HttpResponse::Ok() +#[post("/admin/game/update")] +async fn update_game(pool: web::Data, form: web::Form) -> 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")] @@ -222,6 +238,7 @@ async fn main() -> std::io::Result<()> { .service(admin) .service(admin_query) .service(list_of_games) + .service(update_game) }) .bind(("0.0.0.0", 8080))? .run()