diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-09-06 18:31:40 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-09-06 18:31:40 +0200 |
| commit | 1c09b0eb5db415985bfefb52786dbe48d757665e (patch) | |
| tree | db1cdbcff8baae9a73fca34e14b52cb8cf7ff230 /crates/rocie-server/src/storage/sql/get/unit | |
| parent | feat: Provide basic API frame (diff) | |
| download | server-1c09b0eb5db415985bfefb52786dbe48d757665e.zip | |
feat: Provide basic barcode handling support
Diffstat (limited to 'crates/rocie-server/src/storage/sql/get/unit')
| -rw-r--r-- | crates/rocie-server/src/storage/sql/get/unit/mod.rs | 79 |
1 files changed, 79 insertions, 0 deletions
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 {} +} |
