// rocie - An enterprise grocery management system // // Copyright (C) 2026 Benedikt Peetz // SPDX-License-Identifier: GPL-3.0-or-later // // This file is part of Rocie. // // You should have received a copy of the License along with this program. // If not, see . use crate::{ app::App, storage::sql::{ unit::{Unit, UnitId}, unit_property::UnitPropertyId, }, }; use sqlx::query; impl Unit { pub(crate) async fn get_all(app: &App) -> Result, get_all::Error> { let records = query!( " SELECT id, unit_property, 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), unit_property: UnitPropertyId::from_db(&record.unit_property), 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, unit_property, 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, unit_property: UnitPropertyId::from_db(&record.unit_property), 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) async fn from_name(app: &App, name: &str) -> Result, from_name::Error> { let record = query!( " SELECT id, full_name_singular, unit_property, full_name_plural, short_name, description FROM units WHERE full_name_singular = ? OR full_name_plural = ? OR short_name = ? ", name, name, name ) .fetch_optional(&app.db) .await?; if let Some(record) = record { Ok(Some(Self { id: UnitId::from_db(&record.id), unit_property: UnitPropertyId::from_db(&record.unit_property), 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 {} } pub(crate) mod from_name { 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 {} }