add flow notes

This commit is contained in:
JMARyA 2024-09-25 08:38:12 +02:00
parent 132aeee971
commit 860508fa29
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 84 additions and 3 deletions

View file

@ -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(())
}
}

View file

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

View file

@ -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/<id>/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(&notes).await))
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NoteAdd {
pub content: String,
}
#[post("/flow/<id>/note", data = "<form>")]
pub async fn create_flow_note_route(id: &str, form: Json<NoteAdd>) -> 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 }))
}

View file

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