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

50 lines
1.5 KiB
Rust

use crate::commands::{Command, RequestType};
use crate::context::Context;
use crate::helper::get_player_from_req;
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::parameters::CommandRequest;
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
use geoffrey_models::models::CommandLevel;
use std::sync::Arc;
pub struct AddLocation {}
impl Command for AddLocation {
type Req = CommandRequest<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) -> Result<Self::Resp> {
if let Some(player) = get_player_from_req(&ctx.db, &req)? {
let args = &req.arguments;
let location = LocationDb::new(
args.name.as_str(),
args.position,
player.id.unwrap(),
args.tunnel.clone(),
args.loc_type.into(),
);
let location = ctx.db.insert(location)?;
load_location(&ctx.db, &location).map_err(|err| err.into())
} else {
Err(GeoffreyAPIError::PlayerNotRegistered)
}
}
}