Register update endpoint, use new form type, add error handling

This commit is contained in:
2026-05-23 21:27:02 -07:00
parent 44ce5b3a39
commit 1df44a3e35
2 changed files with 38 additions and 11 deletions

View File

@ -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)
}
@ -88,14 +93,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))?;

View File

@ -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>;
}
@ -177,11 +184,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")]
@ -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()