Skip to content

Commit

Permalink
fix comment/replies support
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 18, 2024
1 parent e9b8c1c commit 32e0c55
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
36 changes: 36 additions & 0 deletions src/scripts/seed/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,30 @@ async function main() {
`[SEED] [assetComments] inserted ${newAssetCommentsReplies.length} rows\n`
)

const newAssetCommentsRepliesReplies = await db
.insert(assetComments)
.values([
{
commentedById: newUsers[0].id,
comment: "test comment reply reply",
parentCommentId: newAssetCommentsReplies[0].id,
},
{
commentedById: newUsers[1].id,
comment: "test comment reply reply 2",
parentCommentId: newAssetCommentsReplies[1].id,
},
{
commentedById: newUsers[0].id,
comment: "test comment reply reply 3",
parentCommentId: newAssetCommentsReplies[2].id,
},
])
.returning()
console.log(
`[SEED] [assetComments] inserted ${newAssetCommentsRepliesReplies.length} rows\n`
)

console.log("[SEED] [assetCommentsLikes] Seeding asset comments likes...")
const newAssetCommentsLikes = await db
.insert(assetCommentsLikes)
Expand Down Expand Up @@ -471,6 +495,18 @@ async function main() {
commentId: newAssetCommentsReplies[2].id,
likedById: newUsers[0].id,
},
{
commentId: newAssetCommentsRepliesReplies[0].id,
likedById: newUsers[1].id,
},
{
commentId: newAssetCommentsRepliesReplies[1].id,
likedById: newUsers[0].id,
},
{
commentId: newAssetCommentsRepliesReplies[2].id,
likedById: newUsers[1].id,
},
])
.returning()
console.log(
Expand Down
7 changes: 6 additions & 1 deletion src/v2/routes/asset/get-asset-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const getAssetCommentsResponseSchema = z.object({
createdAt: true,
})
.extend({
hasReplies: z.boolean(),
likes: z.number(),
})
),
Expand Down Expand Up @@ -86,6 +87,7 @@ export const ViewAssetCommentsRoute = (handler: AppHandler) => {
commentedById: assetComments.commentedById,
comment: assetComments.comment,
createdAt: assetComments.createdAt,
hasReplies: sql`EXISTS (SELECT 1 FROM assetComments AS ac WHERE ac.parent_comment_id = ${assetComments.id})`,
likes: sql`COUNT(${assetCommentsLikes.commentId})`,
})
.from(assetComments)
Expand All @@ -102,7 +104,10 @@ export const ViewAssetCommentsRoute = (handler: AppHandler) => {
return ctx.json(
{
success: true,
comments,
comments: comments.map((c) => ({
...c,
hasReplies: !!c.hasReplies,
})),
},
200
)
Expand Down
4 changes: 2 additions & 2 deletions src/v2/routes/asset/get-comment-replies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const getCommentRepliesResponseSchema = z.object({
const getCommentsRepliesRoute = createRoute({
path: "/comment/{id}/replies",
method: "get",
summary: "Get a comment's replies.",
summary: "Get a comment's replies",
description: "Get a comment's replies.",
tags: ["Asset"],
request: {
Expand Down Expand Up @@ -87,7 +87,7 @@ export const GetCommentsRepliesRoute = (handler: AppHandler) => {
commentedById: assetComments.commentedById,
comment: assetComments.comment,
createdAt: assetComments.createdAt,
hasReplies: sql<number>`EXISTS (SELECT 1 FROM ${assetComments} WHERE ${assetComments.parentCommentId} = ${assetComments.id})`,
hasReplies: sql`EXISTS (SELECT 1 FROM assetComments AS ac WHERE ac.parent_comment_id = ${assetComments.id})`,
likes: sql`COUNT(${assetCommentsLikes.commentId})`,
})
.from(assetComments)
Expand Down

0 comments on commit 32e0c55

Please sign in to comment.