delete posts
cargo devel CI / cargo CI (push) Successful in 3m17s Details

This commit is contained in:
Christoph J. Scherr 2024-06-27 13:14:35 +02:00
parent 0bf4b474df
commit 2edb979aef
2 changed files with 16 additions and 0 deletions

View File

@ -56,6 +56,13 @@ fn repl(conn: &mut SqliteConnection) -> anyhow::Result<()> {
Some(i) => i, Some(i) => i,
None => continue, None => continue,
}; };
if let Err(e) = Post::delete(conn, id){
if let Some(e) = e.downcast_ref::<diesel::result::Error>() {
if matches!(e, diesel::result::Error::NotFound) {
warn!("No post with id {id} exists");
}
}
};
} else if buf.starts_with("LIST") { } else if buf.starts_with("LIST") {
let posts = lib::load_all_posts(conn)?; let posts = lib::load_all_posts(conn)?;
trace!("loaded posts for display: {posts:#?}"); trace!("loaded posts for display: {posts:#?}");

View File

@ -32,6 +32,15 @@ impl Post {
info!("updated post {}", post.id); info!("updated post {}", post.id);
Ok(()) Ok(())
} }
pub fn delete(conn: &mut SqliteConnection, id: i32) -> anyhow::Result<()> {
use crate::schema::posts::dsl::{posts, published};
let post = diesel::delete(posts.find(id))
.returning(Post::as_returning())
.get_result(conn)?;
info!("deleted post {}", post.id);
Ok(())
}
} }
#[derive(Insertable, Debug)] #[derive(Insertable, Debug)]