Added Location and location ownership/rights to gamenight.
This commit is contained in:
65
backend-actix/src/request/location.rs
Normal file
65
backend-actix/src/request/location.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use actix_web::{get, http::header::ContentType, post, web, HttpResponse, Responder};
|
||||
use gamenight_database::{
|
||||
location::{insert_location, locations},
|
||||
DbPool, GetConnection,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
models::{
|
||||
add_location_request_body::AddLocationRequestBody, location::Location,
|
||||
location_id::LocationId,
|
||||
},
|
||||
request::{authorization::AuthUser, error::ApiError},
|
||||
};
|
||||
|
||||
impl From<AddLocationRequestBody> for gamenight_database::location::Location {
|
||||
fn from(value: AddLocationRequestBody) -> Self {
|
||||
Self {
|
||||
id: Uuid::new_v4(),
|
||||
name: value.name,
|
||||
address: value.address,
|
||||
note: value.note,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/locations")]
|
||||
pub async fn get_locations(
|
||||
pool: web::Data<DbPool>,
|
||||
_user: AuthUser,
|
||||
) -> Result<impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
let games: Vec<gamenight_database::location::Location> = locations(&mut conn)?;
|
||||
let model: Vec<Location> = games
|
||||
.iter()
|
||||
.map(|x| Location {
|
||||
id: x.id.to_string(),
|
||||
name: x.name.clone(),
|
||||
address: x.address.clone(),
|
||||
note: x.note.clone(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&model)?))
|
||||
}
|
||||
|
||||
#[post("/location")]
|
||||
pub async fn post_location(
|
||||
pool: web::Data<DbPool>,
|
||||
_user: AuthUser,
|
||||
game_data: web::Json<AddLocationRequestBody>,
|
||||
) -> Result<impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
let uuid = insert_location(&mut conn, game_data.0.into())?;
|
||||
|
||||
let model = LocationId {
|
||||
location_id: uuid.to_string(),
|
||||
};
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&model)?))
|
||||
}
|
||||
Reference in New Issue
Block a user