forked from Roflin/gamenight
Converted the Api to a Restful api.
This commit is contained in:
62
backend-actix/src/request/locations.rs
Normal file
62
backend-actix/src/request/locations.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
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<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("/locations")]
|
||||
pub async fn post_locations(
|
||||
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