Skip to content

Commit

Permalink
just one more infinite nesting bro trust me bro
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 235cc18 commit e9b8c1c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
115 changes: 115 additions & 0 deletions src/v2/routes/asset/get-comment-replies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { getConnection } from "@/v2/db/turso"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"
import { AppHandler } from "../handler"
import { assetComments, assetCommentsLikes } from "@/v2/db/schema"
import { selectAssetCommentsSchema } from "@/v2/db/schema"
import { sql, eq, desc } from "drizzle-orm"

const getCommentRepliesSchema = z.object({
id: z.string().openapi({
param: {
name: "id",
in: "path",
description: "The ID of the comment to check replies for.",
required: true,
},
}),
})

const getCommentRepliesOffsetSchema = z.object({
offset: z
.string()
.optional()
.openapi({
param: {
name: "offset",
in: "query",
description: "The offset to start from.",
required: false,
},
}),
})

const getCommentRepliesResponseSchema = z.object({
success: z.literal(true),
replies: z.array(
selectAssetCommentsSchema
.pick({
id: true,
parentCommentId: true,
commentedById: true,
comment: true,
createdAt: true,
})
.extend({
hasReplies: z.boolean(),
likes: z.number(),
})
),
})

const getCommentsRepliesRoute = createRoute({
path: "/comment/{id}/replies",
method: "get",
summary: "Get a comment's replies.",
description: "Get a comment's replies.",
tags: ["Asset"],
request: {
params: getCommentRepliesSchema,
query: getCommentRepliesOffsetSchema,
},
responses: {
200: {
description: "Array of replies to a comment.",
content: {
"application/json": {
schema: getCommentRepliesResponseSchema,
},
},
},
...GenericResponses,
},
})

export const GetCommentsRepliesRoute = (handler: AppHandler) => {
handler.openapi(getCommentsRepliesRoute, async (ctx) => {
const commentId = ctx.req.valid("param").id
const offset = parseInt(ctx.req.valid("query").offset) || 0

const { drizzle } = await getConnection(ctx.env)

const replies = await drizzle
.select({
id: assetComments.id,
parentCommentId: assetComments.parentCommentId,
commentedById: assetComments.commentedById,
comment: assetComments.comment,
createdAt: assetComments.createdAt,
hasReplies: sql<number>`EXISTS (SELECT 1 FROM ${assetComments} WHERE ${assetComments.parentCommentId} = ${assetComments.id})`,
likes: sql`COUNT(${assetCommentsLikes.commentId})`,
})
.from(assetComments)
.where(eq(assetComments.parentCommentId, commentId))
.leftJoin(
assetCommentsLikes,
eq(assetComments.id, assetCommentsLikes.commentId)
)
.groupBy(assetComments.id)
.offset(offset)
.limit(10)
.orderBy(desc(assetComments.createdAt))

return ctx.json(
{
success: true,
replies: replies.map((reply) => ({
...reply,
hasReplies: !!reply.hasReplies,
})),
},
200
)
})
}
2 changes: 2 additions & 0 deletions src/v2/routes/asset/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ModifyAssetRoute } from "./modify-asset"
import { UploadAssetRoute } from "./upload-asset"
import { DeleteAssetByIdRoute } from "./delete-asset"
import { DownloadAssetRoute } from "./download-asset"
import { GetCommentsRepliesRoute } from "./get-comment-replies"

import { ViewAssetCommentsRoute } from "./get-asset-comments"

Expand All @@ -28,5 +29,6 @@ UnlikeAssetByIdRoute(handler)
GetAssetLikesRoute(handler)

ViewAssetCommentsRoute(handler)
GetCommentsRepliesRoute(handler)

export default handler

0 comments on commit e9b8c1c

Please sign in to comment.