// 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::UnitId, unit_property::{UnitProperty, UnitPropertyId}, }, }; use sqlx::query; impl UnitProperty { pub(crate) async fn get_all(app: &App) -> Result, get_all::Error> { let records = query!( " SELECT id, name, description FROM unit_properties " ) .fetch_all(&app.db) .await?; let mut output = Vec::with_capacity(records.len()); for record in records { let units = query!( " SELECT id FROM units WHERE units.unit_property = ? ", record.id ) .fetch_all(&app.db) .await?; let units = units .into_iter() .map(|record| UnitId::from_db(&record.id)) .collect(); output.push(Self { id: UnitPropertyId::from_db(&record.id), units, name: record.name, description: record.description, }) } Ok(output) } pub(crate) async fn from_id( app: &App, id: UnitPropertyId, ) -> Result, from_id::Error> { let record = query!( " SELECT name, description FROM unit_properties WHERE id = ? ", id ) .fetch_optional(&app.db) .await?; if let Some(record) = record { let units = query!( " SELECT id FROM units WHERE units.unit_property = ? ", id ) .fetch_all(&app.db) .await?; let units = units .into_iter() .map(|record| UnitId::from_db(&record.id)) .collect(); Ok(Some(Self { id, units, name: record.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 {} }