From 1c09b0eb5db415985bfefb52786dbe48d757665e Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 6 Sep 2025 18:31:40 +0200 Subject: feat: Provide basic barcode handling support --- .../rocie-server/src/storage/sql/get/unit/mod.rs | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 crates/rocie-server/src/storage/sql/get/unit/mod.rs (limited to 'crates/rocie-server/src/storage/sql/get/unit') 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, 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, 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 {} +} -- cgit 1.4.1