From 8565c62022c13a01e7114b4cda112d7ad8736651 Mon Sep 17 00:00:00 2001 From: dvalnn Date: Tue, 28 May 2024 23:10:27 +0100 Subject: [PATCH] delivery statistics --- ...be0c668306ad8ed84cca8d64e8b2ae8542358.json | 46 +++++++++++++++++++ migrations/015_metada.sql | 11 +++++ src/db_api/mod.rs | 2 + src/db_api/statistics.rs | 42 +++++++++++++++++ src/db_api/transformations.rs | 1 - src/routes/mod.rs | 20 +++++++- src/startup.rs | 1 + 7 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 .sqlx/query-165f26ea2f3a7964d6bfd3e0168be0c668306ad8ed84cca8d64e8b2ae8542358.json create mode 100644 src/db_api/statistics.rs diff --git a/.sqlx/query-165f26ea2f3a7964d6bfd3e0168be0c668306ad8ed84cca8d64e8b2ae8542358.json b/.sqlx/query-165f26ea2f3a7964d6bfd3e0168be0c668306ad8ed84cca8d64e8b2ae8542358.json new file mode 100644 index 0000000..7bc5449 --- /dev/null +++ b/.sqlx/query-165f26ea2f3a7964d6bfd3e0168be0c668306ad8ed84cca8d64e8b2ae8542358.json @@ -0,0 +1,46 @@ +{ + "db_name": "PostgreSQL", + "query": "\n INSERT INTO delivery_statistics\n (line, piece, quantity, associated_order_id)\n VALUES ($1, $2, $3, $4)\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + { + "Custom": { + "name": "delivery_line", + "kind": { + "Enum": [ + "DL1", + "DL2", + "DL3", + "DL4" + ] + } + } + }, + { + "Custom": { + "name": "piece_kind", + "kind": { + "Enum": [ + "P1", + "P2", + "P3", + "P4", + "P5", + "P6", + "P7", + "P8", + "P9" + ] + } + } + }, + "Int4", + "Uuid" + ] + }, + "nullable": [] + }, + "hash": "165f26ea2f3a7964d6bfd3e0168be0c668306ad8ed84cca8d64e8b2ae8542358" +} diff --git a/migrations/015_metada.sql b/migrations/015_metada.sql index 303e2a9..b505f7d 100644 --- a/migrations/015_metada.sql +++ b/migrations/015_metada.sql @@ -20,3 +20,14 @@ CREATE TRIGGER complete_producing_orders_after_update AFTER UPDATE ON epoch_table FOR EACH ROW EXECUTE FUNCTION run_daily_order_complete_check(); + + +CREATE type delivery_line AS ENUM ('DL1', 'DL2', 'DL3', 'DL4'); + +CREATE TABLE IF NOT EXISTS delivery_statistics( + id BIGINT GENERATED ALWAYS AS IDENTITY, + line delivery_line NOT NULL, + piece piece_kind NOT NULL, + quantity int NOT NULL, + associated_order_id uuid REFERENCES orders(id) NOT NULL +); diff --git a/src/db_api/mod.rs b/src/db_api/mod.rs index a6aa922..fbaaefa 100644 --- a/src/db_api/mod.rs +++ b/src/db_api/mod.rs @@ -5,6 +5,7 @@ mod orders; mod pieces; mod recipes; mod shipments; +mod statistics; mod suppliers; mod transformations; @@ -15,6 +16,7 @@ pub use orders::*; pub use pieces::*; pub use recipes::*; pub use shipments::*; +pub use statistics::*; pub use suppliers::*; pub use transformations::*; diff --git a/src/db_api/statistics.rs b/src/db_api/statistics.rs new file mode 100644 index 0000000..c9cd03e --- /dev/null +++ b/src/db_api/statistics.rs @@ -0,0 +1,42 @@ +use serde::{Deserialize, Serialize}; +use sqlx::PgConnection; +use uuid::Uuid; + +use super::FinalPiece; + +#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type)] +#[sqlx(type_name = "delivery_line")] +pub enum DeliveryLines { + DL1, + DL2, + DL3, + DL4, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct DeliveryStatistics { + line: DeliveryLines, + piece: FinalPiece, + quantity: i32, + associated_order_id: Uuid, +} + +impl DeliveryStatistics { + pub async fn insert(&self, con: &mut PgConnection) -> sqlx::Result<()> { + sqlx::query!( + r#" + INSERT INTO delivery_statistics + (line, piece, quantity, associated_order_id) + VALUES ($1, $2, $3, $4) + "#, + self.line as DeliveryLines, + self.piece as FinalPiece, + self.quantity, + self.associated_order_id + ) + .execute(con) + .await?; + + Ok(()) + } +} diff --git a/src/db_api/transformations.rs b/src/db_api/transformations.rs index deac3fe..3264c13 100644 --- a/src/db_api/transformations.rs +++ b/src/db_api/transformations.rs @@ -1,5 +1,4 @@ use serde::Serialize; -use sqlx::Acquire; use sqlx::PgConnection; use sqlx::PgPool; use uuid::Uuid; diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 906967e..1bdea8f 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -13,8 +13,8 @@ use sqlx::{postgres::types::PgMoney, PgPool}; use uuid::Uuid; use crate::db_api::{ - self, Item, Order, OrderStatus, RawMaterial, Shipment, Transformation, - TransformationDetails, + self, DeliveryStatistics, Item, Order, OrderStatus, RawMaterial, Shipment, + Transformation, TransformationDetails, }; fn internal_server_error(e: impl Debug + Display) -> HttpResponse { @@ -440,6 +440,22 @@ pub async fn post_delivery_confirmation( } } +#[post("/deliveries/statistics")] +pub async fn post_delivery_statistics( + form: Form, + pool: Data, +) -> impl Responder { + let mut con = match pool.acquire().await { + Ok(con) => con, + Err(e) => return internal_server_error(e), + }; + + match form.insert(&mut con).await { + Ok(_) => HttpResponse::Created().finish(), + Err(e) => internal_server_error(e), + } +} + #[cfg(test)] mod tests { use super::{check_health, DayForm}; diff --git a/src/startup.rs b/src/startup.rs index 93ce9e5..b6ee259 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -112,6 +112,7 @@ impl App { .service(routes::post_material_arrival) .service(routes::get_deliveries) .service(routes::post_delivery_confirmation) + .service(routes::post_delivery_statistics) .app_data(Data::new(self.pool.clone())) }) .bind(addr.clone())