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

56 lines
1.5 KiB
Rust

use std::sync::Arc;
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::settings::GeoffreySettings;
use geoffrey_models::models::CommandLevel;
use crate::api_endpoint::{ApiEndpoint, RequestType};
use crate::commands::Command;
use crate::context::Context;
use crate::helper::validate_string_parameter;
use crate::Result;
pub struct AddLocation {}
impl ApiEndpoint for AddLocation {
fn endpoint_name() -> String {
"add_location".to_string()
}
fn request_type() -> RequestType {
RequestType::POST
}
}
impl Command for AddLocation {
type Req = AddLocationParams;
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 location = LocationDb::new(
req.name.as_str(),
req.position,
user.id.unwrap(),
req.portal.clone(),
req.loc_type.into(),
);
let location = ctx.db.insert(location)?;
load_location(&ctx.db, &location).map_err(|err| err.into())
}
fn validate_parameters(req: &Self::Req, cfg: &GeoffreySettings) -> Result<()> {
validate_string_parameter("name", &req.name, cfg.max_str_len)
}
}