diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-11-28 16:30:02 +0100 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-11-28 16:30:02 +0100 |
| commit | a62ab5c6dacaddb67931d7ac160bc7faaa707737 (patch) | |
| tree | a35fa3540fbb89f575ab1ea72f9b23ace399e01c /crates/rocie-server/src/storage/sql/insert/recipe | |
| parent | chore(crates/rocie-client): Re-generate (diff) | |
| download | server-a62ab5c6dacaddb67931d7ac160bc7faaa707737.zip | |
feat(crates/rocie-server): Get closer to feature parity between rocie and grocy
Diffstat (limited to 'crates/rocie-server/src/storage/sql/insert/recipe')
| -rw-r--r-- | crates/rocie-server/src/storage/sql/insert/recipe/mod.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/crates/rocie-server/src/storage/sql/insert/recipe/mod.rs b/crates/rocie-server/src/storage/sql/insert/recipe/mod.rs new file mode 100644 index 0000000..b223bfe --- /dev/null +++ b/crates/rocie-server/src/storage/sql/insert/recipe/mod.rs @@ -0,0 +1,95 @@ +use std::path::PathBuf; + +use serde::{Deserialize, Serialize}; +use sqlx::query; +use uuid::Uuid; + +use crate::storage::sql::{ + insert::{Operations, Transactionable}, + recipe::{Recipe, RecipeId}, +}; + +#[derive(Debug, Deserialize, Serialize)] +pub(crate) enum Operation { + New { + id: RecipeId, + path: PathBuf, + content: String, + }, +} + +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::New { id, path, content } => { + let path = path.display().to_string(); + + query!( + " + INSERT INTO recipies (id, path, content) + VALUES (?, ?, ?) +", + id, + path, + content, + ) + .execute(txn) + .await?; + } + } + Ok(()) + } + + async fn undo(self, txn: &mut sqlx::SqliteConnection) -> Result<(), undo::Error> { + match self { + Operation::New { id, path, content } => { + let path = path.display().to_string(); + + query!( + " + DELETE FROM recipies + WHERE id = ? AND path = ? AND content = ? +", + id, + path, + content + ) + .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 Recipe { + pub(crate) fn new(path: PathBuf, content: String, ops: &mut Operations<Operation>) -> Self { + let id = RecipeId::from(Uuid::new_v4()); + + ops.push(Operation::New { + id, + path: path.clone(), + content: content.clone(), + }); + + Self { id, path, content } + } +} |
