171 lines
4.5 KiB
Rust
171 lines
4.5 KiB
Rust
use crate::game::rename_game;
|
|
use crate::owned_game::own_game;
|
|
use crate::owned_game::owned_games;
|
|
use crate::owned_game::disown_game;
|
|
use crate::owned_game::OwnedGame;
|
|
use gamenight_database::game::load_game;
|
|
use crate::game::insert_game;
|
|
use uuid::Uuid;
|
|
use crate::game::remove_game;
|
|
use actix_web::{delete, get, http::header::ContentType, post, web, HttpResponse, Responder};
|
|
use gamenight_database::{
|
|
user::Role,
|
|
DbPool, GetConnection,
|
|
};
|
|
|
|
use crate::{
|
|
models::{
|
|
add_game_request_body::AddGameRequestBody, game::Game, game_id::GameId,
|
|
rename_game_request_body::RenameGameRequestBody, own_game_request_body::OwnGameRequestBody
|
|
},
|
|
request::{authorization::AuthUser, error::ApiError},
|
|
};
|
|
|
|
#[get("/games")]
|
|
pub async fn get_games(
|
|
pool: web::Data<DbPool>,
|
|
_user: AuthUser,
|
|
) -> Result<impl Responder, ApiError> {
|
|
let mut conn = pool.get_conn();
|
|
let games: Vec<gamenight_database::game::Game> = gamenight_database::games(&mut conn)?;
|
|
let model: Vec<Game> = games
|
|
.iter()
|
|
.map(|x| Game {
|
|
id: x.id.to_string(),
|
|
name: x.name.clone(),
|
|
})
|
|
.collect();
|
|
|
|
Ok(HttpResponse::Ok()
|
|
.content_type(ContentType::json())
|
|
.body(serde_json::to_string(&model)?))
|
|
}
|
|
|
|
impl From<AddGameRequestBody> for gamenight_database::game::Game {
|
|
fn from(value: AddGameRequestBody) -> Self {
|
|
Self {
|
|
id: Uuid::new_v4(),
|
|
name: value.name,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[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();
|
|
let game = game_data.0.into();
|
|
insert_game(&mut conn, &game)?;
|
|
|
|
Ok(HttpResponse::Ok()
|
|
.content_type(ContentType::json())
|
|
.body(serde_json::to_string(&GameId{game_id: game.id.to_string()})?))
|
|
}
|
|
|
|
#[delete("/game")]
|
|
pub async fn delete_game(
|
|
pool: web::Data<DbPool>,
|
|
user: AuthUser,
|
|
game_id: web::Json<GameId>,
|
|
) -> Result<impl Responder, ApiError> {
|
|
if user.0.role != Role::Admin {
|
|
Ok(HttpResponse::Unauthorized())
|
|
} else {
|
|
let mut conn = pool.get_conn();
|
|
remove_game(&mut conn, Uuid::parse_str(&game_id.0.game_id)?)?;
|
|
|
|
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())
|
|
}
|
|
|
|
#[post("/own")]
|
|
pub async fn post_own_game(
|
|
pool: web::Data<DbPool>,
|
|
user: AuthUser,
|
|
own_data: web::Json<OwnGameRequestBody>,
|
|
) -> 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(&own_data.game_id)?,
|
|
location_id: own_data.location_id.clone().map(|x| Uuid::parse_str(&x).unwrap()),
|
|
},
|
|
)?;
|
|
|
|
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)?,
|
|
location_id: None
|
|
},
|
|
)?;
|
|
|
|
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)?))
|
|
}
|