Skip to content

Commit

Permalink
comment offset and limit, sort by creation date (for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent b6bd6bc commit 1e9dca1
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/v2/routes/asset/get-asset-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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 } from "drizzle-orm"
import { sql, eq, desc } from "drizzle-orm"

const getAssetCommentsSchema = z.object({
id: z.string().openapi({
Expand All @@ -18,6 +18,20 @@ const getAssetCommentsSchema = z.object({
}),
})

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

const getAssetCommentsResponseSchema = z.object({
success: z.literal(true),
comments: z.array(
Expand All @@ -42,6 +56,7 @@ const getAssetCommentsRoute = createRoute({
tags: ["Asset"],
request: {
params: getAssetCommentsSchema,
query: getAssetCommentsOffsetSchema,
},
responses: {
200: {
Expand All @@ -59,6 +74,7 @@ const getAssetCommentsRoute = createRoute({
export const ViewAssetCommentsRoute = (handler: AppHandler) => {
handler.openapi(getAssetCommentsRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id
const offset = parseInt(ctx.req.valid("query").offset) || 0

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

Expand All @@ -78,6 +94,9 @@ export const ViewAssetCommentsRoute = (handler: AppHandler) => {
eq(assetComments.id, assetCommentsLikes.commentId)
)
.groupBy(assetComments.id)
.offset(offset)
.limit(10)
.orderBy(desc(assetComments.createdAt))

return ctx.json(
{
Expand Down

0 comments on commit 1e9dca1

Please sign in to comment.