From 7879f69255462952910e5551763357421d4c80ed Mon Sep 17 00:00:00 2001 From: Matthias Lodner Date: Mon, 26 Jan 2026 16:51:29 +0100 Subject: [PATCH] Added update and delete functionality to article with cascade for variants. --- common/src/models/article.rs | 53 ++++++++ frontend/app/components/article/article.vue | 117 ++++++++++++++++-- .../app/components/article/pickArticle.vue | 3 +- .../app/components/variant/variantRow.vue | 1 - frontend/app/store/labelEditorStore.ts | 52 ++++++-- .../20260126152946_cascade_on_delete.sql | 7 ++ server/src/main.rs | 52 +++++++- 7 files changed, 266 insertions(+), 19 deletions(-) create mode 100644 server/migrations/20260126152946_cascade_on_delete.sql diff --git a/common/src/models/article.rs b/common/src/models/article.rs index 0aaa425..5608cf6 100644 --- a/common/src/models/article.rs +++ b/common/src/models/article.rs @@ -141,3 +141,56 @@ pub async fn insert_article(article: &Article, pool: PgPool) -> Option
} } } + +pub async fn update_article(article_id: i64, article: &Article, pool: PgPool) -> Option
{ + let mut args = PgArguments::default(); + args.add(&article.id); + args.add(&article.name); + args.add(&article.bio); + args.add(&article.origin); + args.add(&article.description); + args.add(&article.ingredients); + + let statement = format!( + "UPDATE {} SET name = $2, bio = $3, origin = $4, description = $5, ingredients = $6 WHERE id = $1 RETURNING *", + "article", + ); + + let res = sqlx::query_with( + statement.as_str(), + args) + .fetch_one(&pool).await; + + info!("{:#?}", res); + + match res { + Ok(record) => { + let variant = from_postgres_row_small(record); + Some(variant) + } + Err(err) => { + error!("Error while updating variant: {}", article_id); + None + } + } +} + +pub async fn delete_article(article_id: i64, pool: PgPool) -> Option<()> { + let result = sqlx::query( + format!("DELETE FROM {} WHERE id = {}", + "article", + article_id + ).as_str() + ).execute(&pool).await; + + match result { + Ok(_) => { + Some(()) + } + Err(err) => { + println!("Error deleting article: {:#?}", err); + + None + } + } +} diff --git a/frontend/app/components/article/article.vue b/frontend/app/components/article/article.vue index 60b440e..a0b9ef1 100644 --- a/frontend/app/components/article/article.vue +++ b/frontend/app/components/article/article.vue @@ -1,16 +1,115 @@