aboutsummaryrefslogtreecommitdiffstats
path: root/crates/rocie-server/src/storage/sql/get
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src/storage/sql/get')
-rw-r--r--crates/rocie-server/src/storage/sql/get/mod.rs1
-rw-r--r--crates/rocie-server/src/storage/sql/get/product/mod.rs54
-rw-r--r--crates/rocie-server/src/storage/sql/get/unit/mod.rs79
3 files changed, 118 insertions, 16 deletions
diff --git a/crates/rocie-server/src/storage/sql/get/mod.rs b/crates/rocie-server/src/storage/sql/get/mod.rs
index 2268e85..fa22f81 100644
--- a/crates/rocie-server/src/storage/sql/get/mod.rs
+++ b/crates/rocie-server/src/storage/sql/get/mod.rs
@@ -1 +1,2 @@
pub(crate) mod product;
+pub(crate) mod unit;
diff --git a/crates/rocie-server/src/storage/sql/get/product/mod.rs b/crates/rocie-server/src/storage/sql/get/product/mod.rs
index bcc3e32..d23297a 100644
--- a/crates/rocie-server/src/storage/sql/get/product/mod.rs
+++ b/crates/rocie-server/src/storage/sql/get/product/mod.rs
@@ -1,6 +1,9 @@
use crate::{
app::App,
- storage::sql::product::{Product, ProductId},
+ storage::sql::{
+ product::{Barcode, Product, ProductId, UnitAmount},
+ unit::UnitId,
+ },
};
use sqlx::query;
@@ -40,17 +43,38 @@ impl Product {
.fetch_all(&app.db)
.await?;
- Ok(records
- .into_iter()
- .map(|record| {
- Self {
- id: ProductId::from_db(&record.id),
- name: record.name,
- description: record.description,
- associated_bar_codes: vec![], // todo
- }
- })
- .collect())
+ let mut all = Vec::with_capacity(records.len());
+ for record in records {
+ let barcodes = query!(
+ "
+ SELECT id, amount, unit
+ FROM barcodes
+ WHERE product_id = ?
+",
+ record.id,
+ )
+ .fetch_all(&app.db)
+ .await?;
+
+ all.push(Self {
+ id: ProductId::from_db(&record.id),
+ name: record.name,
+ description: record.description,
+ associated_bar_codes: barcodes
+ .into_iter()
+ .map(|record| Barcode {
+ id: u32::try_from(record.id).expect("Should be strictly positive"),
+ amount: UnitAmount {
+ value: u32::try_from(record.amount)
+ .expect("Should be strictly positve"),
+ unit: UnitId::from_db(&record.unit),
+ },
+ })
+ .collect(),
+ });
+ }
+
+ Ok(all)
}
}
@@ -63,8 +87,7 @@ pub(crate) mod from_id {
SqlError(#[from] sqlx::Error),
}
- impl ResponseError for Error {
- }
+ impl ResponseError for Error {}
}
pub(crate) mod get_all {
@@ -76,6 +99,5 @@ pub(crate) mod get_all {
SqlError(#[from] sqlx::Error),
}
- impl ResponseError for Error {
- }
+ impl ResponseError for Error {}
}
diff --git a/crates/rocie-server/src/storage/sql/get/unit/mod.rs b/crates/rocie-server/src/storage/sql/get/unit/mod.rs
new file mode 100644
index 0000000..6c2bbcc
--- /dev/null
+++ b/crates/rocie-server/src/storage/sql/get/unit/mod.rs
@@ -0,0 +1,79 @@
+use crate::{
+ app::App,
+ storage::sql::unit::{Unit, UnitId},
+};
+
+use sqlx::query;
+
+impl Unit {
+ pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> {
+ let records = query!(
+ "
+ SELECT id, full_name_singular, full_name_plural, short_name, description
+ FROM units
+"
+ )
+ .fetch_all(&app.db)
+ .await?;
+
+ Ok(records
+ .into_iter()
+ .map(|record| Self {
+ id: UnitId::from_db(&record.id),
+ full_name_singular: record.full_name_singular,
+ full_name_plural: record.full_name_plural,
+ short_name: record.short_name,
+ description: record.description,
+ })
+ .collect())
+ }
+
+ pub(crate) async fn from_id(app: &App, id: UnitId) -> Result<Option<Self>, from_id::Error> {
+ let record = query!(
+ "
+ SELECT full_name_singular, full_name_plural, short_name, description
+ FROM units
+ WHERE id = ?
+",
+ id
+ )
+ .fetch_optional(&app.db)
+ .await?;
+
+ if let Some(record) = record {
+ Ok(Some(Self {
+ id,
+ full_name_singular: record.full_name_singular,
+ full_name_plural: record.full_name_plural,
+ short_name: record.short_name,
+ description: record.description,
+ }))
+ } else {
+ Ok(None)
+ }
+ }
+}
+
+pub(crate) mod get_all {
+ use actix_web::ResponseError;
+
+ #[derive(thiserror::Error, Debug)]
+ pub(crate) enum Error {
+ #[error("Failed to execute the sql query")]
+ SqlError(#[from] sqlx::Error),
+ }
+
+ impl ResponseError for Error {}
+}
+
+pub(crate) mod from_id {
+ use actix_web::ResponseError;
+
+ #[derive(thiserror::Error, Debug)]
+ pub(crate) enum Error {
+ #[error("Failed to execute the sql query")]
+ SqlError(#[from] sqlx::Error),
+ }
+
+ impl ResponseError for Error {}
+}