Added Location and location ownership/rights to gamenight.
This commit is contained in:
@@ -1,53 +1,73 @@
|
||||
use actix_web::{get, http::header::ContentType, post, web, HttpResponse, Responder};
|
||||
use gamenight_database::{game::{insert_game, load_game, rename_game}, owned_game::{disown_game, own_game, owned_games, OwnedGame}, 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}};
|
||||
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> {
|
||||
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 {
|
||||
let model: Vec<Game> = games
|
||||
.iter()
|
||||
.map(|x| Game {
|
||||
id: x.id.to_string(),
|
||||
name: x.name.clone(),
|
||||
}}
|
||||
).collect();
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&model)?)
|
||||
)
|
||||
.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
|
||||
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> {
|
||||
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 {
|
||||
let model = Game {
|
||||
id: db_game.id.to_string(),
|
||||
name: db_game.name
|
||||
name: db_game.name,
|
||||
};
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&model)?)
|
||||
)
|
||||
.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> {
|
||||
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())?;
|
||||
|
||||
@@ -55,40 +75,68 @@ pub async fn post_game(pool: web::Data<DbPool>, _user: AuthUser, game_data: web:
|
||||
}
|
||||
|
||||
#[post("/rename_game")]
|
||||
pub async fn post_rename_game(pool: web::Data<DbPool>, _user: AuthUser, game_data: web::Json<RenameGameRequestBody>) -> Result <impl Responder, ApiError> {
|
||||
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)?;
|
||||
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> {
|
||||
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)? })?;
|
||||
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> {
|
||||
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)? })?;
|
||||
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> {
|
||||
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();
|
||||
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)?)
|
||||
)
|
||||
.body(serde_json::to_string(&model)?))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user