diff --git a/src/api.rs b/src/api.rs index 9e40785..30f7a00 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,6 +4,7 @@ use std::collections::HashSet; pub type IdList = Vec; pub type ItemListWithMods = Vec<(i32, Option)>; +pub type ItemList = Vec; #[derive(Serialize, Deserialize, Debug)] pub struct BuySellParams { @@ -16,7 +17,7 @@ pub struct BuySellParams { #[derive(Serialize, Deserialize, Debug)] pub struct NewGroupLoot { source_name: String, - pub items: Vec, + pub items: ItemList, } @@ -70,6 +71,7 @@ pub enum ApiActions { FetchPlayers, FetchInventory, FetchClaims, + CheckItemList(Vec), // Player level FetchPlayer(i32), FetchNotifications(i32), @@ -98,6 +100,28 @@ pub fn execute( // Return an Option that describes what happened. // If there is some value, store the actions in db so that it can be reversed. let action_text: Option<(i32, &str)> = match query { + ApiActions::CheckItemList(names) => { + let (items, errors) = { + let mut found = Vec::new(); + let mut errors = String::new(); + let items = db::Inventory(conn).all()?; + for name in &names { + if let Some(item) = + items.iter().filter(|i| &i.name == name).take(1).next() + { + found.push(item.clone()) + } else { + errors.push_str(&format!("{},\n", name)); + } + } + (found, errors) + }; + + response.set_value(Value::ItemList(items)); + response.push_error(errors); + dbg!(&names, &response); + None + } ApiActions::FetchPlayers => { response.set_value(Value::PlayerList(db::Players(conn).all()?)); None diff --git a/src/server.rs b/src/server.rs index 6816a8c..60b5ea4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,13 +1,16 @@ use actix_cors::Cors; use actix_files as fs; use actix_identity::{CookieIdentityPolicy, Identity, IdentityService, RequestIdentity}; +use actix_service::{Service, Transform}; use actix_web::{ dev::{ServiceRequest, ServiceResponse}, http::{header, StatusCode}, middleware, web, App, Error, HttpResponse, HttpServer, }; -use actix_service::{Service, Transform}; -use futures::{Future, future::{ok, Either, FutureResult}}; +use futures::{ + future::{ok, Either, FutureResult}, + Future, +}; use std::env; use crate::api; @@ -46,7 +49,6 @@ fn restricted_to_group(id: i32, params: (AppPool, api::ApiActions)) -> MaybeForb } } - struct RestrictedAccess; impl Transform for RestrictedAccess @@ -67,7 +69,7 @@ where } struct RestrictedAccessMiddleware { - service: S + service: S, } impl Service for RestrictedAccessMiddleware @@ -90,9 +92,9 @@ where if is_logged_in { Either::A(self.service.call(req)) } else { - Either::B(ok(req.into_response( - HttpResponse::Forbidden().finish().into_body() - ))) + Either::B(ok( + req.into_response(HttpResponse::Forbidden().finish().into_body()) + )) } } } @@ -186,9 +188,16 @@ fn configure_api(config: &mut web::ServiceConfig) { "/claims", web::get().to_async(|pool| db_call(pool, Q::FetchClaims)), ) - .route( - "/items", - web::get().to_async(move |pool: AppPool| db_call(pool, Q::FetchInventory)), + .service( + web::resource("/items") + .route( + web::get().to_async(move |pool: AppPool| db_call(pool, Q::FetchInventory)), + ) + .route(web::post().to_async( + move |pool: AppPool, items: web::Json>| { + db_call(pool, Q::CheckItemList(items.into_inner())) + }, + )), ), ); }