-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
145 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.