works on adding from list
This commit is contained in:
26
src/api.rs
26
src/api.rs
@@ -4,6 +4,7 @@ use std::collections::HashSet;
|
|||||||
|
|
||||||
pub type IdList = Vec<i32>;
|
pub type IdList = Vec<i32>;
|
||||||
pub type ItemListWithMods = Vec<(i32, Option<f64>)>;
|
pub type ItemListWithMods = Vec<(i32, Option<f64>)>;
|
||||||
|
pub type ItemList = Vec<db::Item>;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct BuySellParams {
|
pub struct BuySellParams {
|
||||||
@@ -16,7 +17,7 @@ pub struct BuySellParams {
|
|||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct NewGroupLoot {
|
pub struct NewGroupLoot {
|
||||||
source_name: String,
|
source_name: String,
|
||||||
pub items: Vec<db::Item>,
|
pub items: ItemList,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -70,6 +71,7 @@ pub enum ApiActions {
|
|||||||
FetchPlayers,
|
FetchPlayers,
|
||||||
FetchInventory,
|
FetchInventory,
|
||||||
FetchClaims,
|
FetchClaims,
|
||||||
|
CheckItemList(Vec<String>),
|
||||||
// Player level
|
// Player level
|
||||||
FetchPlayer(i32),
|
FetchPlayer(i32),
|
||||||
FetchNotifications(i32),
|
FetchNotifications(i32),
|
||||||
@@ -98,6 +100,28 @@ pub fn execute(
|
|||||||
// Return an Option<String> that describes what happened.
|
// Return an Option<String> that describes what happened.
|
||||||
// If there is some value, store the actions in db so that it can be reversed.
|
// If there is some value, store the actions in db so that it can be reversed.
|
||||||
let action_text: Option<(i32, &str)> = match query {
|
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 => {
|
ApiActions::FetchPlayers => {
|
||||||
response.set_value(Value::PlayerList(db::Players(conn).all()?));
|
response.set_value(Value::PlayerList(db::Players(conn).all()?));
|
||||||
None
|
None
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
use actix_files as fs;
|
use actix_files as fs;
|
||||||
use actix_identity::{CookieIdentityPolicy, Identity, IdentityService, RequestIdentity};
|
use actix_identity::{CookieIdentityPolicy, Identity, IdentityService, RequestIdentity};
|
||||||
|
use actix_service::{Service, Transform};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
dev::{ServiceRequest, ServiceResponse},
|
dev::{ServiceRequest, ServiceResponse},
|
||||||
http::{header, StatusCode},
|
http::{header, StatusCode},
|
||||||
middleware, web, App, Error, HttpResponse, HttpServer,
|
middleware, web, App, Error, HttpResponse, HttpServer,
|
||||||
};
|
};
|
||||||
use actix_service::{Service, Transform};
|
use futures::{
|
||||||
use futures::{Future, future::{ok, Either, FutureResult}};
|
future::{ok, Either, FutureResult},
|
||||||
|
Future,
|
||||||
|
};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use crate::api;
|
use crate::api;
|
||||||
@@ -46,7 +49,6 @@ fn restricted_to_group(id: i32, params: (AppPool, api::ApiActions)) -> MaybeForb
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct RestrictedAccess;
|
struct RestrictedAccess;
|
||||||
|
|
||||||
impl<S, B> Transform<S> for RestrictedAccess
|
impl<S, B> Transform<S> for RestrictedAccess
|
||||||
@@ -67,7 +69,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct RestrictedAccessMiddleware<S> {
|
struct RestrictedAccessMiddleware<S> {
|
||||||
service: S
|
service: S,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, B> Service for RestrictedAccessMiddleware<S>
|
impl<S, B> Service for RestrictedAccessMiddleware<S>
|
||||||
@@ -90,9 +92,9 @@ where
|
|||||||
if is_logged_in {
|
if is_logged_in {
|
||||||
Either::A(self.service.call(req))
|
Either::A(self.service.call(req))
|
||||||
} else {
|
} else {
|
||||||
Either::B(ok(req.into_response(
|
Either::B(ok(
|
||||||
HttpResponse::Forbidden().finish().into_body()
|
req.into_response(HttpResponse::Forbidden().finish().into_body())
|
||||||
)))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,9 +188,16 @@ fn configure_api(config: &mut web::ServiceConfig) {
|
|||||||
"/claims",
|
"/claims",
|
||||||
web::get().to_async(|pool| db_call(pool, Q::FetchClaims)),
|
web::get().to_async(|pool| db_call(pool, Q::FetchClaims)),
|
||||||
)
|
)
|
||||||
.route(
|
.service(
|
||||||
"/items",
|
web::resource("/items")
|
||||||
web::get().to_async(move |pool: AppPool| db_call(pool, Q::FetchInventory)),
|
.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()))
|
||||||
|
},
|
||||||
|
)),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user