diff --git a/src/db.rs b/src/db.rs index 0750fbf..865edf3 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) } @@ -83,14 +88,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 9646e1d..79cf671 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; } @@ -193,11 +200,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")] @@ -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()