Skip to content

Commit

Permalink
feat: adding functionalities for user thread (#324)
Browse files Browse the repository at this point in the history
* feat: adding functionalities for user thread

* chore: typo fix

* feat: changing schema model to an empty model
  • Loading branch information
JasonNotJson authored Sep 18, 2023
1 parent a97abc1 commit 171dfec
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 81 deletions.
30 changes: 23 additions & 7 deletions lib/constructs/business/rest-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ export class ForumThreadsApiService extends RestApiService {
const root = scope.apiEndpoint.root.addResource('forum');
const boardResource = root.addResource('{board_id}');
const threadResource = boardResource.addResource('{thread_id}');
const userResource = root.addResource('{uid}');

const optionsForumHome = root.addCorsPreflight({
allowOrigins: allowOrigins,
Expand Down Expand Up @@ -753,6 +754,18 @@ export class ForumThreadsApiService extends RestApiService {
],
});

const optionsUserThreads = userResource.addCorsPreflight({
allowOrigins: allowOrigins,
allowHeaders: allowHeaders,
allowMethods: [
apigw2.HttpMethod.GET,
apigw2.HttpMethod.POST,
apigw2.HttpMethod.PATCH,
apigw2.HttpMethod.DELETE,
apigw2.HttpMethod.OPTIONS,
],
});

const getRespModel = scope.apiEndpoint.addModel('threads-get-resp-model', {
schema: forumThreadGetRespSchema,
contentType: 'application/json',
Expand Down Expand Up @@ -786,8 +799,8 @@ export class ForumThreadsApiService extends RestApiService {
forumThreadsFunctions.getAllFunction,
{ proxy: true },
);
const getBoardIntegration = new apigw.LambdaIntegration(
forumThreadsFunctions.getBoardFunction,
const getUserIntegration = new apigw.LambdaIntegration(
forumThreadsFunctions.getUserFunction,
{ proxy: true },
);
const getThreadIntegration = new apigw.LambdaIntegration(
Expand Down Expand Up @@ -823,15 +836,15 @@ export class ForumThreadsApiService extends RestApiService {
},
);

const getBoardForumThreads = boardResource.addMethod(
const getUserForumThreads = userResource.addMethod(
apigw2.HttpMethod.GET,
getBoardIntegration,
getUserIntegration,
{
operationName: 'GetBoardThreads',
operationName: 'GetUserThreads',
methodResponses: [
{
statusCode: '200',
responseModels: { ['application/json']: getRespModel },
responseModels: { ['application/json']: apigw.Model.EMPTY_MODEL },
responseParameters: lambdaRespParams,
},
],
Expand Down Expand Up @@ -910,8 +923,11 @@ export class ForumThreadsApiService extends RestApiService {
[apigw2.HttpMethod.GET]: getAllForumThreads,
[apigw2.HttpMethod.OPTIONS]: optionsForumHome,
},
'/forum/{uid}': {
[apigw2.HttpMethod.GET]: getUserForumThreads,
[apigw2.HttpMethod.OPTIONS]: optionsUserThreads,
},
'/forum/{board_id}': {
[apigw2.HttpMethod.GET]: getBoardForumThreads,
[apigw2.HttpMethod.POST]: postForumThreads,
[apigw2.HttpMethod.OPTIONS]: optionsForumBoards,
},
Expand Down
12 changes: 6 additions & 6 deletions lib/constructs/common/lambda-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ export class SyllabusUpdateFunction extends Construct {

export class ForumThreadFunctions extends Construct {
readonly getAllFunction: lambda.Function;
readonly getBoardFunction: lambda.Function;
readonly getUserFunction: lambda.Function;
readonly getSingleFunction: lambda.Function;
readonly postFunction: lambda.Function;
readonly patchFunction: lambda.Function;
Expand Down Expand Up @@ -559,13 +559,13 @@ export class ForumThreadFunctions extends Construct {
},
);

this.getBoardFunction = new lambda_py.PythonFunction(
this.getUserFunction = new lambda_py.PythonFunction(
this,
'get-board-threads',
'get-user-threads',
{
entry: 'src/lambda/get-board-threads',
description: 'Get forum threads from the database.',
functionName: 'get-board-threads',
entry: 'src/lambda/get-user-threads',
description: "Get user's threads from the database.",
functionName: 'get-user-threads',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 128,
role: dynamoDBReadRole,
Expand Down
15 changes: 7 additions & 8 deletions lib/constructs/persistence/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ export class DynamoDatabase extends Construct {
},
billingMode: dynamodb.BillingMode.PROVISIONED,
encryption: dynamodb.TableEncryption.DEFAULT,
removalPolicy: RemovalPolicy.DESTROY,
removalPolicy: RemovalPolicy.RETAIN,
sortKey: { name: 'thread_id', type: dynamodb.AttributeType.STRING },
tableName: 'forum-threads',
readCapacity: 15,
writeCapacity: 15,
pointInTimeRecovery: true,
},
);
this.tables[Collection.THREAD].addGlobalSecondaryIndex({
indexName: 'UidbyThreadIDIndex',
partitionKey: { name: 'uid', type: dynamodb.AttributeType.STRING },
sortKey: { name: 'thread_id', type: dynamodb.AttributeType.STRING },
projectionType: dynamodb.ProjectionType.ALL,
});

// this.tables[Collection.THREAD].addLocalSecondaryIndex({
// indexName: 'GroupIndex',
Expand All @@ -113,13 +119,6 @@ export class DynamoDatabase extends Construct {
// projectionType: dynamodb.ProjectionType.ALL,
// });

// this.tables[Collection.THREAD].addGlobalSecondaryIndex({
// indexName: "UidbyCreated_at",
// partitionKey: { name: "uid", type: dynamodb.AttributeType.STRING },
// sortKey: { name: "created_at", type: dynamodb.AttributeType.NUMBER },
// projectionType: dynamodb.ProjectionType.ALL,
// });

this.tables[Collection.COMMENT] = new dynamodb.Table(
this,
'dynamodb-comment-table',
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/get-all-threads/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_all_threads(uid, index, num, school, tags, board_id):
response = table.query(KeyConditionExpression=Key(
"board_id").eq(board_id), ScanIndexForward=False)
else:
response = table.scan()
response = table.scan(ConsistentRead=False)

items = response['Items']

Expand Down
59 changes: 0 additions & 59 deletions src/lambda/get-board-threads/index.py

This file was deleted.

42 changes: 42 additions & 0 deletions src/lambda/get-user-threads/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from boto3.dynamodb.conditions import Key, Attr
import boto3
from datetime import datetime
from utils import JsonPayloadBuilder, table, resp_handler


@resp_handler
def get_user_threads(uid):

# Query the GSI
response = table.query(
IndexName='UidbyThreadIDIndex', # Replace with your actual GSI name
KeyConditionExpression=Key('uid').eq(uid),
FilterExpression=Attr('new_comment').eq(True),
ScanIndexForward=False # Sorting by thread_id in descending order
)

results = response['Items']

# Count the threads with new comments
new_comment_count = len(results)

# Collect the thread_ids
thread_ids = [item['thread_id'] for item in results]

# Determine the response data based on new_comment_count
response_data = True if new_comment_count > 1 else {
'thread_ids': thread_ids, 'new_comment_count': new_comment_count}

body = JsonPayloadBuilder().add_status(True)\
.add_data(response_data)\
.add_message('Fetched successfully').compile()

return body


def handler(event, context):
uid = event['queryStringParameters'].get('uid', '')
# index = event['queryStringParameters'].get('index', '0')
# num = event['queryStringParameters'].get('num', '10')

return get_user_threads(uid)
File renamed without changes.

0 comments on commit 171dfec

Please sign in to comment.