// 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::recipe_parent::{RecipeParent, RecipeParentId}, }; use sqlx::query; impl RecipeParent { pub(crate) async fn get_all(app: &App) -> Result, get_all::Error> { let records = query!( " SELECT id, parent, name, description FROM recipe_parents " ) .fetch_all(&app.db) .await?; let mut all = Vec::with_capacity(records.len()); for record in records { let parent = Self { id: RecipeParentId::from_db(&record.id), parent: record.parent.map(|parent| RecipeParentId::from_db(&parent)), name: record.name, description: record.description, }; all.push(parent); } Ok(all) } pub(crate) async fn from_id( app: &App, id: RecipeParentId, ) -> Result, from_id::Error> { let record = query!( " SELECT parent, name, description FROM recipe_parents WHERE id = ? ", id ) .fetch_optional(&app.db) .await?; match record { Some(record) => Ok(Some(Self { id, parent: record.parent.map(|parent| RecipeParentId::from_db(&parent)), name: record.name, description: record.description, })), None => Ok(None), } } } 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 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 {} }