use axum::routing::{get, post}; use axum::{ Json, extract::{Path, State}, response::IntoResponse, }; use crate::app_state::AppState; use crate::models::{CreateGame, Game, GamePathParams}; pub fn router(state: AppState) -> axum::Router { axum::Router::new() .route("/api/games", get(games_list).post(insert_game)) .route("/api/games/{id}", post(update_game).delete(delete_game)) .with_state(state) } pub async fn games_list(State(state): State) -> impl IntoResponse { let games: Vec = sqlx::query_file_as!(Game, "queries/get_all.sql") .fetch_all(&state.pool) .await .unwrap(); Json(games) } pub async fn insert_game( State(state): State, Json(game): Json, ) -> impl IntoResponse { sqlx::query_file!( "queries/insert.sql", game.name, game.publishing_house, game.developer, game.description, game.multiplayer, game.is_free_to_play, ) .execute(&state.pool) .await .unwrap(); axum::http::StatusCode::CREATED } pub async fn update_game( State(state): State, Path(params): Path, Json(game): Json, ) -> impl IntoResponse { sqlx::query_file!( "queries/update.sql", game.name, game.publishing_house, game.developer, game.description, game.multiplayer, game.is_free_to_play, params.id, ) .execute(&state.pool) .await .unwrap(); } pub async fn delete_game(State(state): State, Path(id): Path) -> impl IntoResponse { sqlx::query_file!("queries/delete.sql", id) .execute(&state.pool) .await .unwrap(); axum::http::StatusCode::OK }