From 149f2d0eb494e1048636d0471163b273a8dac12e Mon Sep 17 00:00:00 2001 From: Jason Park Date: Sun, 17 Sep 2023 22:45:41 +0900 Subject: [PATCH] feat: adding new action of comment count decrease to patch thread lambda --- src/lambda/patch-thread/index.py | 37 ++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/lambda/patch-thread/index.py b/src/lambda/patch-thread/index.py index 67031c024..c44179321 100644 --- a/src/lambda/patch-thread/index.py +++ b/src/lambda/patch-thread/index.py @@ -71,8 +71,8 @@ def patch_thread(board_id, uid, thread_id, thread, action): ':uid': {uid} }, ) - # Update comment_count by 1 - elif action == 'update_count': + # Increase comment_count by 1 + elif action == 'update_incr': table.update_item( Key={ "board_id": board_id, @@ -89,6 +89,39 @@ def patch_thread(board_id, uid, thread_id, thread, action): } ) + # Decrease comment_count by 1 + elif action == 'update_decr': + # Fetch the current item to get the current comment_count + response = table.get_item( + Key={ + "board_id": board_id, + "thread_id": thread_id, + } + ) + current_comment_count = response['Item'].get('comment_count', 0) + new_comment_flag = True # Default to True + + # If decrementing would result in 0 comments, set newComment to False + if current_comment_count - 1 == 0: + new_comment_flag = False + + # Update the comment_count and newComment flag + table.update_item( + Key={ + "board_id": board_id, + "thread_id": thread_id, + }, + UpdateExpression="SET #c = #c - :decr, #nc = :newComment", + ExpressionAttributeNames={ + '#c': 'comment_count', + '#nc': 'new_comment' + }, + ExpressionAttributeValues={ + ":decr": 1, + ":newComment": new_comment_flag + } + ) + body = JsonPayloadBuilder().add_status( True).add_data(None).add_message('').compile() return body