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

44 lines
1.2 KiB
Rust

use crate::commands::{Command, RequestType};
use crate::context::Context;
use crate::Result;
use geoffrey_models::models::locations::Location;
use geoffrey_models::models::parameters::find_params::FindParams;
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 FindCommand {}
impl Command for FindCommand {
type Req = CommandRequest<FindParams>;
type Resp = Vec<Location>;
fn command_name() -> String {
"find".to_string()
}
fn request_type() -> RequestType {
RequestType::GET
}
fn command_level() -> CommandLevel {
CommandLevel::ALL
}
fn run_command(ctx: Arc<Context>, req: Self::Req) -> Result<Self::Resp> {
let locations = ctx
.db
.filter(|_, loc: &Location| {
let name = loc.name.to_lowercase();
let query = req.arguments.query.to_lowercase();
name.contains(&query)
})
.map_err(|err| GeoffreyAPIError::DatabaseError(err.to_string()))?
.collect();
Ok(locations)
}
}