From d042430f0c86bd61a866c2cd735d824efcd63a11 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Fri, 3 May 2024 08:47:40 +0200 Subject: [PATCH] work --- Dockerfile | 21 ++++++---- docker-compose.yml | 20 +++++----- src/item.rs | 89 +++++++++++++++++++++++++++++++++--------- src/routes/item/mod.rs | 2 +- 4 files changed, 95 insertions(+), 37 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7be8d0b..d38d019 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,17 @@ -FROM rust as build -WORKDIR /build -COPY ./Cargo.toml /build/Cargo.toml -COPY ./Cargo.lock /build/Cargo.lock -COPY ./src /build/src +FROM rust:buster as builder + +COPY . /app +WORKDIR /app + RUN cargo build --release -FROM rust -COPY --from=build /build/target/release/cdb /cdb +FROM debian:buster + +RUN apt update && apt upgrade -y +RUN apt install -y ca-certificates openssl + +COPY --from=builder /app/target/release/cdb /cdb + +WORKDIR / -ENV RUST_LOG=debug CMD ["/cdb"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c61945a..849b4dd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,15 +1,15 @@ version: '3' services: - cdb: - build: . - ports: - - "8080:8080" - depends_on: - - mongodb - volumes: - - ./itemdb:/itemdb - environment: - - "DB_URI=mongodb://user:pass@mongodb:27017" +# cdb: +# build: . +# ports: +# - "8080:8080" +# depends_on: +# - mongodb +# volumes: +# - ./itemdb:/itemdb +# environment: +# - "DB_URI=mongodb://user:pass@mongodb:27017" mongodb: image: mongo:latest diff --git a/src/item.rs b/src/item.rs index 9df7459..35aea2f 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,10 +1,9 @@ use std::collections::HashSet; use futures::TryStreamExt; -use mongodb::{bson::doc, Collection}; +use mongodb::{bson::doc, ClientSession, Collection}; // todo : api key auth -// todo : oid identifiers // case: itemset // items describe generic top level structure @@ -98,7 +97,7 @@ impl ItemEntry { items .insert_one( mongodb::bson::doc! { - "oid": self.name.clone(), + "_id": self.name.clone(), "name": self.name.clone(), "category": self.category.clone() }, @@ -191,7 +190,6 @@ impl Item { } pub async fn variant(&self, variant: &str) -> Option { - // todo : check if avail let variants: Collection = self.db.database("cdb").collection("variants"); let res = variants @@ -217,7 +215,7 @@ impl Item { variants .insert_one( mongodb::bson::doc! { - "oid": format!("{}-{}", self.item.name.clone(), name), + "_id": format!("{}-{}", self.item.name.clone(), name), "item": self.item.name.clone(), "variant": name, "amount": amount as i64 @@ -286,16 +284,16 @@ impl BatchTransaction { } } - fn as_doc(self) -> mongodb::bson::Document { + fn as_doc(&self) -> mongodb::bson::Document { mongodb::bson::doc! { - "uuid": self.uuid, + "_id": &self.uuid, "kind": "batch", - "transactions": self.transactions + "transactions": &self.transactions } } fn from(b: mongodb::bson::Document) -> Self { - let uuid = b.get_str("uuid").unwrap().to_string(); + let uuid = b.get_str("_id").unwrap().to_string(); let transactions = b .get_array("transactions") .unwrap() @@ -343,13 +341,13 @@ impl Transaction { } } - fn as_doc(self) -> mongodb::bson::Document { + fn as_doc(&self) -> mongodb::bson::Document { mongodb::bson::doc! { - "oid": self.uuid, - "item": self.item, - "variant": self.variant, + "_id": &self.uuid, + "item": &self.item, + "variant": &self.variant, "price": self.price.as_bson(), - "origin": self.origin, + "origin": &self.origin, "timestamp": self.timestamp } } @@ -373,11 +371,19 @@ impl Transaction { } impl Variant { + pub async fn demand(&self, uuid: &str) -> String { + // todo : transaction_out + receipt + todo!() + } + pub async fn supply(&self, amount: usize, price: Price, origin: &str) -> String { // todo : implement db transactions let db = get_mongo!(); + + let mut ses = db.start_session(None).await.unwrap(); + let col: mongodb::Collection = - db.database("cdb").collection("transactions"); + ses.client().database("cdb").collection("transactions"); let mut transactions = vec![]; for _ in 0..amount { @@ -389,11 +395,58 @@ impl Variant { )); } - for transaction in transactions { - let r = col.insert_one(transaction.as_doc(), None).await; + for transaction in &transactions { + let r = col + .insert_one_with_session(transaction.as_doc(), None, &mut ses) + .await + .unwrap(); } - todo!() + let batch = BatchTransaction::new(transactions.iter().map(|x| x.uuid.clone()).collect()); + let col: mongodb::Collection = ses + .client() + .database("cdb") + .collection("transactions_batch"); + col.insert_one_with_session(batch.as_doc(), None, &mut ses) + .await + .unwrap(); + + // todo : batch transaction + // todo : transaction overlap cache -> scale + + ses.commit_transaction().await.unwrap(); + + if amount == 1 { + transactions.first().unwrap().uuid.clone() + } else { + batch.uuid.clone() + } + } +} + +struct InventoryCache {} + +impl InventoryCache { + pub async fn push(uuid: &str, ses: &mut ClientSession) { + // todo : if not exists? + let update = doc! { "$push": { "transactions": uuid } }; + + let options = mongodb::options::FindOneAndUpdateOptions::builder() + .upsert(true) + .return_document(mongodb::options::ReturnDocument::After) + .build(); + + let result = ses + .client() + .database("cdb") + .collection::("cache") + .find_one_and_update(doc! { "_id": "inventory"}, update, options) + .await + .unwrap(); + } + + pub async fn remove(uuid: &str) { + // todo : remove from cache } } diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index 0bf7a21..77d8b44 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -67,7 +67,7 @@ pub async fn add_variant_route( let item = itemdb .get_item(id) .ok_or_else(|| bad_req("The item does not exist"))?; - item.add_variant(&f.variant, f.amount); + item.add_variant(&f.variant, f.amount).await; Ok(HttpResponse::Ok()) }