Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
delivery statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
dvalnn committed May 28, 2024
1 parent 56161a5 commit 8565c62
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 3 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions migrations/015_metada.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
2 changes: 2 additions & 0 deletions src/db_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod orders;
mod pieces;
mod recipes;
mod shipments;
mod statistics;
mod suppliers;
mod transformations;

Expand All @@ -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::*;

Expand Down
42 changes: 42 additions & 0 deletions src/db_api/statistics.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
1 change: 0 additions & 1 deletion src/db_api/transformations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use serde::Serialize;
use sqlx::Acquire;
use sqlx::PgConnection;
use sqlx::PgPool;
use uuid::Uuid;
Expand Down
20 changes: 18 additions & 2 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -440,6 +440,22 @@ pub async fn post_delivery_confirmation(
}
}

#[post("/deliveries/statistics")]
pub async fn post_delivery_statistics(
form: Form<DeliveryStatistics>,
pool: Data<PgPool>,
) -> 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};
Expand Down
1 change: 1 addition & 0 deletions src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 8565c62

Please sign in to comment.