Geoffrey-rs/geoffrey_api/src/commands/add_location.rs

45 lines
1.2 KiB
Rust

use crate::commands::{Command, RequestType};
use crate::context::Context;
use crate::Result;
use geoffrey_db::helper::load_location;
use geoffrey_models::models::locations::{Location, LocationDb};
use geoffrey_models::models::parameters::add_location_params::AddLocationParams;
use geoffrey_models::models::player::Player;
use geoffrey_models::models::CommandLevel;
use std::sync::Arc;
pub struct AddLocation {}
impl Command for AddLocation {
type Req = AddLocationParams;
type Resp = Location;
fn command_name() -> String {
"add_location".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();
let location = LocationDb::new(
req.name.as_str(),
req.position,
user.id.unwrap(),
req.tunnel.clone(),
req.loc_type.into(),
);
let location = ctx.db.insert(location)?;
load_location(&ctx.db, &location).map_err(|err| err.into())
}
}