Skip to content

Commit

Permalink
feat: adding more logic to patch and get threads related to likes and…
Browse files Browse the repository at this point in the history
… comments
  • Loading branch information
JasonNotJson committed Sep 17, 2023
1 parent 02264ea commit 07309e5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
63 changes: 35 additions & 28 deletions src/lambda/get-single-thread/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
1 change: 1 addition & 0 deletions src/lambda/patch-thread/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 07309e5

Please sign in to comment.