Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: recreating lambdas for forum threads #262

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions src/lambda/delete-thread/index.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
# from boto3.dynamodb.conditions import Attr
# import boto3
from boto3.dynamodb.conditions import Attr
import boto3

# from utils import JsonPayloadBuilder, table, resp_handler
from utils import JsonPayloadBuilder, table, resp_handler


# @resp_handler
# def delete_thread(board_id, thread_id, ts, uid):
# table.delete_item(
# Key={
# "board_id": board_id,
# "created_at": ts
# },
# ConditionExpression=Attr('uid').eq(
# uid) & Attr('thread_id').eq(thread_id)
# )
@resp_handler
def delete_thread(board_id, thread_id, ts, uid):
table.delete_item(
Key={
"board_id": board_id,
"created_at": ts
},
ConditionExpression=Attr('uid').eq(
uid) & Attr('thread_id').eq(thread_id)
)

# resp_body = JsonPayloadBuilder().add_status(
# True).add_data(None).add_message('').compile()
# return resp_body
resp_body = JsonPayloadBuilder().add_status(
True).add_data(None).add_message('').compile()
return resp_body


# def handler(event, context):
# params = {
# "board_id": event["pathParameters"]["board_id"],
# "thread_id": event["pathParameters"]["thread_id"],
# "ts": event["queryStringParameters"]["ts"],
# "uid": event['requestContext']['authorizer']['claims']['sub']
# }

# return delete_thread(**params)
def handler(event, context):
pass
params = {
"board_id": event["pathParameters"]["board_id"],
"thread_id": event["pathParameters"]["thread_id"],
"ts": event["queryStringParameters"]["ts"],
"uid": event['requestContext']['authorizer']['claims']['sub']
}

return delete_thread(**params)
28 changes: 13 additions & 15 deletions src/lambda/get-all-threads/index.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# from boto3.dynamodb.conditions import Key
# import boto3
# from datetime import datetime
# from utils import JsonPayloadBuilder, table, resp_handler
from boto3.dynamodb.conditions import Key
import boto3
from datetime import datetime
from utils import JsonPayloadBuilder, table, resp_handler


# @resp_handler
# def get_all_threads():
@resp_handler
def get_all_threads():

# response = table.scan(TableName=table)
# items = response['Items']
response = table.scan(TableName=table)
items = response['Items']

# body = JsonPayloadBuilder().add_status(
# True).add_data(items).add_message('').compile()
# return body
body = JsonPayloadBuilder().add_status(
True).add_data(items).add_message('').compile()
return body


# def handler(event, context):

# return get_all_threads()
def handler(event, context):
pass

return get_all_threads()
38 changes: 18 additions & 20 deletions src/lambda/get-board-threads/index.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# from boto3.dynamodb.conditions import Key
# import boto3
# from datetime import datetime
# from utils import JsonPayloadBuilder, table, resp_handler
from boto3.dynamodb.conditions import Key
import boto3
from datetime import datetime
from utils import JsonPayloadBuilder, table, resp_handler


# @resp_handler
# def get_board_threads(board_id):
@resp_handler
def get_board_threads(board_id):

# results = table.query(KeyConditionExpression=Key(
# "board_id").eq(board_id), ScanIndexForward=False)["Items"]
# if not results:
# raise LookupError
results = table.query(KeyConditionExpression=Key(
"board_id").eq(board_id), ScanIndexForward=False)["Items"]
if not results:
raise LookupError

# body = JsonPayloadBuilder().add_status(
# True).add_data(results).add_message('').compile()
# return body
body = JsonPayloadBuilder().add_status(
True).add_data(results).add_message('').compile()
return body


# def handler(event, context):
def handler(event, context):

# params = {
# "board_id": event["pathParameters"]["board_id"]
# }
params = {
"board_id": event["pathParameters"]["board_id"]
}

# return get_board_threads(**params)
def handler(event, context):
pass
return get_board_threads(**params)
90 changes: 44 additions & 46 deletions src/lambda/get-group-threads/index.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
# from boto3.dynamodb.conditions import Key, Attr
# import boto3
# from datetime import datetime
# from utils import JsonPayloadBuilder, table, resp_handler


# @resp_handler
# def get_group_threads(board_id, group_id, tag_id):

# if group_id is not None and tag_id is not None:
# results = table.query(KeyConditionExpression=Key(
# "board_id").eq(board_id) & Key("group_id").eq(group_id),
# IndexName="groupIndex",
# FilterExpression=Attr('tag_id').eq(tag_id))["Items"]
# if not results:
# raise LookupError

