work
This commit is contained in:
parent
e50b51c829
commit
39905f53c2
5 changed files with 64 additions and 59 deletions
21
src/cache.rs
21
src/cache.rs
|
@ -1,6 +1,8 @@
|
||||||
use mongodb::{bson::doc, ClientSession};
|
use mongodb::{bson::doc, ClientSession};
|
||||||
|
|
||||||
struct InventoryCache {}
|
use crate::get_mongo;
|
||||||
|
|
||||||
|
pub struct InventoryCache {}
|
||||||
|
|
||||||
impl InventoryCache {
|
impl InventoryCache {
|
||||||
pub async fn push(uuid: &str, ses: &mut ClientSession) {
|
pub async fn push(uuid: &str, ses: &mut ClientSession) {
|
||||||
|
@ -21,6 +23,23 @@ impl InventoryCache {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get() -> Vec<String> {
|
||||||
|
let db = get_mongo!();
|
||||||
|
let inventorycache = db
|
||||||
|
.database("cdb")
|
||||||
|
.collection::<mongodb::bson::Document>("cache")
|
||||||
|
.find_one(doc! {"_id": "inventory"}, None)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
inventorycache
|
||||||
|
.get_array("transactions")
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.map(|x| x.as_str().unwrap().to_string())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn remove(uuid: &str, ses: &mut ClientSession) {
|
pub async fn remove(uuid: &str, ses: &mut ClientSession) {
|
||||||
let update = doc! { "$pull": { "transactions": uuid}};
|
let update = doc! { "$pull": { "transactions": uuid}};
|
||||||
|
|
||||||
|
|
|
@ -39,11 +39,10 @@ macro_rules! id_of {
|
||||||
|
|
||||||
pub struct ItemDB {
|
pub struct ItemDB {
|
||||||
index: mdq::Index,
|
index: mdq::Index,
|
||||||
mongodb: mongodb::Client,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ItemDB {
|
impl ItemDB {
|
||||||
pub async fn new(dir: &str, mongodb: &str) -> Self {
|
pub async fn new(dir: &str) -> Self {
|
||||||
// scan for markdown item entries
|
// scan for markdown item entries
|
||||||
let index = mdq::Index::new(dir, true);
|
let index = mdq::Index::new(dir, true);
|
||||||
let mongodb = get_mongo!();
|
let mongodb = get_mongo!();
|
||||||
|
@ -53,13 +52,12 @@ impl ItemDB {
|
||||||
item.init_db(&mongodb).await;
|
item.init_db(&mongodb).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
Self { index, mongodb }
|
Self { index }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves an item by name
|
/// Retrieves an item by name
|
||||||
pub fn get_item(&self, item: &str) -> Option<Item> {
|
pub fn get_item(&self, item: &str) -> Option<Item> {
|
||||||
Some(Item::new(
|
Some(Item::new(
|
||||||
self.mongodb.clone(),
|
|
||||||
self.index
|
self.index
|
||||||
.documents
|
.documents
|
||||||
.iter()
|
.iter()
|
||||||
|
|
65
src/item.rs
65
src/item.rs
|
@ -1,8 +1,7 @@
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{collect_results, id_of};
|
use crate::id_of;
|
||||||
use futures::TryStreamExt;
|
use mongodb::{bson::doc, Collection};
|
||||||
use mongodb::{bson::doc, ClientSession, Collection};
|
|
||||||
|
|
||||||
use crate::variant::Variant;
|
use crate::variant::Variant;
|
||||||
|
|
||||||
|
@ -16,18 +15,14 @@ use crate::variant::Variant;
|
||||||
// DEMAND STATS
|
// DEMAND STATS
|
||||||
// SEASONAL REVIEWS
|
// SEASONAL REVIEWS
|
||||||
|
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
fn hashmap_to_bson_document(
|
||||||
pub struct ItemVariantEntry {
|
hashmap: HashMap<String, mongodb::bson::Document>,
|
||||||
pub item_name: String,
|
) -> mongodb::bson::Document {
|
||||||
pub variant_name: String,
|
let mut document = mongodb::bson::Document::new();
|
||||||
}
|
for (key, value) in hashmap {
|
||||||
|
document.insert(key, value);
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
}
|
||||||
pub struct ItemInventoryEntry {
|
document
|
||||||
pub item_name: String,
|
|
||||||
pub variant_name: String,
|
|
||||||
pub origin: String,
|
|
||||||
pub price: f64,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ItemEntry {
|
pub struct ItemEntry {
|
||||||
|
@ -86,7 +81,8 @@ impl ItemEntry {
|
||||||
let doc = mongodb::bson::doc! {
|
let doc = mongodb::bson::doc! {
|
||||||
"_id": self.name.clone(),
|
"_id": self.name.clone(),
|
||||||
"name": self.name.clone(),
|
"name": self.name.clone(),
|
||||||
"category": self.category.clone()
|
"category": self.category.clone(),
|
||||||
|
"variants": hashmap_to_bson_document(self.variants.iter().map(|x| (x.0.clone(), x.1.as_bson())).collect())
|
||||||
};
|
};
|
||||||
|
|
||||||
if items
|
if items
|
||||||
|
@ -111,43 +107,12 @@ impl ItemEntry {
|
||||||
/// a larger inventory or database of physical goods. It includes fields for
|
/// a larger inventory or database of physical goods. It includes fields for
|
||||||
/// the name of the item and its category.
|
/// the name of the item and its category.
|
||||||
pub struct Item {
|
pub struct Item {
|
||||||
db: mongodb::Client,
|
|
||||||
pub item: ItemEntry,
|
pub item: ItemEntry,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Item {
|
impl Item {
|
||||||
pub fn new(db: mongodb::Client, item: ItemEntry) -> Self {
|
pub fn new(item: ItemEntry) -> Self {
|
||||||
Self { db, item }
|
Self { item }
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_inventory_entries(&self) -> Vec<ItemInventoryEntry> {
|
|
||||||
let quantities: Collection<ItemInventoryEntry> =
|
|
||||||
self.db.database("cdb").collection("item_quantities");
|
|
||||||
|
|
||||||
let mut found = quantities
|
|
||||||
.find(doc! { "item_name": self.item.name.clone()}, None)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
collect_results!(found)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_origins(&self) -> Vec<String> {
|
|
||||||
let quantities: Collection<ItemInventoryEntry> =
|
|
||||||
self.db.database("cdb").collection("item_quantities");
|
|
||||||
|
|
||||||
let mut found = quantities
|
|
||||||
.find(doc! { "item_name": self.item.name.clone()}, None)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut hashset = HashSet::new();
|
|
||||||
|
|
||||||
while let Some(res) = found.try_next().await.unwrap() {
|
|
||||||
hashset.insert(res.origin);
|
|
||||||
}
|
|
||||||
|
|
||||||
hashset.into_iter().collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_variants(&self) -> Vec<String> {
|
pub async fn get_variants(&self) -> Vec<String> {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use actix_web::{get, HttpRequest, Responder};
|
use actix_web::{get, HttpRequest, Responder};
|
||||||
use maud::html;
|
|
||||||
|
|
||||||
mod cache;
|
mod cache;
|
||||||
mod db;
|
mod db;
|
||||||
|
@ -34,7 +33,7 @@ pub(crate) async fn index(r: HttpRequest) -> impl Responder {
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let itemdb = db::ItemDB::new("./itemdb", "mongodb://user:pass@127.0.0.1:27017").await;
|
let itemdb = db::ItemDB::new("./itemdb").await;
|
||||||
let itemdb = actix_web::web::Data::new(itemdb);
|
let itemdb = actix_web::web::Data::new(itemdb);
|
||||||
|
|
||||||
web_base::map!(web_base::Site::new(), |app: actix_web::App<_>| {
|
web_base::map!(web_base::Site::new(), |app: actix_web::App<_>| {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use mongodb::bson::doc;
|
use mongodb::bson::doc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
cache::InventoryCache,
|
||||||
cdb_col, get_mongo,
|
cdb_col, get_mongo,
|
||||||
transaction::{BatchTransaction, Price, Transaction},
|
transaction::{BatchTransaction, Price, Transaction},
|
||||||
};
|
};
|
||||||
|
@ -16,6 +17,7 @@ pub struct Variant {
|
||||||
pub item: String,
|
pub item: String,
|
||||||
pub variant: String,
|
pub variant: String,
|
||||||
pub amount: u64,
|
pub amount: u64,
|
||||||
|
pub depends: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Variant {
|
impl Variant {
|
||||||
|
@ -29,6 +31,18 @@ impl Variant {
|
||||||
.get("amount")
|
.get("amount")
|
||||||
.map(|x| x.as_u64().unwrap())
|
.map(|x| x.as_u64().unwrap())
|
||||||
.unwrap_or(1),
|
.unwrap_or(1),
|
||||||
|
depends: json
|
||||||
|
.as_mapping()
|
||||||
|
.unwrap()
|
||||||
|
.get("depends")
|
||||||
|
.map(|x| {
|
||||||
|
x.as_sequence()
|
||||||
|
.unwrap()
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| x.as_str().unwrap().to_string())
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
.unwrap_or(Vec::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,6 +114,9 @@ impl Variant {
|
||||||
.insert_one_with_session(transaction.as_doc(), None, &mut ses)
|
.insert_one_with_session(transaction.as_doc(), None, &mut ses)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// update cache
|
||||||
|
InventoryCache::push(&transaction.uuid, &mut ses).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
// batch transaction
|
// batch transaction
|
||||||
|
@ -118,10 +135,17 @@ impl Variant {
|
||||||
batch.uuid
|
batch.uuid
|
||||||
};
|
};
|
||||||
|
|
||||||
// todo : transaction overlap cache -> scale
|
|
||||||
|
|
||||||
ses.commit_transaction().await.unwrap();
|
ses.commit_transaction().await.unwrap();
|
||||||
|
|
||||||
ret_uuid
|
ret_uuid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn as_bson(&self) -> mongodb::bson::Document {
|
||||||
|
mongodb::bson::doc! {
|
||||||
|
"item": &self.item,
|
||||||
|
"variant": &self.variant,
|
||||||
|
"amount": self.amount as u32,
|
||||||
|
"depends": &self.depends
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue