diff --git a/src/lambda/delete-thread/index.py b/src/lambda/delete-thread/index.py index 82f197750..a5a36dbf6 100644 --- a/src/lambda/delete-thread/index.py +++ b/src/lambda/delete-thread/index.py @@ -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) diff --git a/src/lambda/get-all-threads/index.py b/src/lambda/get-all-threads/index.py index 243533b52..0d6f3f3dd 100644 --- a/src/lambda/get-all-threads/index.py +++ b/src/lambda/get-all-threads/index.py @@ -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() diff --git a/src/lambda/get-board-threads/index.py b/src/lambda/get-board-threads/index.py index bfe54e785..2371ccc49 100644 --- a/src/lambda/get-board-threads/index.py +++ b/src/lambda/get-board-threads/index.py @@ -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) diff --git a/src/lambda/get-group-threads/index.py b/src/lambda/get-group-threads/index.py index e37c2c556..393284132 100644 --- a/src/lambda/get-group-threads/index.py +++ b/src/lambda/get-group-threads/index.py @@ -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) diff --git a/src/lambda/get-single-thread/index.py b/src/lambda/get-single-thread/index.py index a79feb911..a6489c17e 100644 --- a/src/lambda/get-single-thread/index.py +++ b/src/lambda/get-single-thread/index.py @@ -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) diff --git a/src/lambda/patch-thread/index.py b/src/lambda/patch-thread/index.py index 68162b06a..5e103f87b 100644 --- a/src/lambda/patch-thread/index.py +++ b/src/lambda/patch-thread/index.py @@ -1,45 +1,43 @@ -# import json -# from datetime import datetime -# from boto3.dynamodb.conditions import Attr -# from utils import JsonPayloadBuilder -# from utils import resp_handler -# from utils import table - - -# @resp_handler -# def patch_thread(board_id, ts, thread_id, thread): - -# dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' -# table.update_item( -# Key={ -# "board_id": board_id, -# "created_at": ts, -# }, -# ConditionExpression=Attr('thread_id').eq(thread_id), -# UpdateExpression='SET body = :tbody, title = :ttitle, update_at = :ts', -# ExpressionAtrributeValues={ -# ":tbody": [thread['body']], -# ":ttitle": [thread['title']], -# ":ts": dt_now -# }, -# ) - -# body = JsonPayloadBuilder().add_status( -# True).add_data(None).add_message('').compile() -# return body - - -# def handler(event, context): - -# req = json.loads(event['body']) -# params = { -# "board_id": event["pathParameters"]["board_id"], -# "thread_id": event["pathParameters"]["thread_id"], -# "ts": event["queryStringParameters"]["ts"], -# "uid": event['requestContext']['authorizer']['claims']['sub'], -# "thread": req["data"] -# } - -# return patch_thread(**params) +import json +from datetime import datetime +from boto3.dynamodb.conditions import Attr +from utils import JsonPayloadBuilder +from utils import resp_handler +from utils import table + + +@resp_handler +def patch_thread(board_id, ts, thread_id, thread): + + dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' + table.update_item( + Key={ + "board_id": board_id, + "created_at": ts, + }, + ConditionExpression=Attr('thread_id').eq(thread_id), + UpdateExpression='SET body = :tbody, title = :ttitle, update_at = :ts', + ExpressionAtrributeValues={ + ":tbody": [thread['body']], + ":ttitle": [thread['title']], + ":ts": dt_now + }, + ) + + body = JsonPayloadBuilder().add_status( + True).add_data(None).add_message('').compile() + return body + + def handler(event, context): - pass + + req = json.loads(event['body']) + params = { + "board_id": event["pathParameters"]["board_id"], + "thread_id": event["pathParameters"]["thread_id"], + "ts": event["queryStringParameters"]["ts"], + "uid": event['requestContext']['authorizer']['claims']['sub'], + "thread": req["data"] + } + + return patch_thread(**params) diff --git a/src/lambda/post-thread/index.py b/src/lambda/post-thread/index.py index 45c14dd02..b975faa78 100644 --- a/src/lambda/post-thread/index.py +++ b/src/lambda/post-thread/index.py @@ -1,48 +1,47 @@ -# from boto3.dynamodb.conditions import Key -# import json -# from datetime import datetime -# from utils import JsonPayloadBuilder, table, resp_handler, build_thread_id -# import uuid +from boto3.dynamodb.conditions import Key +import json +from datetime import datetime +from utils import JsonPayloadBuilder, table, resp_handler, build_thread_id +import uuid -# @resp_handler -# def post_thread(board_id, thread, uid): +@resp_handler +def post_thread(board_id, thread, uid): -# thread_id = build_thread_id(uid) + thread_id = build_thread_id(uid) -# text = thread["body"] + text = thread["body"] -# dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' + dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' -# thread_item = { -# "board_id": board_id, -# "created_at": dt_now, -# "updated_at": dt_now, -# "title": thread["title"], -# "body": text, -# "uid": uid, -# "thread_id": thread_id, -# "group_id": thread["group_id"], -# "univ_id": thread["univ_id"], -# "view": 0, -# } + thread_item = { + "board_id": board_id, + "created_at": dt_now, + "updated_at": dt_now, + "title": thread["title"], + "body": text, + "uid": uid, + "thread_id": thread_id, + "tag_id": thread["tag_id"], + "group_id": thread["group_id"], + "univ_id": thread["univ_id"], + "view": 0, + } -# table.put_item(Item=thread_item) + table.put_item(Item=thread_item) -# body = JsonPayloadBuilder().add_status( -# True).add_data(None).add_message('').compile() -# return body + body = JsonPayloadBuilder().add_status( + True).add_data(None).add_message('').compile() + return body -# def handler(event, context): +def handler(event, context): -# req = json.loads(event['body']) -# params = { -# "board_id": event["pathParameters"]["board_id"], -# "thread": req["data"], -# "uid": event['requestContext']['authorizer']['claims']['sub'] -# } + req = json.loads(event['body']) + params = { + "board_id": event["pathParameters"]["board_id"], + "thread": req["data"], + "uid": event['requestContext']['authorizer']['claims']['sub'] + } -# return post_thread(**params) -def handler(event, context): - pass + return post_thread(**params)