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
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"]

View file

@ -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

View file

@ -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<Variant> {
// todo : check if avail
let variants: Collection<mongodb::bson::Document> =
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<mongodb::bson::Document> =
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<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
.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())
}