Skip to content

Commit

Permalink
feat: adding new action of comment count decrease to patch thread lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonNotJson committed Sep 17, 2023
1 parent 07309e5 commit 149f2d0
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/lambda/patch-thread/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 149f2d0

Please sign in to comment.