Converted the Api to a Restful api.
This commit is contained in:
@@ -1,65 +1,28 @@
|
||||
use actix_web::{get, http::header::ContentType, post, web, HttpResponse, Responder};
|
||||
use gamenight_database::{
|
||||
location::{insert_location, locations},
|
||||
DbPool, GetConnection,
|
||||
};
|
||||
use crate::models::location::Location;
|
||||
use crate::request::authorization::AuthUser;
|
||||
use crate::request::error::ApiError;
|
||||
use actix_web::http::header::ContentType;
|
||||
use actix_web::{get, web, HttpResponse, Responder};
|
||||
use gamenight_database::{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")]
|
||||
#[get("/location/{locationId}")]
|
||||
pub async fn get_locations(
|
||||
pool: web::Data<DbPool>,
|
||||
_user: AuthUser,
|
||||
location_id: web::Path<Uuid>,
|
||||
) -> 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();
|
||||
let location = gamenight_database::location::load_location(&mut conn, location_id.into_inner())?;
|
||||
|
||||
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(),
|
||||
let model = Location {
|
||||
id: location.id.to_string(),
|
||||
name: location.name.clone(),
|
||||
note: location.note.clone(),
|
||||
address: location.address.clone(),
|
||||
};
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&model)?))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user