From 7b27fdde487cb16e3836e4ccc6e03b6628c6f205 Mon Sep 17 00:00:00 2001 From: Amit S Namboothiry Date: Tue, 13 Feb 2024 23:22:02 +0530 Subject: [PATCH] Refactor common API functions --- api/common/post-comments.js | 76 +++++++++++++++++++ api/common/post-ratings.js | 50 +++++++++++++ api/posts-service/get.js | 91 ++--------------------- api/posts-service/list.js | 126 +------------------------------- api/posts-service/random.js | 28 ++----- api/posts-service/top-posts.js | 50 +------------ api/posts-service/user-posts.js | 126 +------------------------------- api/rating-service/top-rated.js | 50 +------------ 8 files changed, 145 insertions(+), 452 deletions(-) create mode 100644 api/common/post-comments.js create mode 100644 api/common/post-ratings.js diff --git a/api/common/post-comments.js b/api/common/post-comments.js new file mode 100644 index 00000000..e3ff0c77 --- /dev/null +++ b/api/common/post-comments.js @@ -0,0 +1,76 @@ +import * as dynamoDbLib from "../libs/dynamodb-lib"; + +export async function countReplyComments(commentId) { + const params = { + TableName: "NaadanChordsComments", + ScanFilter: { + commentId: { + ComparisonOperator: "EQ", + AttributeValueList: [commentId], + }, + }, + }; + const comment = await dynamoDbLib.call("scan", params); + if (comment.Items && comment.Items.length === 1) { + let count = 1; + let commentItem = comment.Items[0]; + let replies = commentItem.replies || []; + for (let i = 0; i < replies.length; i++) { + const replyCommentsCount = await countReplyComments(replies[i]); + count += replyCommentsCount; + } + return count; + } + return 0; +} + +export async function appendCommentsCount(result) { + let items = result.Items; + let filterExpression = ""; + let expressionAttributeValues = {}; + + for (let i = 0; i < items.length; i++) { + let postId = items[i].postId; + if (filterExpression) { + filterExpression += ` OR contains(postId, :postId${i})`; + } else { + filterExpression = `contains(postId, :postId${i})`; + } + expressionAttributeValues[`:postId${i}`] = postId; + } + + let params = { + TableName: "NaadanChordsComments", + FilterExpression: filterExpression, + ExpressionAttributeValues: expressionAttributeValues, + }; + + try { + let commentsResult = await dynamoDbLib.call("scan", params); + let comments = commentsResult.Items; + let commentsObject = {}; + + for (let i = 0; i < comments.length; i++) { + let commentItem = comments[i]; + let replies = commentItem.replies || []; + commentsObject[commentItem.postId] = + (commentsObject[commentItem.postId] || 0) + 1; + for (let j = 0; j < replies.length; j++) { + const replyCommentsCount = await countReplyComments(replies[j]); + commentsObject[commentItem.postId] += replyCommentsCount; + } + } + + for (let i = 0; i < items.length; i++) { + if (commentsObject.hasOwnProperty(items[i].postId)) { + items[i].commentsCount = commentsObject[items[i].postId]; + } + } + + result.Items = items; + } catch (e) { + result.commentsError = e; + } + + return result; +} diff --git a/api/common/post-ratings.js b/api/common/post-ratings.js new file mode 100644 index 00000000..dad5ee64 --- /dev/null +++ b/api/common/post-ratings.js @@ -0,0 +1,50 @@ +import * as dynamoDbLib from "../libs/dynamodb-lib"; + +export async function appendRatings(result) { + let items = result.Items; + let filterExpression = ""; + let expressionAttributeValues = {}; + + for (let i = 0; i < items.length; i++) { + let postId = items[i].postId; + if (filterExpression) { + filterExpression += ` OR contains(postId, :postId${i})`; + } else { + filterExpression = `contains(postId, :postId${i})`; + } + expressionAttributeValues[`:postId${i}`] = postId; + } + + let params = { + TableName: "NaadanChordsRatings", + FilterExpression: filterExpression, + ExpressionAttributeValues: expressionAttributeValues, + }; + + try { + let ratingsResult = await dynamoDbLib.call("scan", params); + let ratings = ratingsResult.Items; + let ratingsObject = {}; + + for (let i = 0; i < ratings.length; i++) { + let ratingItem = ratings[i]; + ratingsObject[ratingItem.postId] = { + rating: ratingItem.rating, + ratingCount: ratingItem.count, + }; + } + + for (let i = 0; i < items.length; i++) { + if (ratingsObject.hasOwnProperty(items[i].postId)) { + items[i].rating = ratingsObject[items[i].postId].rating; + items[i].ratingCount = ratingsObject[items[i].postId].ratingCount; + } + } + + result.Items = items; + } catch (e) { + result.ratingsError = e; + } + + return result; +} diff --git a/api/posts-service/get.js b/api/posts-service/get.js index 2f431363..dfbb6c22 100644 --- a/api/posts-service/get.js +++ b/api/posts-service/get.js @@ -1,84 +1,8 @@ import * as dynamoDbLib from "../libs/dynamodb-lib"; import * as userNameLib from "../libs/username-lib"; import { success, failure } from "../libs/response-lib"; - -async function countReplyComments(commentId) { - const params = { - TableName: "NaadanChordsComments", - ScanFilter: { - commentId: { - ComparisonOperator: "EQ", - AttributeValueList: [commentId], - }, - }, - }; - const comment = await dynamoDbLib.call("scan", params); - if (comment.Items && comment.Items.length === 1) { - let count = 1; - let commentItem = comment.Items[0]; - let replies = commentItem.replies || []; - for (let i = 0; i < replies.length; i++) { - const replyCommentsCount = await countReplyComments(replies[i]); - count += replyCommentsCount; - } - return count; - } - return 0; -} - -async function appendCommentsCount(item) { - let filterExpression = "contains(postId, :postId)"; - let expressionAttributeValues = {}; - expressionAttributeValues[`:postId`] = item.postId; - - let params = { - TableName: "NaadanChordsComments", - FilterExpression: filterExpression, - ExpressionAttributeValues: expressionAttributeValues, - }; - - try { - let commentsResult = await dynamoDbLib.call("scan", params); - let comments = commentsResult.Items; - let commentsCount = 0; - - for (let i = 0; i < comments.length; i++) { - let commentItem = comments[i]; - let replies = commentItem.replies || []; - commentsCount += 1; - for (let j = 0; j < replies.length; j++) { - const replyCommentsCount = await countReplyComments(replies[j]); - commentsCount += replyCommentsCount; - } - } - item.commentsCount = commentsCount; - } catch (e) { - item.commentsError = e; - } - - return item; -} - -async function appendRating(item) { - const params = { - TableName: "NaadanChordsRatings", - Key: { - postId: item.postId, - }, - }; - - try { - let ratingResult = await dynamoDbLib.call("get", params); - if (ratingResult && ratingResult.hasOwnProperty("Item")) { - item.rating = ratingResult.Item.rating; - item.ratingCount = ratingResult.Item.count; - } - } catch (e) { - item.ratingError = e; - } - - return item; -} +import { appendRatings } from "../common/post-ratings"; +import { appendCommentsCount } from "../common/post-comments"; function retryLoop(postId) { let keywords = postId.split("-"); @@ -119,8 +43,9 @@ async function retryGet(postId) { //Do not expose userId delete finalResult.userId; - finalResult = await appendRating(finalResult); - return success(finalResult); + finalResult = await appendRatings({ Items: [finalResult] }); + finalResult = await appendCommentsCount(finalResult); + return success(finalResult.Items[0]); } else { return retryLoop(postId); } @@ -155,9 +80,9 @@ export async function main(event, context) { //Do not expose userId delete result.Item.userId; - let finalResult = await appendRating(result.Item); - finalResult = await appendCommentsCount(result.Item); - return success(finalResult); + let finalResult = await appendRatings({ Items: [result.Item] }); + finalResult = await appendCommentsCount(finalResult); + return success(finalResult.Items[0]); } else { return retryGet(event.pathParameters.id); } diff --git a/api/posts-service/list.js b/api/posts-service/list.js index a1230547..3b6ff83c 100644 --- a/api/posts-service/list.js +++ b/api/posts-service/list.js @@ -1,130 +1,8 @@ import * as dynamoDbLib from "../libs/dynamodb-lib"; import * as userNameLib from "../libs/username-lib"; import * as searchFilterLib from "../libs/searchfilter-lib"; - -async function countReplyComments(commentId) { - const params = { - TableName: "NaadanChordsComments", - ScanFilter: { - commentId: { - ComparisonOperator: "EQ", - AttributeValueList: [commentId], - }, - }, - }; - const comment = await dynamoDbLib.call("scan", params); - if (comment.Items && comment.Items.length === 1) { - let count = 1; - let commentItem = comment.Items[0]; - let replies = commentItem.replies || []; - for (let i = 0; i < replies.length; i++) { - const replyCommentsCount = await countReplyComments(replies[i]); - count += replyCommentsCount; - } - return count; - } - return 0; -} - -async function appendCommentsCount(result) { - let items = result.Items; - let filterExpression = ""; - let expressionAttributeValues = {}; - - for (let i = 0; i < items.length; i++) { - let postId = items[i].postId; - if (filterExpression) { - filterExpression += ` OR contains(postId, :postId${i})`; - } else { - filterExpression = `contains(postId, :postId${i})`; - } - expressionAttributeValues[`:postId${i}`] = postId; - } - - let params = { - TableName: "NaadanChordsComments", - FilterExpression: filterExpression, - ExpressionAttributeValues: expressionAttributeValues, - }; - - try { - let commentsResult = await dynamoDbLib.call("scan", params); - let comments = commentsResult.Items; - let commentsObject = {}; - - for (let i = 0; i < comments.length; i++) { - let commentItem = comments[i]; - let replies = commentItem.replies || []; - commentsObject[commentItem.postId] = - (commentsObject[commentItem.postId] || 0) + 1; - for (let j = 0; j < replies.length; j++) { - const replyCommentsCount = await countReplyComments(replies[j]); - commentsObject[commentItem.postId] += replyCommentsCount; - } - } - - for (let i = 0; i < items.length; i++) { - if (commentsObject.hasOwnProperty(items[i].postId)) { - items[i].commentsCount = commentsObject[items[i].postId]; - } - } - - result.Items = items; - } catch (e) { - result.commentsError = e; - } - - return result; -} - -async function appendRatings(result) { - let items = result.Items; - let filterExpression = ""; - let expressionAttributeValues = {}; - - for (let i = 0; i < items.length; i++) { - let postId = items[i].postId; - if (filterExpression) { - filterExpression += ` OR contains(postId, :postId${i})`; - } else { - filterExpression = `contains(postId, :postId${i})`; - } - expressionAttributeValues[`:postId${i}`] = postId; - } - - let params = { - TableName: "NaadanChordsRatings", - FilterExpression: filterExpression, - ExpressionAttributeValues: expressionAttributeValues, - }; - - try { - let ratingsResult = await dynamoDbLib.call("scan", params); - let ratings = ratingsResult.Items; - let ratingsObject = {}; - - for (let i = 0; i < ratings.length; i++) { - let ratingItem = ratings[i]; - ratingsObject[ratingItem.postId] = { - rating: ratingItem.rating, - ratingCount: ratingItem.count, - }; - } - - for (let i = 0; i < items.length; i++) { - if (ratingsObject.hasOwnProperty(items[i].postId)) { - items[i].rating = ratingsObject[items[i].postId].rating; - items[i].ratingCount = ratingsObject[items[i].postId].ratingCount; - } - } - - result.Items = items; - } catch (e) { - result.ratingsError = e; - } - - return result; -} +import { appendRatings } from "../common/post-ratings"; +import { appendCommentsCount } from "../common/post-comments"; export async function main(event, context, callback) { var lastEvaluatedKey; diff --git a/api/posts-service/random.js b/api/posts-service/random.js index d6afa4c0..d3b1b635 100644 --- a/api/posts-service/random.js +++ b/api/posts-service/random.js @@ -1,27 +1,8 @@ import * as dynamoDbLib from "../libs/dynamodb-lib"; import * as userNameLib from "../libs/username-lib"; import { success, failure } from "../libs/response-lib"; - -async function appendRating(item) { - const params = { - TableName: "NaadanChordsRatings", - Key: { - postId: item.postId, - }, - }; - - try { - let ratingResult = await dynamoDbLib.call("get", params); - if (ratingResult && ratingResult.hasOwnProperty("Item")) { - item.rating = ratingResult.Item.rating; - item.ratingCount = ratingResult.Item.count; - } - } catch (e) { - item.ratingError = e; - } - - return item; -} +import { appendRatings } from "../common/post-ratings"; +import { appendCommentsCount } from "../common/post-comments"; async function getItemCount() { const itemCountParams = { @@ -77,9 +58,10 @@ export async function main(event, context) { delete result.userId; //Append rating - let finalResult = await appendRating(result); + let finalResult = await appendRatings({ Items: [result] }); + finalResult = await appendCommentsCount(finalResult); - return success(finalResult); + return success(finalResult.Items[0]); } catch (e) { return failure({ status: false, error: e }); } diff --git a/api/posts-service/top-posts.js b/api/posts-service/top-posts.js index 8e5b670b..7f8a2582 100644 --- a/api/posts-service/top-posts.js +++ b/api/posts-service/top-posts.js @@ -1,55 +1,7 @@ import * as dynamoDbLib from "../libs/dynamodb-lib"; import * as userNameLib from "../libs/username-lib"; import { success, failure } from "../libs/response-lib"; - -async function appendRatings(result) { - let items = result.Items; - let filterExpression = ""; - let expressionAttributeValues = {}; - - for (let i = 0; i < items.length; i++) { - let postId = items[i].postId; - if (filterExpression) { - filterExpression += ` OR contains(postId, :postId${i})`; - } else { - filterExpression = `contains(postId, :postId${i})`; - } - expressionAttributeValues[`:postId${i}`] = postId; - } - - let params = { - TableName: "NaadanChordsRatings", - FilterExpression: filterExpression, - ExpressionAttributeValues: expressionAttributeValues, - }; - - try { - let ratingsResult = await dynamoDbLib.call("scan", params); - let ratings = ratingsResult.Items; - let ratingsObject = {}; - - for (let i = 0; i < ratings.length; i++) { - let ratingItem = ratings[i]; - ratingsObject[ratingItem.postId] = { - rating: ratingItem.rating, - ratingCount: ratingItem.count, - }; - } - - for (let i = 0; i < items.length; i++) { - if (ratingsObject.hasOwnProperty(items[i].postId)) { - items[i].rating = ratingsObject[items[i].postId].rating; - items[i].ratingCount = ratingsObject[items[i].postId].ratingCount; - } - } - - result.Items = items; - } catch (e) { - result.ratingsError = e; - } - - return result; -} +import { appendRatings } from "../common/post-ratings"; export async function main() { let params = { diff --git a/api/posts-service/user-posts.js b/api/posts-service/user-posts.js index 7298aba1..aae56efe 100644 --- a/api/posts-service/user-posts.js +++ b/api/posts-service/user-posts.js @@ -1,130 +1,8 @@ import * as dynamoDbLib from "../libs/dynamodb-lib"; import * as userNameLib from "../libs/username-lib"; import * as searchFilterLib from "../libs/searchfilter-lib"; - -async function countReplyComments(commentId) { - const params = { - TableName: "NaadanChordsComments", - ScanFilter: { - commentId: { - ComparisonOperator: "EQ", - AttributeValueList: [commentId], - }, - }, - }; - const comment = await dynamoDbLib.call("scan", params); - if (comment.Items && comment.Items.length === 1) { - let count = 1; - let commentItem = comment.Items[0]; - let replies = commentItem.replies || []; - for (let i = 0; i < replies.length; i++) { - const replyCommentsCount = await countReplyComments(replies[i]); - count += replyCommentsCount; - } - return count; - } - return 0; -} - -async function appendCommentsCount(result) { - let items = result.Items; - let filterExpression = ""; - let expressionAttributeValues = {}; - - for (let i = 0; i < items.length; i++) { - let postId = items[i].postId; - if (filterExpression) { - filterExpression += ` OR contains(postId, :postId${i})`; - } else { - filterExpression = `contains(postId, :postId${i})`; - } - expressionAttributeValues[`:postId${i}`] = postId; - } - - let params = { - TableName: "NaadanChordsComments", - FilterExpression: filterExpression, - ExpressionAttributeValues: expressionAttributeValues, - }; - - try { - let commentsResult = await dynamoDbLib.call("scan", params); - let comments = commentsResult.Items; - let commentsObject = {}; - - for (let i = 0; i < comments.length; i++) { - let commentItem = comments[i]; - let replies = commentItem.replies || []; - commentsObject[commentItem.postId] = - (commentsObject[commentItem.postId] || 0) + 1; - for (let j = 0; j < replies.length; j++) { - const replyCommentsCount = await countReplyComments(replies[j]); - commentsObject[commentItem.postId] += replyCommentsCount; - } - } - - for (let i = 0; i < items.length; i++) { - if (commentsObject.hasOwnProperty(items[i].postId)) { - items[i].commentsCount = commentsObject[items[i].postId]; - } - } - - result.Items = items; - } catch (e) { - result.commentsError = e; - } - - return result; -} - -async function appendRatings(result) { - let items = result.Items; - let filterExpression = ""; - let expressionAttributeValues = {}; - - for (let i = 0; i < items.length; i++) { - let postId = items[i].postId; - if (filterExpression) { - filterExpression += ` OR contains(postId, :postId${i})`; - } else { - filterExpression = `contains(postId, :postId${i})`; - } - expressionAttributeValues[`:postId${i}`] = postId; - } - - let params = { - TableName: "NaadanChordsRatings", - FilterExpression: filterExpression, - ExpressionAttributeValues: expressionAttributeValues, - }; - - try { - let ratingsResult = await dynamoDbLib.call("scan", params); - let ratings = ratingsResult.Items; - let ratingsObject = {}; - - for (let i = 0; i < ratings.length; i++) { - let ratingItem = ratings[i]; - ratingsObject[ratingItem.postId] = { - rating: ratingItem.rating, - ratingCount: ratingItem.count, - }; - } - - for (let i = 0; i < items.length; i++) { - if (ratingsObject.hasOwnProperty(items[i].postId)) { - items[i].rating = ratingsObject[items[i].postId].rating; - items[i].ratingCount = ratingsObject[items[i].postId].ratingCount; - } - } - - result.Items = items; - } catch (e) { - result.ratingsError = e; - } - - return result; -} +import { appendRatings } from "../common/post-ratings"; +import { appendCommentsCount } from "../common/post-comments"; export async function main(event, context, callback) { let dynamoDbQueryType = "query"; diff --git a/api/rating-service/top-rated.js b/api/rating-service/top-rated.js index 84cb3432..9b9d8ffc 100644 --- a/api/rating-service/top-rated.js +++ b/api/rating-service/top-rated.js @@ -1,55 +1,7 @@ import * as dynamoDbLib from "../libs/dynamodb-lib"; import * as userNameLib from "../libs/username-lib"; import { success, failure } from "../libs/response-lib"; - -async function appendRatings(result) { - let items = result.Items; - let filterExpression = ""; - let expressionAttributeValues = {}; - - for (let i = 0; i < items.length; i++) { - let postId = items[i].postId; - if (filterExpression) { - filterExpression += ` OR contains(postId, :postId${i})`; - } else { - filterExpression = `contains(postId, :postId${i})`; - } - expressionAttributeValues[`:postId${i}`] = postId; - } - - let params = { - TableName: "NaadanChordsRatings", - FilterExpression: filterExpression, - ExpressionAttributeValues: expressionAttributeValues, - }; - - try { - let ratingsResult = await dynamoDbLib.call("scan", params); - let ratings = ratingsResult.Items; - let ratingsObject = {}; - - for (let i = 0; i < ratings.length; i++) { - let ratingItem = ratings[i]; - ratingsObject[ratingItem.postId] = { - rating: ratingItem.rating, - ratingCount: ratingItem.count, - }; - } - - for (let i = 0; i < items.length; i++) { - if (ratingsObject.hasOwnProperty(items[i].postId)) { - items[i].rating = ratingsObject[items[i].postId].rating; - items[i].ratingCount = ratingsObject[items[i].postId].ratingCount; - } - } - - result.Items = items; - } catch (e) { - result.ratingsError = e; - } - - return result; -} +import { appendRatings } from "../common/post-ratings"; export async function main() { let params = {