2021-12-18 03:03:15 +00:00
|
|
|
use crate::commands::{Command, RequestType};
|
|
|
|
use crate::context::Context;
|
|
|
|
use crate::Result;
|
2021-12-19 21:06:56 +00:00
|
|
|
use geoffrey_db::helper::{find_location_by_name, load_location};
|
2021-12-18 03:03:15 +00:00
|
|
|
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 std::sync::Arc;
|
|
|
|
|
|
|
|
pub struct Delete {}
|
|
|
|
|
|
|
|
impl Command for Delete {
|
|
|
|
type Req = DeleteParams;
|
|
|
|
type Resp = Location;
|
|
|
|
|
|
|
|
fn command_name() -> String {
|
|
|
|
"delete".to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request_type() -> RequestType {
|
|
|
|
RequestType::POST
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2021-12-19 21:06:56 +00:00
|
|
|
let location = find_location_by_name(&ctx.db, &req.location, user.id.unwrap())?;
|
2021-12-18 03:03:15 +00:00
|
|
|
let location = load_location(&ctx.db, &location).map_err(GeoffreyAPIError::from)?;
|
|
|
|
|
|
|
|
ctx.db.remove::<LocationDb>(location.id)?;
|
|
|
|
|
|
|
|
Ok(location)
|
|
|
|
}
|
|
|
|
}
|