49 lines
1.4 KiB
Rust
49 lines
1.4 KiB
Rust
use crate::commands::{Command, RequestType};
|
|
use crate::context::Context;
|
|
use crate::helper::get_player_from_req;
|
|
use crate::Result;
|
|
use geoffrey_models::models::locations::Location;
|
|
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 = Location::new(
|
|
args.name.as_str(),
|
|
args.position,
|
|
player.id.unwrap(),
|
|
args.tunnel.clone(),
|
|
args.loc_type.clone(),
|
|
);
|
|
|
|
ctx.db
|
|
.insert(location)
|
|
.map_err(|err| GeoffreyAPIError::DatabaseError(err.to_string()))
|
|
} else {
|
|
Err(GeoffreyAPIError::PlayerNotRegistered)
|
|
}
|
|
}
|
|
}
|