This commit is contained in:
JMARyA 2024-05-03 08:47:40 +02:00
parent 6d43193959
commit d042430f0c
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 95 additions and 37 deletions

View file

@ -1,12 +1,17 @@
FROM rust as build FROM rust:buster as builder
WORKDIR /build
COPY ./Cargo.toml /build/Cargo.toml COPY . /app
COPY ./Cargo.lock /build/Cargo.lock WORKDIR /app
COPY ./src /build/src
RUN cargo build --release RUN cargo build --release
FROM rust FROM debian:buster
COPY --from=build /build/target/release/cdb /cdb
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"] CMD ["/cdb"]

View file

@ -1,15 +1,15 @@
version: '3' version: '3'
services: services:
cdb: # cdb:
build: . # build: .
ports: # ports:
- "8080:8080" # - "8080:8080"
depends_on: # depends_on:
- mongodb # - mongodb
volumes: # volumes:
- ./itemdb:/itemdb # - ./itemdb:/itemdb
environment: # environment:
- "DB_URI=mongodb://user:pass@mongodb:27017" # - "DB_URI=mongodb://user:pass@mongodb:27017"
mongodb: mongodb:
image: mongo:latest image: mongo:latest

View file

@ -1,10 +1,9 @@
use std::collections::HashSet; use std::collections::HashSet;
use futures::TryStreamExt; use futures::TryStreamExt;
use mongodb::{bson::doc, Collection}; use mongodb::{bson::doc, ClientSession, Collection};
// todo : api key auth // todo : api key auth
// todo : oid identifiers
// case: itemset // case: itemset
// items describe generic top level structure // items describe generic top level structure
@ -98,7 +97,7 @@ impl ItemEntry {
items items
.insert_one( .insert_one(
mongodb::bson::doc! { mongodb::bson::doc! {
"oid": self.name.clone(), "_id": self.name.clone(),
"name": self.name.clone(), "name": self.name.clone(),
"category": self.category.clone() "category": self.category.clone()
}, },
@ -191,7 +190,6 @@ impl Item {
} }
pub async fn variant(&self, variant: &str) -> Option<Variant> { pub async fn variant(&self, variant: &str) -> Option<Variant> {
// todo : check if avail
let variants: Collection<mongodb::bson::Document> = let variants: Collection<mongodb::bson::Document> =
self.db.database("cdb").collection("variants"); self.db.database("cdb").collection("variants");
let res = variants let res = variants
@ -217,7 +215,7 @@ impl Item {
variants variants
.insert_one( .insert_one(
mongodb::bson::doc! { mongodb::bson::doc! {
"oid": format!("{}-{}", self.item.name.clone(), name), "_id": format!("{}-{}", self.item.name.clone(), name),
"item": self.item.name.clone(), "item": self.item.name.clone(),
"variant": name, "variant": name,
"amount": amount as i64 "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! { mongodb::bson::doc! {
"uuid": self.uuid, "_id": &self.uuid,
"kind": "batch", "kind": "batch",
"transactions": self.transactions "transactions": &self.transactions
} }
} }
fn from(b: mongodb::bson::Document) -> Self { 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 let transactions = b
.get_array("transactions") .get_array("transactions")
.unwrap() .unwrap()
@ -343,13 +341,13 @@ impl Transaction {
} }
} }
fn as_doc(self) -> mongodb::bson::Document { fn as_doc(&self) -> mongodb::bson::Document {
mongodb::bson::doc! { mongodb::bson::doc! {
"oid": self.uuid, "_id": &self.uuid,
"item": self.item, "item": &self.item,
"variant": self.variant, "variant": &self.variant,
"price": self.price.as_bson(), "price": self.price.as_bson(),
"origin": self.origin, "origin": &self.origin,
"timestamp": self.timestamp "timestamp": self.timestamp
} }
} }
@ -373,11 +371,19 @@ impl Transaction {
} }
impl Variant { 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 { pub async fn supply(&self, amount: usize, price: Price, origin: &str) -> String {
// todo : implement db transactions // todo : implement db transactions
let db = get_mongo!(); let db = get_mongo!();
let mut ses = db.start_session(None).await.unwrap();
let col: mongodb::Collection<mongodb::bson::Document> = let col: mongodb::Collection<mongodb::bson::Document> =
db.database("cdb").collection("transactions"); ses.client().database("cdb").collection("transactions");
let mut transactions = vec![]; let mut transactions = vec![];
for _ in 0..amount { for _ in 0..amount {
@ -389,11 +395,58 @@ impl Variant {
)); ));
} }
for transaction in transactions { for transaction in &transactions {
let r = col.insert_one(transaction.as_doc(), None).await; 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<mongodb::bson::Document> = 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::<mongodb::bson::Document>("cache")
.find_one_and_update(doc! { "_id": "inventory"}, update, options)
.await
.unwrap();
}
pub async fn remove(uuid: &str) {
// todo : remove from cache
} }
} }

View file

@ -67,7 +67,7 @@ pub async fn add_variant_route(
let item = itemdb let item = itemdb
.get_item(id) .get_item(id)
.ok_or_else(|| bad_req("The item does not exist"))?; .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()) Ok(HttpResponse::Ok())
} }