works on adding from list

This commit is contained in:
2019-11-14 16:26:03 +01:00
parent 61a03f1781
commit cb8dfa9a2a
2 changed files with 44 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ use std::collections::HashSet;
pub type IdList = Vec<i32>;
pub type ItemListWithMods = Vec<(i32, Option<f64>)>;
pub type ItemList = Vec<db::Item>;
#[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<db::Item>,
pub items: ItemList,
}
@@ -70,6 +71,7 @@ pub enum ApiActions {
FetchPlayers,
FetchInventory,
FetchClaims,
CheckItemList(Vec<String>),
// Player level
FetchPlayer(i32),
FetchNotifications(i32),
@@ -98,6 +100,28 @@ pub fn execute(
// Return an Option<String> 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

View File

@@ -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<S, B> Transform<S> for RestrictedAccess
@@ -67,7 +69,7 @@ where
}
struct RestrictedAccessMiddleware<S> {
service: S
service: S,
}
impl<S, B> Service for RestrictedAccessMiddleware<S>
@@ -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<Vec<String>>| {
db_call(pool, Q::CheckItemList(items.into_inner()))
},
)),
),
);
}