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

47 lines
1.3 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::info_params::InfoParams;
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 InfoCommand {}
impl Command for InfoCommand {
type Req = InfoParams;
type Resp = Location;
fn command_name() -> String {
"info".to_string()
}
fn request_type() -> RequestType {
RequestType::GET
}
fn command_level() -> CommandLevel {
CommandLevel::ALL
}
fn run_command(ctx: Arc<Context>, req: &Self::Req, _: Option<Player>) -> Result<Self::Resp> {
let query = req.location_name.to_lowercase();
let location: LocationDb = ctx
.db
.filter(|_, loc: &LocationDb| {
let name = loc.name.to_lowercase();
name == query
})
.map_err(GeoffreyAPIError::from)?
.next()
.ok_or(GeoffreyAPIError::EntryNotFound)?;
Ok(load_location(&ctx.db, &location)?)
}
}