From 860508fa292c2de7f6f19aec019b1a2d09864427 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Wed, 25 Sep 2024 08:38:12 +0200 Subject: [PATCH] add flow notes --- src/flow.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 +++- src/routes/flow.rs | 34 +++++++++++++++++++++++++++++++-- src/transaction.rs | 2 ++ 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/src/flow.rs b/src/flow.rs index c44134d..07f5cf7 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -215,3 +215,50 @@ impl Flow { .unwrap() } } + +/// A note for a Flow +#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)] +pub struct FlowNote { + /// ID + pub _id: String, + /// Tiemstamp when the note was created + pub timestamp: i64, + /// Note Content + pub content: String, + /// Associated flow + pub on_flow: Reference, +} + +impl FlowNote { + pub async fn create(content: &str, flow: Reference) -> Self { + let s = Self { + _id: uuid::Uuid::new_v4().to_string(), + timestamp: chrono::Utc::now().timestamp(), + content: content.to_string(), + on_flow: flow, + }; + + s.insert().await.unwrap(); + + s + } +} + +impl ToAPI for FlowNote { + async fn api(&self) -> serde_json::Value { + json!({ + "uuid": self._id, + "timestamp": self.timestamp, + "content": self.content, + "on_flow": self.on_flow.id() + }) + } +} + +impl Validate for FlowNote { + async fn validate(&self) -> Result<(), String> { + assert_reference_of!(self.on_flow, Flow); + + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 1095c1d..e003158 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,7 +94,9 @@ async fn rocket() -> _ { routes::item::variant_price_latest_by_origin, routes::item::item_stat_route, routes::flow::active_flows_route, - routes::flow::flow_api_route + routes::flow::flow_api_route, + routes::flow::create_flow_note_route, + routes::flow::flow_notes_route ], ) .manage(itemdb) diff --git a/src/routes/flow.rs b/src/routes/flow.rs index c52cb08..a134e84 100644 --- a/src/routes/flow.rs +++ b/src/routes/flow.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use mongod::{vec_to_api, Model, Referencable, ToAPI}; +use mongod::{reference_of, vec_to_api, Model, Referencable, Sort, ToAPI}; use mongodb::bson::doc; use rocket::{get, post, serde::json::Json, State}; use serde::{Deserialize, Serialize}; @@ -9,7 +9,7 @@ use serde_json::json; use crate::{ check_auth, config::Config, - flow::{Flow, FlowInfo}, + flow::{Flow, FlowInfo, FlowNote}, json_store::JSONStore, routes::{api_error, FallibleApiResponse, Token}, transaction::{Price, Transaction}, @@ -189,3 +189,33 @@ pub async fn continue_flow_route( Ok(json!({"uuid": next_flow._id})) } + +#[get("/flow//notes")] +pub async fn flow_notes_route(id: &str) -> FallibleApiResponse { + let notes = FlowNote::find( + doc! { + "on_flow": reference_of!(Flow, id).ok_or_else(|| api_error("No such flow"))? + }, + None, + Some(doc! { "timestamp": Sort::Descending }), + ) + .await + .unwrap(); + + Ok(json!(vec_to_api(¬es).await)) +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct NoteAdd { + pub content: String, +} + +#[post("/flow//note", data = "
")] +pub async fn create_flow_note_route(id: &str, form: Json) -> FallibleApiResponse { + let note = FlowNote::create( + &form.content, + reference_of!(Flow, id).ok_or_else(|| api_error("No such flow"))?, + ) + .await; + Ok(json!({"uuid": note._id })) +} diff --git a/src/transaction.rs b/src/transaction.rs index a954398..9ead47a 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -10,6 +10,8 @@ use serde_json::json; use crate::{item::Item, location::Location}; +// todo : produced / consumed by flow field? + /// A Transaction of an Item Variant #[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)] pub struct Transaction {