update
Some checks are pending
ci/woodpecker/push/build Pipeline is pending

This commit is contained in:
JMARyA 2025-06-07 21:30:01 +02:00
parent 16353c7683
commit 581361dde6
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 71 additions and 49 deletions

View file

@ -27,6 +27,6 @@ CREATE TABLE transactions (
"location" TEXT, "location" TEXT,
note TEXT, note TEXT,
destination TEXT, destination TEXT,
consumed_price NUMERIC(2), consumed_price DOUBLE PRECISION,
consumed_timestamp timestamptz consumed_timestamp timestamptz
); );

View file

@ -122,7 +122,7 @@ impl Flow {
info.location.as_ref().map(|x| x.as_str()), info.location.as_ref().map(|x| x.as_str()),
info.note.as_ref().map(|x| x.as_str()), info.note.as_ref().map(|x| x.as_str()),
info.quanta, info.quanta,
info.properties.clone() info.properties.clone(),
) )
.await; .await;
ret.entry(item.item_variant_id().clone()) ret.entry(item.item_variant_id().clone())

View file

@ -43,12 +43,12 @@ pub struct Transaction {
pub enum Origin { pub enum Origin {
Flow(String), Flow(String),
Custom(String) Custom(String),
} }
pub enum Destination { pub enum Destination {
Flow(String), Flow(String),
Custom(String) Custom(String),
} }
impl Transaction { impl Transaction {
@ -60,7 +60,7 @@ impl Transaction {
location: Option<&str>, location: Option<&str>,
note: Option<&str>, note: Option<&str>,
quanta: Option<i64>, quanta: Option<i64>,
properties: Option<serde_json::Value> properties: Option<serde_json::Value>,
) -> Self { ) -> Self {
sqlx::query_as("INSERT INTO transactions (item, variant, price, origin, location, note, quanta, properties) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *") sqlx::query_as("INSERT INTO transactions (item, variant, price, origin, location, note, quanta, properties) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *")
.bind(item) .bind(item)
@ -201,9 +201,13 @@ impl Transaction {
impl ToAPI for Transaction { impl ToAPI for Transaction {
async fn api(&self) -> serde_json::Value { async fn api(&self) -> serde_json::Value {
let location = if let Some(loc) = &self.location { let location = if let Some(loc) = &self.location {
if !loc.is_empty() {
Some(get_locations!().get(loc).unwrap().api().await) Some(get_locations!().get(loc).unwrap().api().await)
} else { } else {
None None
}
} else {
None
}; };
let consumed = if self.consumed_timestamp.is_some() { let consumed = if self.consumed_timestamp.is_some() {

View file

@ -34,8 +34,10 @@ pub fn timestamp_range(year: i32, month: u32) -> (i64, i64) {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Variant { pub struct Variant {
/// Associated Item /// Associated Item
#[serde(default)]
pub item: String, pub item: String,
/// Variant ID /// Variant ID
#[serde(default)]
pub variant: String, pub variant: String,
/// Variant Name /// Variant Name
pub name: String, pub name: String,
@ -50,13 +52,13 @@ pub struct Variant {
/// Custom fields as JSON object schema /// Custom fields as JSON object schema
pub meta: Option<serde_json::Value>, pub meta: Option<serde_json::Value>,
/// Quantifiable /// Quantifiable
pub unit: Option<Quanta> pub unit: Option<Quanta>,
} }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Quanta { pub struct Quanta {
pub by: String, pub by: String,
pub conversions: HashMap<String, f64> pub conversions: HashMap<String, f64>,
} }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
@ -67,8 +69,11 @@ pub struct VariantNeedCondition {
impl Variant { impl Variant {
/// Create variant from itemdb yaml /// Create variant from itemdb yaml
pub fn from_yml(json: serde_yaml::Value, variant: &str, item: &str) -> Self { pub fn from_yml(json: serde_yaml::Value, variant_str: &str, item: &str) -> Self {
serde_yaml::from_value(json).unwrap() let mut variant: Variant = serde_yaml::from_value(json).unwrap();
variant.item = item.to_string();
variant.variant = variant_str.to_string();
variant
} }
/// Get a API id for this Item Variant. /// Get a API id for this Item Variant.
@ -143,9 +148,19 @@ impl Variant {
location: Option<&str>, location: Option<&str>,
note: Option<&str>, note: Option<&str>,
quanta: Option<i64>, quanta: Option<i64>,
properties: Option<serde_json::Value> properties: Option<serde_json::Value>,
) -> Transaction { ) -> Transaction {
Transaction::new(&self.item, &self.variant, price, origin, location, note, quanta, properties).await Transaction::new(
&self.item,
&self.variant,
price,
origin,
location,
note,
quanta,
properties,
)
.await
} }
/// Returns all Transactions of this Item Variant /// Returns all Transactions of this Item Variant

View file

@ -141,7 +141,9 @@ async fn rocket() -> _ {
integrity::verify_integrity(&config, &flows, &locations, &itemdb).await; integrity::verify_integrity(&config, &flows, &locations, &itemdb).await;
rocket::build() rocket::build()
.mount("/", route![ .mount(
"/",
route![
routes::item::get_items_route, routes::item::get_items_route,
routes::item::item_route, routes::item::item_route,
routes::item::item_variants_page, routes::item::item_variants_page,
@ -175,7 +177,8 @@ async fn rocket() -> _ {
routes::flow::create_flow_note_route, routes::flow::create_flow_note_route,
routes::flow::flow_notes_route, routes::flow::flow_notes_route,
routes::item::item_image_route routes::item::item_image_route
]) ],
)
.manage(itemdb) .manage(itemdb)
.manage(locations) .manage(locations)
.manage(flows) .manage(flows)

View file

@ -22,7 +22,7 @@ pub struct SupplyForm {
pub location: Option<String>, pub location: Option<String>,
pub note: Option<String>, pub note: Option<String>,
pub quanta: Option<i64>, pub quanta: Option<i64>,
pub properties: Option<serde_json::Value> pub properties: Option<serde_json::Value>,
} }
#[post("/supply", data = "<form>")] #[post("/supply", data = "<form>")]
@ -50,7 +50,7 @@ pub async fn supply_route(
form.location.as_deref(), form.location.as_deref(),
form.note.as_deref(), form.note.as_deref(),
form.quanta, form.quanta,
form.properties.clone() form.properties.clone(),
) )
.await; .await;