Added update and delete functionality to article with cascade for variants.
This commit is contained in:
@@ -141,3 +141,56 @@ pub async fn insert_article(article: &Article, pool: PgPool) -> Option<Article>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_article(article_id: i64, article: &Article, pool: PgPool) -> Option<Article> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user