use std::sync::Arc; use geoffrey_db::helper::load_location; use geoffrey_db::query::QueryBuilder; 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 crate::api_endpoint::{ApiEndpoint, RequestType}; use crate::commands::Command; use crate::context::Context; use crate::Result; pub struct SetPortal {} impl ApiEndpoint for SetPortal { fn endpoint_name() -> String { "set_portal".to_string() } fn request_type() -> RequestType { RequestType::POST } } impl Command for SetPortal { type Req = SetPortalParams; type Resp = Location; fn command_level() -> CommandLevel { CommandLevel::REGISTERED } fn run_command(ctx: Arc, req: &Self::Req, user: Option) -> Result { let user = user.unwrap(); let query = QueryBuilder::::default() .with_name(&req.loc_name) .with_owner(user.id.unwrap()); let mut location = ctx .db .run_query(query)? .pop() .ok_or(GeoffreyAPIError::EntryNotFound)?; location.portal = Some(req.portal.clone()); let location = ctx.db.insert(location)?; load_location(&ctx.db, &location).map_err(|err| err.into()) } }