Skip to content

Commit

Permalink
Refactor common API functions
Browse files Browse the repository at this point in the history
  • Loading branch information
amits97 committed Feb 13, 2024
1 parent 2780fe4 commit 7b27fdd
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 452 deletions.
76 changes: 76 additions & 0 deletions api/common/post-comments.js
Original file line number Diff line number Diff line change
@@ -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;
}
50 changes: 50 additions & 0 deletions api/common/post-ratings.js
Original file line number Diff line number Diff line change
@@ -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;
}
91 changes: 8 additions & 83 deletions api/posts-service/get.js
Original file line number Diff line number Diff line change
@@ -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("-");
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
126 changes: 2 additions & 124 deletions api/posts-service/list.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit 7b27fdd

Please sign in to comment.