From 07309e5eed3ea1b7897f808f65a9a24a38d29d05 Mon Sep 17 00:00:00 2001 From: Jason Park Date: Sun, 17 Sep 2023 22:31:43 +0900 Subject: [PATCH] feat: adding more logic to patch and get threads related to likes and comments --- src/lambda/get-single-thread/index.py | 63 +++++++++++++++------------ src/lambda/patch-thread/index.py | 1 + 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/lambda/get-single-thread/index.py b/src/lambda/get-single-thread/index.py index adbdd2265..2c6007d7d 100644 --- a/src/lambda/get-single-thread/index.py +++ b/src/lambda/get-single-thread/index.py @@ -5,7 +5,6 @@ @resp_handler def get_single_thread(board_id, thread_id, uid=""): - results = table.query( KeyConditionExpression=Key("board_id").eq( board_id) & Key("thread_id").eq(thread_id) @@ -14,24 +13,43 @@ def get_single_thread(board_id, thread_id, uid=""): if not results: raise LookupError - table.update_item( - Key={ - "board_id": board_id, - "thread_id": thread_id, - }, - UpdateExpression="SET #v = #v + :incr, #nc = :newComment", - ExpressionAttributeNames={ - '#v': 'views', - '#nc': 'new_comment' - }, - ExpressionAttributeValues={ - ":incr": 1, - ":newComment": False - } - ) - item = results[0] + if item["uid"] == uid: + table.update_item( + Key={ + "board_id": board_id, + "thread_id": thread_id, + }, + UpdateExpression="SET #v = #v + :incr, #nc = :newComment", + ConditionExpression="#uid = :uidValue", + ExpressionAttributeNames={ + '#v': 'views', + '#nc': 'new_comment', + '#uid': 'uid' + }, + ExpressionAttributeValues={ + ":incr": 1, + ":newComment": False, + ":uidValue": uid + } + ) + else: + # Increment the view count but do not update new_comment + table.update_item( + Key={ + "board_id": board_id, + "thread_id": thread_id, + }, + UpdateExpression="SET #v = #v + :incr", + ExpressionAttributeNames={ + '#v': 'views' + }, + ExpressionAttributeValues={ + ":incr": 1 + } + ) + item["mod"] = False if item["uid"] == uid: item["mod"] = True @@ -44,14 +62,3 @@ def get_single_thread(board_id, thread_id, uid=""): body = JsonPayloadBuilder().add_status( True).add_data(item).add_message('').compile() return body - - -def handler(event, context): - params = { - "board_id": event["pathParameters"]["board_id"], - "thread_id": event["pathParameters"]["thread_id"], - } - if "uid" in event["queryStringParameters"]: - params["uid"] = event["queryStringParameters"]["uid"] - - return get_single_thread(**params) diff --git a/src/lambda/patch-thread/index.py b/src/lambda/patch-thread/index.py index 4b6341bff..67031c024 100644 --- a/src/lambda/patch-thread/index.py +++ b/src/lambda/patch-thread/index.py @@ -45,6 +45,7 @@ def patch_thread(board_id, uid, thread_id, thread, action): ) current_likes = response['Item'].get('likes', set()) + print("Current likes: ", current_likes) # if only one like, remove the set entirely if len(current_likes) == 1 and uid in current_likes: