Delete post fix, proper cascading on comments

This commit is contained in:
Imbus 2024-03-22 23:20:47 +01:00
parent 61ab9e072e
commit d2eac68638
2 changed files with 10 additions and 3 deletions

View file

@ -6,9 +6,9 @@ CREATE TABLE IF NOT EXISTS comments (
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (parent_post_id) REFERENCES posts (id),
FOREIGN KEY (parent_post_id) REFERENCES posts (id) ON DELETE CASCADE,
-- FOREIGN KEY (parent_comment_id) REFERENCES comments (id),
FOREIGN KEY (author_user_id) REFERENCES users (id)
FOREIGN KEY (author_user_id) REFERENCES users (id) ON DELETE CASCADE
);
-- Create a function to set created_at and updated_at on INSERT

View file

@ -153,7 +153,14 @@ pub async fn delete_post(
.await;
match q {
Ok(_) => Ok(HttpResponse::Ok().json("Deleted")),
Ok(q) => {
// Does this include cascading deletes?
if q.rows_affected() == 1 {
Ok(HttpResponse::Ok().json("Deleted"))
} else {
Ok(HttpResponse::Forbidden().json("Forbidden"))
}
}
Err(e) => {
info!("Error deleting post: {}", e);
Ok(HttpResponse::InternalServerError().json("Error"))