Rework some code issues
This commit is contained in:
@ -1,19 +1,75 @@
|
||||
use axum::routing::{get, post};
|
||||
|
||||
pub mod funcs;
|
||||
pub mod game;
|
||||
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(funcs::games_list).post(funcs::insert_game),
|
||||
)
|
||||
.route(
|
||||
"/api/games/{id}",
|
||||
post(funcs::insert_game).delete(funcs::delete_game),
|
||||
)
|
||||
.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<AppState>) -> impl IntoResponse {
|
||||
let games: Vec<Game> = 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<AppState>,
|
||||
Json(game): Json<CreateGame>,
|
||||
) -> 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<AppState>,
|
||||
Path(params): Path<GamePathParams>,
|
||||
Json(game): Json<CreateGame>,
|
||||
) -> 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<AppState>, Path(id): Path<i32>) -> impl IntoResponse {
|
||||
sqlx::query_file!("queries/delete.sql", id)
|
||||
.execute(&state.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
axum::http::StatusCode::OK
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user