56 lines
1.4 KiB
Rust
56 lines
1.4 KiB
Rust
use std::sync::Arc;
|
|
|
|
use geoffrey_db::helper::load_location;
|
|
use geoffrey_db::query::QueryBuilder;
|
|
use geoffrey_models::models::locations::{Location, LocationDb};
|
|
use geoffrey_models::models::parameters::delete_params::DeleteParams;
|
|
use geoffrey_models::models::player::Player;
|
|
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
|
|
use geoffrey_models::models::CommandLevel;
|
|
|
|
use crate::api_endpoint::{ApiEndpoint, RequestType};
|
|
use crate::commands::Command;
|
|
use crate::context::Context;
|
|
use crate::Result;
|
|
|
|
pub struct Delete {}
|
|
|
|
impl ApiEndpoint for Delete {
|
|
fn endpoint_name() -> String {
|
|
"delete".to_string()
|
|
}
|
|
|
|
fn request_type() -> RequestType {
|
|
RequestType::POST
|
|
}
|
|
}
|
|
|
|
impl Command for Delete {
|
|
type Req = DeleteParams;
|
|
type Resp = Location;
|
|
|
|
fn command_level() -> CommandLevel {
|
|
CommandLevel::REGISTERED
|
|
}
|
|
|
|
fn run_command(ctx: Arc<Context>, req: &Self::Req, user: Option<Player>) -> Result<Self::Resp> {
|
|
let user = user.unwrap();
|
|
|
|
let query = QueryBuilder::<LocationDb>::default()
|
|
.with_owner(user.id.unwrap())
|
|
.with_name(&req.location);
|
|
|
|
let location = ctx
|
|
.db
|
|
.run_query(query)?
|
|
.pop()
|
|
.ok_or(GeoffreyAPIError::EntryNotFound)?;
|
|
|
|
let location = load_location(&ctx.db, &location).map_err(GeoffreyAPIError::from)?;
|
|
|
|
ctx.db.remove::<LocationDb>(location.id)?;
|
|
|
|
Ok(location)
|
|
}
|
|
}
|