Adds renaming games functionality

This commit is contained in:
2025-07-12 17:07:33 +02:00
parent 28f7306d57
commit 3f99b68d62
30 changed files with 502 additions and 178 deletions

View File

@@ -49,7 +49,9 @@ async fn main() -> std::io::Result<()> {
.service(post_leave_gamenight)
.service(get_get_participants)
.service(get_games)
.service(get_game)
.service(post_game)
.service(post_rename_game)
})
.bind(("::1", 8080))?
.run()

View File

@@ -1,8 +1,8 @@
use actix_web::{get, http::header::ContentType, post, web, HttpResponse, Responder};
use gamenight_database::{game::insert_game, DbPool, GetConnection};
use gamenight_database::{game::{insert_game, load_game, rename_game}, DbPool, GetConnection};
use uuid::Uuid;
use crate::{models::{add_game_request_body::AddGameRequestBody, game::Game}, request::{authorization::AuthUser, error::ApiError}};
use crate::{models::{add_game_request_body::AddGameRequestBody, game::Game, game_id::GameId, rename_game_request_body::RenameGameRequestBody}, request::{authorization::AuthUser, error::ApiError}};
#[get("/games")]
pub async fn get_games(pool: web::Data<DbPool>, _user: AuthUser) -> Result<impl Responder, ApiError> {
@@ -30,10 +30,34 @@ impl From<AddGameRequestBody> for gamenight_database::game::Game {
}
}
#[get("/game")]
pub async fn get_game(pool: web::Data<DbPool>, _user: AuthUser, game_id: web::Json<GameId>) -> Result<impl Responder, ApiError> {
let mut conn = pool.get_conn();
let db_game = load_game(&mut conn, Uuid::parse_str(&game_id.0.game_id)?)?;
let model = Game {
id: db_game.id.to_string(),
name: db_game.name
};
Ok(HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&model)?)
)
}
#[post("/game")]
pub async fn post_game(pool: web::Data<DbPool>, _user: AuthUser, game_data: web::Json<AddGameRequestBody>) -> Result<impl Responder, ApiError> {
let mut conn = pool.get_conn();
insert_game(&mut conn, game_data.0.into())?;
Ok(HttpResponse::Ok())
}
#[post("/rename_game")]
pub async fn post_rename_game(pool: web::Data<DbPool>, _user: AuthUser, game_data: web::Json<RenameGameRequestBody>) -> Result <impl Responder, ApiError> {
let mut conn = pool.get_conn();
rename_game(&mut conn, Uuid::parse_str(&game_data.0.id)?, game_data.0.name)?;
Ok(HttpResponse::Ok())
}

View File

@@ -19,4 +19,6 @@ pub use join_gamenight::post_join_gamenight;
pub use join_gamenight::post_leave_gamenight;
pub use participant_handlers::get_get_participants;
pub use game::get_games;
pub use game::get_game;
pub use game::post_game;
pub use game::post_rename_game;