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

74 lines
2.4 KiB
Rust

use crate::commands::{Command, RequestType};
use crate::context::Context;
use crate::Result;
use geoffrey_models::models::locations::{LocationDataDb, LocationDb};
use geoffrey_models::models::parameters::selling_params::SellingParams;
use geoffrey_models::models::player::Player;
use geoffrey_models::models::response::selling_listing::SellingListing;
use geoffrey_models::models::CommandLevel;
use std::sync::Arc;
pub struct Selling {}
impl Command for Selling {
type Req = SellingParams;
type Resp = Vec<SellingListing>;
fn command_name() -> String {
"selling".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 shops: Vec<SellingListing> = ctx
.db
.filter(|_, loc: &LocationDb| {
if let LocationDataDb::Shop(shop_data) = loc.loc_data.clone() {
shop_data.item_listings.iter().any(|item| {
item.item
.name
.to_lowercase()
.contains(&req.query.to_lowercase())
})
} else {
false
}
})?
.filter_map(|shop: LocationDb| {
if let LocationDataDb::Shop(shop_data) = shop.clone().loc_data {
let listings: Vec<SellingListing> = shop_data
.item_listings
.iter()
.filter_map(|item| {
if item.item.name.to_lowercase().contains(&req.query) {
Some(SellingListing {
listing: item.clone(),
shop_name: shop.name.clone(),
shop_loc: shop.position,
portal: shop.tunnel.clone(),
})
} else {
None
}
})
.collect();
Some(listings)
} else {
None
}
})
.flatten()
.collect();
Ok(shops)
}
}