From d2eac68638cb73e4725b78f45e9cfa9793fe5d5d Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 22 Mar 2024 23:20:47 +0100 Subject: [PATCH] Delete post fix, proper cascading on comments --- server/migrations/0003_comments_table.sql | 4 ++-- server/src/routes/post.rs | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/server/migrations/0003_comments_table.sql b/server/migrations/0003_comments_table.sql index d0cbfc7..8e6969f 100644 --- a/server/migrations/0003_comments_table.sql +++ b/server/migrations/0003_comments_table.sql @@ -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 diff --git a/server/src/routes/post.rs b/server/src/routes/post.rs index f1a5d4f..4fc2194 100755 --- a/server/src/routes/post.rs +++ b/server/src/routes/post.rs @@ -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"))