From b6c9f541e2bca69ecbcad37fee6d81f393ec1525 Mon Sep 17 00:00:00 2001 From: khorshuheng Date: Mon, 29 Jul 2024 14:47:33 +0800 Subject: [PATCH] fix: delete reaction from comment SQL query should be based on comment id, not view id --- ...4d59efde9ab71798ff8b31ce7320e02344d4e8b3e42.json} | 4 ++-- libs/client-api/src/http_publish.rs | 2 +- libs/database/src/workspace.rs | 10 +++++----- tests/workspace/publish.rs | 12 ++++++------ 4 files changed, 14 insertions(+), 14 deletions(-) rename .sqlx/{query-3bf9811b3cfc16b677c76acee21342b892cf815954e4516589493aae01555dc0.json => query-816a026ca4c25329b2fb24d59efde9ab71798ff8b31ce7320e02344d4e8b3e42.json} (56%) diff --git a/.sqlx/query-3bf9811b3cfc16b677c76acee21342b892cf815954e4516589493aae01555dc0.json b/.sqlx/query-816a026ca4c25329b2fb24d59efde9ab71798ff8b31ce7320e02344d4e8b3e42.json similarity index 56% rename from .sqlx/query-3bf9811b3cfc16b677c76acee21342b892cf815954e4516589493aae01555dc0.json rename to .sqlx/query-816a026ca4c25329b2fb24d59efde9ab71798ff8b31ce7320e02344d4e8b3e42.json index 76dfc942..93decd8c 100644 --- a/.sqlx/query-3bf9811b3cfc16b677c76acee21342b892cf815954e4516589493aae01555dc0.json +++ b/.sqlx/query-816a026ca4c25329b2fb24d59efde9ab71798ff8b31ce7320e02344d4e8b3e42.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n DELETE FROM af_published_view_reaction\n WHERE view_id = $1 AND created_by = (SELECT uid FROM af_user WHERE uuid = $2) AND reaction_type = $3\n ", + "query": "\n DELETE FROM af_published_view_reaction\n WHERE comment_id = $1 AND created_by = (SELECT uid FROM af_user WHERE uuid = $2) AND reaction_type = $3\n ", "describe": { "columns": [], "parameters": { @@ -12,5 +12,5 @@ }, "nullable": [] }, - "hash": "3bf9811b3cfc16b677c76acee21342b892cf815954e4516589493aae01555dc0" + "hash": "816a026ca4c25329b2fb24d59efde9ab71798ff8b31ce7320e02344d4e8b3e42" } diff --git a/libs/client-api/src/http_publish.rs b/libs/client-api/src/http_publish.rs index 423627a3..389a9f69 100644 --- a/libs/client-api/src/http_publish.rs +++ b/libs/client-api/src/http_publish.rs @@ -111,8 +111,8 @@ impl Client { pub async fn create_reaction_on_comment( &self, reaction_type: &str, - comment_id: &uuid::Uuid, view_id: &uuid::Uuid, + comment_id: &uuid::Uuid, ) -> Result<(), AppResponseError> { let url = format!( "{}/api/workspace/published-info/{}/reaction", diff --git a/libs/database/src/workspace.rs b/libs/database/src/workspace.rs index 477c7c32..87010178 100644 --- a/libs/database/src/workspace.rs +++ b/libs/database/src/workspace.rs @@ -1365,24 +1365,24 @@ pub async fn insert_reaction_on_comment<'a, E: Executor<'a, Database = Postgres> pub async fn delete_reaction_from_comment<'a, E: Executor<'a, Database = Postgres>>( executor: E, - view_id: &Uuid, + comment_id: &Uuid, user_uuid: &Uuid, reaction_type: &str, ) -> Result<(), AppError> { let res = sqlx::query!( r#" DELETE FROM af_published_view_reaction - WHERE view_id = $1 AND created_by = (SELECT uid FROM af_user WHERE uuid = $2) AND reaction_type = $3 + WHERE comment_id = $1 AND created_by = (SELECT uid FROM af_user WHERE uuid = $2) AND reaction_type = $3 "#, - view_id, + comment_id, user_uuid, reaction_type, ).execute(executor).await?; if res.rows_affected() != 1 { tracing::error!( - "Failed to delete reaction from published view, view_id: {}, user_id: {}, reaction_type: {}, rows_affected: {}", - view_id, user_uuid, reaction_type, res.rows_affected() + "Failed to delete reaction from published comment, comment_id: {}, user_id: {}, reaction_type: {}, rows_affected: {}", + comment_id, user_uuid, reaction_type, res.rows_affected() ); }; diff --git a/tests/workspace/publish.rs b/tests/workspace/publish.rs index 6f714de6..27283d9f 100644 --- a/tests/workspace/publish.rs +++ b/tests/workspace/publish.rs @@ -454,23 +454,23 @@ async fn test_publish_reactions() { let like_emoji = "👍"; let party_emoji = "🎉"; page_owner_client - .create_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) + .create_reaction_on_comment(like_emoji, &view_id, &likable_comment_id) .await .unwrap(); let guest_client = localhost_client(); let result = guest_client - .create_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) + .create_reaction_on_comment(like_emoji, &view_id, &likable_comment_id) .await; assert!(result.is_err()); assert_eq!(result.unwrap_err().code, ErrorCode::NotLoggedIn); let (user_client, _) = generate_unique_registered_user_client().await; user_client - .create_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) + .create_reaction_on_comment(like_emoji, &view_id, &likable_comment_id) .await .unwrap(); user_client - .create_reaction_on_comment(party_emoji, &party_comment_id, &view_id) + .create_reaction_on_comment(party_emoji, &view_id, &party_comment_id) .await .unwrap(); @@ -489,12 +489,12 @@ async fn test_publish_reactions() { // Test if the reactions are deleted correctly based on view and comment id let result = guest_client - .delete_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) + .delete_reaction_on_comment(like_emoji, &view_id, &likable_comment_id) .await; assert!(result.is_err()); assert_eq!(result.unwrap_err().code, ErrorCode::NotLoggedIn); user_client - .delete_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) + .delete_reaction_on_comment(like_emoji, &view_id, &likable_comment_id) .await .unwrap();