// 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 serde::{Deserialize, Serialize}; use sqlx::query; use uuid::Uuid; use crate::storage::sql::{ insert::{Operations, Transactionable}, recipe_parent::{RecipeParent, RecipeParentId}, }; #[derive(Debug, Deserialize, Serialize)] pub(crate) enum Operation { RegisterRecipeParent { id: RecipeParentId, name: String, description: Option, parent: Option, }, } impl Transactionable for Operation { type ApplyError = apply::Error; type UndoError = undo::Error; async fn apply(self, txn: &mut sqlx::SqliteConnection) -> Result<(), apply::Error> { match self { Operation::RegisterRecipeParent { id, name, description, parent, } => { query!( " INSERT INTO recipe_parents (id, name, description, parent) VALUES (?,?,?,?) ", id, name, description, parent ) .execute(txn) .await?; } } Ok(()) } async fn undo(self, txn: &mut sqlx::SqliteConnection) -> Result<(), undo::Error> { match self { Operation::RegisterRecipeParent { id, name, description, parent, } => { query!( " DELETE FROM recipe_parents WHERE id = ? AND name = ? AND description = ? AND parent = ?; ", id, name, description, parent, ) .execute(txn) .await?; } } Ok(()) } } pub(crate) mod undo { #[derive(thiserror::Error, Debug)] pub(crate) enum Error { #[error("Failed to execute undo sql statments: {0}")] SqlError(#[from] sqlx::Error), } } pub(crate) mod apply { #[derive(thiserror::Error, Debug)] pub(crate) enum Error { #[error("Failed to execute apply sql statments: {0}")] SqlError(#[from] sqlx::Error), } } impl RecipeParent { pub(crate) fn register( name: String, description: Option, parent: Option, ops: &mut Operations, ) -> Self { let id = RecipeParentId::from(Uuid::new_v4()); ops.push(Operation::RegisterRecipeParent { id, name: name.clone(), description: description.clone(), parent, }); Self { id, name, description, parent, } } }