63 lines
1.8 KiB
Rust
63 lines
1.8 KiB
Rust
use std::sync::Arc;
|
|
|
|
use geoffrey_db::helper::load_location;
|
|
use geoffrey_db::query::QueryBuilder;
|
|
use geoffrey_models::models::locations::{Location, LocationDataDb, LocationDb, LocationType};
|
|
use geoffrey_models::models::parameters::item_command_params::ItemCommandParams;
|
|
use geoffrey_models::models::player::Player;
|
|
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
|
|
use geoffrey_models::models::CommandLevel;
|
|
|
|
use crate::api_endpoint::{ApiEndpoint, RequestType};
|
|
use crate::commands::Command;
|
|
use crate::context::Context;
|
|
use crate::Result;
|
|
|
|
pub struct RemoveItem {}
|
|
|
|
impl ApiEndpoint for RemoveItem {
|
|
fn endpoint_name() -> String {
|
|
"remove_item".to_string()
|
|
}
|
|
|
|
fn request_type() -> RequestType {
|
|
RequestType::POST
|
|
}
|
|
}
|
|
|
|
impl Command for RemoveItem {
|
|
type Req = ItemCommandParams;
|
|
type Resp = Location;
|
|
|
|
fn command_level() -> CommandLevel {
|
|
CommandLevel::REGISTERED
|
|
}
|
|
|
|
fn run_command(ctx: Arc<Context>, req: &Self::Req, user: Option<Player>) -> Result<Self::Resp> {
|
|
let user = user.unwrap();
|
|
|
|
let query = QueryBuilder::<LocationDb>::default()
|
|
.with_type(LocationType::Shop)
|
|
.with_name(&req.shop_name)
|
|
.with_owner(user.id.unwrap());
|
|
|
|
let mut shop = ctx
|
|
.db
|
|
.run_query(query)?
|
|
.pop()
|
|
.ok_or(GeoffreyAPIError::EntryNotFound)?;
|
|
|
|
if let LocationDataDb::Shop(shop_data) = &mut shop.loc_data {
|
|
shop_data
|
|
.item_listings
|
|
.retain(|item| item.item.name.to_lowercase() != req.item_name.to_lowercase());
|
|
|
|
let shop = ctx.db.insert(shop)?;
|
|
|
|
load_location(&ctx.db, &shop).map_err(|err| err.into())
|
|
} else {
|
|
Err(GeoffreyAPIError::EntryNotFound)
|
|
}
|
|
}
|
|
}
|