Adds own/disown game logic

This commit is contained in:
2025-07-12 20:00:22 +02:00
parent 3f99b68d62
commit df1e3ff905
13 changed files with 427 additions and 4 deletions

View File

@@ -52,6 +52,9 @@ async fn main() -> std::io::Result<()> {
.service(get_game)
.service(post_game)
.service(post_rename_game)
.service(post_own_game)
.service(post_disown_game)
.service(get_owned_games)
})
.bind(("::1", 8080))?
.run()

View File

@@ -1,5 +1,5 @@
use actix_web::{get, http::header::ContentType, post, web, HttpResponse, Responder};
use gamenight_database::{game::{insert_game, load_game, rename_game}, DbPool, GetConnection};
use gamenight_database::{game::{insert_game, load_game, rename_game}, owned_game::{disown_game, own_game, owned_games, OwnedGame}, DbPool, GetConnection};
use uuid::Uuid;
use crate::{models::{add_game_request_body::AddGameRequestBody, game::Game, game_id::GameId, rename_game_request_body::RenameGameRequestBody}, request::{authorization::AuthUser, error::ApiError}};
@@ -60,4 +60,35 @@ pub async fn post_rename_game(pool: web::Data<DbPool>, _user: AuthUser, game_dat
rename_game(&mut conn, Uuid::parse_str(&game_data.0.id)?, game_data.0.name)?;
Ok(HttpResponse::Ok())
}
}
#[post("/own")]
pub async fn post_own_game(pool: web::Data<DbPool>, user: AuthUser, game_id: web::Json<GameId>) -> Result <impl Responder, ApiError> {
let mut conn = pool.get_conn();
own_game(&mut conn, OwnedGame { user_id: user.0.id, game_id: Uuid::parse_str(&game_id.0.game_id)? })?;
Ok(HttpResponse::Ok())
}
#[post("/disown")]
pub async fn post_disown_game(pool: web::Data<DbPool>, user: AuthUser, game_id: web::Json<GameId>) -> Result <impl Responder, ApiError> {
let mut conn = pool.get_conn();
disown_game(&mut conn, OwnedGame { user_id: user.0.id, game_id: Uuid::parse_str(&game_id.0.game_id)? })?;
Ok(HttpResponse::Ok())
}
#[get("/owned_games")]
pub async fn get_owned_games(pool: web::Data<DbPool>, user: AuthUser) -> Result <impl Responder, ApiError> {
let mut conn = pool.get_conn();
let game_ids = owned_games(&mut conn, user.0.id)?;
let model : Vec<String> = game_ids.iter().map(|x| {
x.to_string()
}).collect();
Ok(HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&model)?)
)
}

View File

@@ -22,3 +22,6 @@ pub use game::get_games;
pub use game::get_game;
pub use game::post_game;
pub use game::post_rename_game;
pub use game::post_own_game;
pub use game::post_disown_game;
pub use game::get_owned_games;