This commit is contained in:
JMARyA 2024-09-12 10:17:14 +02:00
parent e7fe303941
commit 6f9048e5b1
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 92 additions and 56 deletions

View file

@ -1,4 +1,7 @@
use std::{collections::HashMap, ops::Deref};
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
use serde::Deserialize;
@ -42,3 +45,9 @@ impl<T> Deref for JSONStore<T> {
&self.documents
}
}
impl<T> DerefMut for JSONStore<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.documents
}
}

View file

@ -35,6 +35,7 @@ impl Validate for Location {
impl ToAPI for Location {
async fn api(&self) -> serde_json::Value {
json!({
"id": self._id,
"name": self.name,
"parent": self.parent,
"conditions": self.conditions

View file

@ -1,4 +1,4 @@
use std::ops::Deref;
use std::ops::{Deref, DerefMut};
use json_store::JSONStore;
use location::Location;
@ -49,10 +49,10 @@ async fn rocket() -> _ {
.expect("error creating CORS options");
let itemdb = db::ItemDB::new("./itemdb").await;
let locations: JSONStore<Location> = JSONStore::new("./locations");
let mut locations: JSONStore<Location> = JSONStore::new("./locations");
for location in locations.deref() {
location.1.clone().add(location.0).await;
for location in locations.deref_mut() {
location.1.add(location.0).await;
}
let config = config::get_config();

View file

@ -62,13 +62,13 @@ pub async fn locations_list(
) -> FallibleApiResponse {
check_auth!(t, c);
let mut lst = Vec::with_capacity(locations.len());
let mut ret = HashMap::<String, serde_json::Value>::new();
for id in locations.deref().keys() {
lst.push(id);
for l in locations.iter() {
ret.insert(l.0.clone(), l.1.api().await);
}
Ok(json!(lst))
Ok(json!(ret))
}
#[get("/location_map")]
@ -98,3 +98,16 @@ pub async fn locations_info(
Ok(serde_json::to_value(location_api).unwrap())
}
#[get("/location/<location>/inventory")]
pub async fn location_inventory(
location: &str,
t: Token,
locations: &State<JSONStore<Location>>,
c: &State<Config>,
) -> FallibleApiResponse {
check_auth!(t, c);
// todo : inventory
unimplemented!()
}

View file

@ -76,12 +76,16 @@ impl Transaction {
}
/// Consumes the Item with `price` and `destination`
pub async fn consume(&mut self, price: Price, destination: &str) {
self.update(&json!({
"consumed": Consumed{destination: destination.to_string(),price, timestamp: chrono::Utc::now().timestamp() }
}))
.await
.ok().unwrap();
pub async fn consume(self, price: Price, destination: &str) {
self.change()
.consumed(Some(Consumed {
destination: destination.to_string(),
price,
timestamp: chrono::Utc::now().timestamp(),
}))
.update()
.await
.unwrap();
}
pub async fn is_expired(&self) -> bool {
@ -107,6 +111,15 @@ impl Transaction {
false
}
pub async fn in_location(l: &str) -> Vec<Self> {
Self::find(doc! { "location": l}, None, None).await.unwrap()
}
pub async fn in_location_recursive(l: &str) -> Vec<Self> {
// todo : search and merge sub locations
Self::find(doc! { "location": l}, None, None).await.unwrap()
}
}
impl mongod::ToAPI for Transaction {