use actix_web::{get, post, web, HttpResponse, Responder}; use actix_web::http::header::ContentType; use gamenight_database::{DbPool, GetConnection}; use gamenight_database::location::{insert_location, locations}; use uuid::Uuid; use crate::models::add_location_request_body::AddLocationRequestBody; use crate::models::location::Location; use crate::models::location_id::LocationId; use crate::request::authorization::AuthUser; use crate::request::error::ApiError; impl From 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, _user: AuthUser, ) -> Result { let mut conn = pool.get_conn(); let games: Vec = locations(&mut conn)?; let model: Vec = 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("/locations")] pub async fn post_locations( pool: web::Data, _user: AuthUser, game_data: web::Json, ) -> Result { 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)?)) }