Skip to content

Commit

Permalink
feat: renamed forum database and fixed multiple api (#230)
Browse files Browse the repository at this point in the history
* feat: updated api get schema and renamed database

* fix: fix lambda function

* fix: fixed lambda get single thread and others
  • Loading branch information
JasonNotJson authored Dec 30, 2022
1 parent d7e82e0 commit 6620d99
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 133 deletions.
4 changes: 4 additions & 0 deletions lib/configs/api-gateway/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ export const forumThreadGetRespSchema: apigw.JsonSchema = {
body: {
type: apigw.JsonSchemaType.STRING,
},
views: {
type: apigw.JsonSchemaType.INTEGER,
},
mod: {
type: apigw.JsonSchemaType.BOOLEAN,
},
Expand All @@ -430,6 +433,7 @@ export const forumThreadGetRespSchema: apigw.JsonSchema = {
'thread_id',
'title',
'body',
'views',
'mod',
],
},
Expand Down
10 changes: 5 additions & 5 deletions lib/constructs/persistence/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
);
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/lambda/delete-thread/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def handler(event, context):
pass
26 changes: 0 additions & 26 deletions src/lambda/get-single-thread copy/index.py

This file was deleted.

66 changes: 0 additions & 66 deletions src/lambda/get-single-thread copy/utils.py

This file was deleted.

51 changes: 29 additions & 22 deletions src/lambda/get-single-thread/index.py
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 13 additions & 13 deletions src/lambda/get-threads/index.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions src/lambda/patch-thread/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def handler(event, context):
pass
3 changes: 2 additions & 1 deletion src/lambda/post-thread/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6620d99

Please sign in to comment.