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

Commit

Permalink
expected shipments route also returns delayed shipments
Browse files Browse the repository at this point in the history
  • Loading branch information
dvalnn committed May 29, 2024
1 parent 26695a2 commit 6b4a57f
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 165 deletions.

This file was deleted.

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

This file was deleted.

81 changes: 1 addition & 80 deletions src/db_api/shipments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Shipment {
sup.raw_material_kind as "material_type: RawMaterial"
FROM shipments AS ship
JOIN suppliers AS sup ON ship.supplier_id = sup.id
WHERE request_date + delivery_time = $1
WHERE request_date + delivery_time <= $1
AND arrival_date IS NULL
"#,
date
Expand All @@ -81,36 +81,6 @@ impl Shipment {
.await
}

pub async fn get_existing_shipment(
kind: RawMaterial,
arrival_date: i32,
current_date: i32,
con: &mut PgConnection,
) -> sqlx::Result<Option<Shipment>> {
sqlx::query_as!(
Shipment,
r#"
SELECT
ship.id,
ship.supplier_id,
ship.request_date,
ship.quantity,
ship.cost
FROM shipments as ship
JOIN suppliers as sup ON ship.supplier_id = sup.id
WHERE raw_material_kind = $1
AND request_date > $2
AND request_date + sup.delivery_time = $3
AND ship.arrival_date IS NULL
"#,
kind as RawMaterial,
current_date,
arrival_date
)
.fetch_optional(con)
.await
}

pub async fn get_under_allocated(
due_date: i32,
material_kind: RawMaterial,
Expand Down Expand Up @@ -164,58 +134,9 @@ impl Shipment {
Ok(id)
}

pub async fn update(&self, con: &mut PgConnection) -> sqlx::Result<i64> {
let Some(id) = self.id else {
tracing::error!("Shipment update failed: id not found");
return Err(sqlx::Error::RowNotFound);
};

sqlx::query!(
r#"
UPDATE shipments
SET supplier_id = $1, request_date = $2, quantity = $3, cost = $4
WHERE id = $5
"#,
self.supplier_id,
self.request_date,
self.quantity,
self.cost,
id
)
.execute(con)
.await?;

tracing::info!("Updated shipment with id: {}", id);

Ok(id)
}

pub async fn upsert(&self, con: &mut PgConnection) -> sqlx::Result<i64> {
match self.id.is_some() {
true => self.update(con).await,
false => self.insert(con).await,
}
}

pub fn supplier_id(&self) -> i64 {
self.supplier_id
}

pub fn quantity(&self) -> i32 {
self.quantity
}

pub fn add_to_quantity(&mut self, ammount: i32) {
self.quantity += ammount
}

pub fn cost(&self) -> PgMoney {
self.cost
}

pub fn id(&self) -> Option<i64> {
self.id
}
}

pub struct MaterialShipment {
Expand Down

0 comments on commit 6b4a57f

Please sign in to comment.