# body = JsonPayloadBuilder().add_status(
# True).add_data(results).add_message('').compile()
# return body


# def handler(event, context):

# params = {
# "board_id": event["pathParameters"]["board_id"]
# }

# group_id = event["queryStringParameters"]["group_id"]
# tag_id = event["queryStringParameters"]["tag_id"]

# if group_id is not None and tag_id is not None:
# params["group_id"] = group_id
# params["tag_id"] = tag_id
# elif group_id is not None and tag_id is None:
# params["group_id"] = group_id
# params["tag_id"] = None
# elif tag_id is not None and group_id is None:
# params["group_id"] = None
# params["tag_id"] = tag_id
# else:
# params["group_id"] = None
# params["tag_id"] = None

# return get_group_threads(**params)
from boto3.dynamodb.conditions import Key, Attr
import boto3
from datetime import datetime
from utils import JsonPayloadBuilder, table, resp_handler


@resp_handler
def get_group_threads(board_id, group_id, tag_id):

if group_id is not None and tag_id is not None:
results = table.query(KeyConditionExpression=Key(
"board_id").eq(board_id) & Key("group_id").eq(group_id),
IndexName="groupIndex",
FilterExpression=Attr('tag_id').eq(tag_id))["Items"]
if not results:
raise LookupError

body = JsonPayloadBuilder().add_status(
True).add_data(results).add_message('').compile()
return body


def handler(event, context):
pass

params = {
"board_id": event["pathParameters"]["board_id"]
}

group_id = event["queryStringParameters"]["group_id"]
tag_id = event["queryStringParameters"]["tag_id"]

if group_id is not None and tag_id is not None:
params["group_id"] = group_id
params["tag_id"] = tag_id
elif group_id is not None and tag_id is None:
params["group_id"] = group_id
params["tag_id"] = None
elif tag_id is not None and group_id is None:
params["group_id"] = None
params["tag_id"] = tag_id
else:
params["group_id"] = None
params["tag_id"] = None

return get_group_threads(**params)
96 changes: 47 additions & 49 deletions src/lambda/get-single-thread/index.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
# from boto3.dynamodb.conditions import Key, Attr
# from datetime import datetime
# from utils import JsonPayloadBuilder, table, resp_handler


# @resp_handler
# def get_single_thread(board_id, ts, thread_id, uid):

# results = table.query(KeyConditionExpression=Key(
# "board_id").eq(board_id),
# ConditionExpression=Attr('thread_id').eq(thread_id))["Items"]
# if not results:
# raise LookupError

# table.update_item(
# Key={
# "board_id": board_id,
# "created_at": ts,
# },
# ConditionExpression=Attr('thread_id').eq(thread_id),
# UpdateExpression="SET views = views + :incr",
# ExpressionAttributeValues={
# ":incr": 1
# }
# )

# item = results[0]

# item["mod"] = False
# if item["uid"] == uid:
# item["mod"] = True
# del item["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"],
# "ts": event["queryStringParameters"]["ts"],
# "thread_id": event["pathParameters"]["thread_id"]
# }
# if "uid" in event["queryStringParameters"]:
# params["uid"] = event["queryStringParameters"]["uid"]

# return get_single_thread(**params)
from boto3.dynamodb.conditions import Key, Attr
from datetime import datetime
from utils import JsonPayloadBuilder, table, resp_handler


@resp_handler
def get_single_thread(board_id, ts, thread_id, uid):

results = table.query(KeyConditionExpression=Key(
"board_id").eq(board_id),
ConditionExpression=Attr('thread_id').eq(thread_id))["Items"]
if not results:
raise LookupError

table.update_item(
Key={
"board_id": board_id,
"created_at": ts,
},
ConditionExpression=Attr('thread_id').eq(thread_id),
UpdateExpression="SET views = views + :incr",
ExpressionAttributeValues={
":incr": 1
}
)

item = results[0]

item["mod"] = False
if item["uid"] == uid:
item["mod"] = True
del item["uid"]

body = JsonPayloadBuilder().add_status(
True).add_data(item).add_message('').compile()
return body


def handler(event, context):
pass
params = {
"board_id": event["pathParameters"]["board_id"],
"ts": event["queryStringParameters"]["ts"],
"thread_id": event["pathParameters"]["thread_id"]
}
if "uid" in event["queryStringParameters"]:
params["uid"] = event["queryStringParameters"]["uid"]

return get_single_thread(**params)
Loading