2021-12-08 03:33:25 +00:00
|
|
|
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::set_portal_params::SetPortalParams;
|
|
|
|
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 SetPortal {}
|
|
|
|
|
|
|
|
impl Command for SetPortal {
|
|
|
|
type Req = SetPortalParams;
|
|
|
|
type Resp = Location;
|
|
|
|
|
|
|
|
fn command_name() -> String {
|
|
|
|
"set_portal".to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request_type() -> RequestType {
|
|
|
|
RequestType::POST
|
|
|
|
}
|
|
|
|
|
|
|
|
fn command_level() -> CommandLevel {
|
|
|
|
CommandLevel::REGISTERED
|
|
|
|
}
|
|
|
|
|
2021-12-18 03:03:15 +00:00
|
|
|
fn run_command(ctx: Arc<Context>, req: &Self::Req, user: Option<Player>) -> Result<Self::Resp> {
|
2021-12-08 03:33:25 +00:00
|
|
|
let user = user.unwrap();
|
|
|
|
let filter = regex::Regex::new(format!(r"(?i)^{}$", req.loc_name).as_str()).unwrap();
|
|
|
|
|
|
|
|
let mut location: LocationDb = ctx
|
|
|
|
.db
|
|
|
|
.filter(|_, loc: &LocationDb| {
|
|
|
|
loc.owners().contains(&user.id.unwrap()) && filter.is_match(loc.name.as_str())
|
|
|
|
})?
|
|
|
|
.next()
|
|
|
|
.ok_or(GeoffreyAPIError::EntryNotFound)?;
|
|
|
|
|
2021-12-18 03:03:15 +00:00
|
|
|
location.portal = Some(req.portal.clone());
|
2021-12-08 03:33:25 +00:00
|
|
|
|
|
|
|
let location = ctx.db.insert(location)?;
|
|
|
|
|
|
|
|
load_location(&ctx.db, &location).map_err(|err| err.into())
|
|
|
|
}
|
|
|
|
}
|