// 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 crate::storage::sql::{ config::Config, insert::{Operations, Transactionable}, }; #[derive(Debug, Deserialize, Serialize)] pub(crate) enum Operation { UseDefault { value: bool, old_value: bool }, } 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::UseDefault { value, .. } => { query!( " UPDATE rocie_config SET use_defaults = ? WHERE id = 0 ", value, ) .execute(txn) .await?; } } Ok(()) } async fn undo(self, txn: &mut sqlx::SqliteConnection) -> Result<(), undo::Error> { match self { Operation::UseDefault { old_value, .. } => { query!( " UPDATE rocie_config SET use_defaults = ? WHERE id = 0 ", old_value, ) .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 Config { pub(crate) fn set_use_default(&mut self, new_value: bool, ops: &mut Operations) { if self.should_use_defaults != new_value { ops.push(Operation::UseDefault { value: new_value, old_value: self.should_use_defaults, }); self.should_use_defaults = new_value; } } }