From 6620d994eed9b91f9acd86b8b6ac9fdaa985f663 Mon Sep 17 00:00:00 2001 From: Jason Park <93040528+Autogod@users.noreply.github.com> Date: Fri, 30 Dec 2022 22:23:39 +0900 Subject: [PATCH] feat: renamed forum database and fixed multiple api (#230) * feat: updated api get schema and renamed database * fix: fix lambda function * fix: fixed lambda get single thread and others --- lib/configs/api-gateway/schema.ts | 4 ++ lib/constructs/persistence/database.ts | 10 ++-- src/lambda/delete-thread/index.py | 2 + src/lambda/get-single-thread copy/index.py | 26 --------- src/lambda/get-single-thread copy/utils.py | 66 ---------------------- src/lambda/get-single-thread/index.py | 51 +++++++++-------- src/lambda/get-threads/index.py | 26 ++++----- src/lambda/patch-thread/index.py | 2 + src/lambda/post-thread/index.py | 3 +- 9 files changed, 57 insertions(+), 133 deletions(-) delete mode 100644 src/lambda/get-single-thread copy/index.py delete mode 100644 src/lambda/get-single-thread copy/utils.py diff --git a/lib/configs/api-gateway/schema.ts b/lib/configs/api-gateway/schema.ts index d0810afc6..2c86ff952 100644 --- a/lib/configs/api-gateway/schema.ts +++ b/lib/configs/api-gateway/schema.ts @@ -416,6 +416,9 @@ export const forumThreadGetRespSchema: apigw.JsonSchema = { body: { type: apigw.JsonSchemaType.STRING, }, + views: { + type: apigw.JsonSchemaType.INTEGER, + }, mod: { type: apigw.JsonSchemaType.BOOLEAN, }, @@ -430,6 +433,7 @@ export const forumThreadGetRespSchema: apigw.JsonSchema = { 'thread_id', 'title', 'body', + 'views', 'mod', ], }, diff --git a/lib/constructs/persistence/database.ts b/lib/constructs/persistence/database.ts index 02f8ca94c..811d9d1c3 100644 --- a/lib/constructs/persistence/database.ts +++ b/lib/constructs/persistence/database.ts @@ -88,15 +88,15 @@ export class DynamoDatabase extends Construct { { partitionKey: { name: 'board_id', - type: dynamodb.AttributeType.NUMBER, + type: dynamodb.AttributeType.STRING, }, billingMode: dynamodb.BillingMode.PROVISIONED, encryption: dynamodb.TableEncryption.DEFAULT, removalPolicy: RemovalPolicy.RETAIN, sortKey: { name: 'created_at', type: dynamodb.AttributeType.STRING }, - tableName: 'forum-thread', - readCapacity: 10, - writeCapacity: 7, + tableName: 'forum-threads', + readCapacity: 15, + writeCapacity: 15, pointInTimeRecovery: true, }, ); @@ -132,7 +132,7 @@ export class DynamoDatabase extends Construct { encryption: dynamodb.TableEncryption.DEFAULT, removalPolicy: RemovalPolicy.RETAIN, sortKey: { name: 'created_at', type: dynamodb.AttributeType.STRING }, - tableName: 'forum-comment', + tableName: 'forum-comments', readCapacity: 10, writeCapacity: 7, pointInTimeRecovery: true, diff --git a/src/lambda/delete-thread/index.py b/src/lambda/delete-thread/index.py index e69de29bb..9055b33b0 100644 --- a/src/lambda/delete-thread/index.py +++ b/src/lambda/delete-thread/index.py @@ -0,0 +1,2 @@ +def handler(event, context): + pass diff --git a/src/lambda/get-single-thread copy/index.py b/src/lambda/get-single-thread copy/index.py deleted file mode 100644 index 3520c2adb..000000000 --- a/src/lambda/get-single-thread copy/index.py +++ /dev/null @@ -1,26 +0,0 @@ -from boto3.dynamodb.conditions import Key -from datetime import datetime -from utils import JsonPayloadBuilder, table, resp_handler - - -@resp_handler -def get_thread(thread_id): - - results = table.query(KeyConditionExpression=Key( - "thread_id").eq(thread_id))["Items"] - if not results: - raise LookupError - - item = results[0] - - body = JsonPayloadBuilder().add_status( - True).add_data(item).add_message('').compile() - return body - - -def handler(event, context): - params = { - "thread_id": event["pathParameters"]["thread_id"] - } - - return get_thread(**params) diff --git a/src/lambda/get-single-thread copy/utils.py b/src/lambda/get-single-thread copy/utils.py deleted file mode 100644 index 3ccbaf2f5..000000000 --- a/src/lambda/get-single-thread copy/utils.py +++ /dev/null @@ -1,66 +0,0 @@ -# import boto3 -# import json -# import logging -# import os -# from decimal import Decimal - -# db = boto3.resource("dynamodb", region_name="ap-northeast-1") -# table = db.Table(os.getenv('TABLE_NAME')) - - -# class DecimalEncoder(json.JSONEncoder): -# def default(self, obj): -# if isinstance(obj, Decimal): -# return float(obj) -# return json.JSONEncoder.default(self, obj) - - -# class JsonPayloadBuilder: -# payload = {} - -# def add_status(self, success): -# self.payload['success'] = success -# return self - -# def add_data(self, data): -# self.payload['data'] = data -# return self - -# def add_message(self, msg): -# self.payload['message'] = msg -# return self - -# def compile(self): -# return json.dumps(self.payload, cls=DecimalEncoder, ensure_ascii=False).encode('utf8') - - -# def api_response(code, body): -# return { -# "isBase64Encoded": False, -# "statusCode": code, -# 'headers': { -# "Access-Control-Allow-Origin": '*', -# "Content-Type": "application/json", -# "Referrer-Policy": "origin" -# }, -# "multiValueHeaders": {"Access-Control-Allow-Methods": ["POST", "OPTIONS", "GET", "PATCH", "DELETE"]}, -# "body": body -# } - - -# def resp_handler(func): -# def handle(*args, **kwargs): -# try: -# resp = func(*args, **kwargs) -# return api_response(200, resp) -# except LookupError: -# resp = JsonPayloadBuilder().add_status(False).add_data(None) \ -# .add_message("Not found").compile() -# return api_response(404, resp) -# except Exception as e: -# logging.error(str(e)) -# resp = JsonPayloadBuilder().add_status(False).add_data(None) \ -# .add_message("Internal error, please contact bugs@wasedatime.com.").compile() -# return api_response(500, resp) - -# return handle diff --git a/src/lambda/get-single-thread/index.py b/src/lambda/get-single-thread/index.py index 07a7c4561..b058830fb 100644 --- a/src/lambda/get-single-thread/index.py +++ b/src/lambda/get-single-thread/index.py @@ -1,29 +1,36 @@ -# from boto3.dynamodb.conditions import Key -# from datetime import datetime -# from utils import JsonPayloadBuilder, table, resp_handler +from boto3.dynamodb.conditions import Key +from datetime import datetime +from utils import JsonPayloadBuilder, table, resp_handler -# @resp_handler -# def get_thread(thread_id): -# now = datetime.now() +@resp_handler +def get_single_thread(thread_id): -# items = table.query( -# KeyConditionExpression='sort_key <= :sk', -# ExpressionAttributeValues={ -# ':sk': now.strftime('%Y-%m-%d %H:%M:%S') -# }, -# ScanIndexForward=False, -# Limit=50 -# ) + results = table.query(KeyConditionExpression=Key( + "thread_id").eq(thread_id))["Items"] + if not results: + raise LookupError -# body = JsonPayloadBuilder().add_status( -# True).add_data(items).add_message('').compile() -# return body + table.update_item( + key={ + "thread_id": thread_id + }, + UpdateExpression="SET views = views + :incr", + ExpressionAttributeValues={ + ":incr": 1 + } + ) + item = results[0] -# def handler(event, context): -# params = { -# "thread_id": event["queryStringParameters"]["id"] -# } + body = JsonPayloadBuilder().add_status( + True).add_data(item).add_message('').compile() + return body -# return get_thread(**params) + +def handler(event, context): + params = { + "thread_id": event["pathParameters"]["thread_id"] + } + + return get_single_thread(**params) diff --git a/src/lambda/get-threads/index.py b/src/lambda/get-threads/index.py index 14e4b0e8b..6922b24b7 100644 --- a/src/lambda/get-threads/index.py +++ b/src/lambda/get-threads/index.py @@ -1,20 +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_threads(): +@resp_handler +def get_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): +def handler(event, context): -# return get_threads() + return get_threads() diff --git a/src/lambda/patch-thread/index.py b/src/lambda/patch-thread/index.py index e69de29bb..9055b33b0 100644 --- a/src/lambda/patch-thread/index.py +++ b/src/lambda/patch-thread/index.py @@ -0,0 +1,2 @@ +def handler(event, context): + pass diff --git a/src/lambda/post-thread/index.py b/src/lambda/post-thread/index.py index b15131fab..693bc1d80 100644 --- a/src/lambda/post-thread/index.py +++ b/src/lambda/post-thread/index.py @@ -23,7 +23,8 @@ def post_thread(board_id, thread, uid): "uid": uid, "thread_id": thread_id, "group_id": thread["group_id"], - "univ_id": thread["univ_id"] + "univ_id": thread["univ_id"], + "view": 0, } table.put_item(thread_item)