diff --git a/src/v2/db/schema/asset/asset-comments.ts b/src/v2/db/schema/asset/asset-comments.ts index 5ba7bd9..6d05134 100644 --- a/src/v2/db/schema/asset/asset-comments.ts +++ b/src/v2/db/schema/asset/asset-comments.ts @@ -27,7 +27,7 @@ export const assetComments = sqliteTable( }), // typescript limitations means that the type will be set as `any` if we self reference, so we create FK manually parentCommentId: text("parent_comment_id"), - commentedById: text("liked_by_id") + commentedById: text("commented_by_id") .notNull() .references(() => authUser.id, { onUpdate: "cascade", @@ -109,18 +109,22 @@ export const selectAssetCommentsLikesSchema = createSelectSchema(assetCommentsLikes) // not too sure about this -export const assetCommentsRelations = relations(assetComments, ({ one }) => ({ - asset: one(asset, { - fields: [assetComments.assetId], - references: [asset.id], - relationName: "asset_comments_asset", - }), - commentedBy: one(authUser, { - fields: [assetComments.commentedById], - references: [authUser.id], - relationName: "asset_comments_commented_by", - }), -})) +export const assetCommentsRelations = relations( + assetComments, + ({ one, many }) => ({ + asset: one(asset, { + fields: [assetComments.assetId], + references: [asset.id], + relationName: "asset_comments_asset", + }), + commentedBy: one(authUser, { + fields: [assetComments.commentedById], + references: [authUser.id], + relationName: "asset_comments_commented_by", + }), + assetCommentsLikes: many(assetCommentsLikes), + }) +) export const assetCommentsLikesRelations = relations( assetCommentsLikes, diff --git a/src/v2/routes/asset/get-asset-comments.ts b/src/v2/routes/asset/get-asset-comments.ts index 1672e0a..9411bd5 100644 --- a/src/v2/routes/asset/get-asset-comments.ts +++ b/src/v2/routes/asset/get-asset-comments.ts @@ -1,9 +1,11 @@ import { getConnection } from "@/v2/db/turso" -import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager" 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 } from "drizzle-orm" const getAssetCommentsSchema = z.object({ id: z.string().openapi({ @@ -18,6 +20,19 @@ const getAssetCommentsSchema = z.object({ const getAssetCommentsResponseSchema = z.object({ success: z.literal(true), + comments: z.array( + selectAssetCommentsSchema + .pick({ + id: true, + parentCommentId: true, + commentedById: true, + comment: true, + createdAt: true, + }) + .extend({ + likes: z.number(), + }) + ), }) const getAssetCommentsRoute = createRoute({ @@ -45,28 +60,24 @@ export const ViewAssetCommentsRoute = (handler: AppHandler) => { handler.openapi(getAssetCommentsRoute, async (ctx) => { const assetId = ctx.req.valid("param").id - const authSessionManager = new AuthSessionManager(ctx) - const { user } = await authSessionManager.validateSession() - - if (!user) { - return ctx.json( - { - success: false, - message: "Unauthorized", - }, - 401 - ) - } - const { drizzle } = await getConnection(ctx.env) - const comments = await drizzle.query.assetComments.findMany({ - where: (assetComments, { eq }) => - eq(assetComments.assetId, assetId), - with: { - assetCommentsLikes: true, - }, - }) + const comments = await drizzle + .select({ + id: assetComments.id, + parentCommentId: assetComments.parentCommentId, + commentedById: assetComments.commentedById, + comment: assetComments.comment, + createdAt: assetComments.createdAt, + likes: sql`COUNT(${assetCommentsLikes.commentId})`, + }) + .from(assetComments) + .where(eq(assetComments.assetId, assetId)) + .leftJoin( + assetCommentsLikes, + eq(assetComments.id, assetCommentsLikes.commentId) + ) + .groupBy(assetComments.id) return ctx.json( { diff --git a/src/v2/routes/asset/handler.ts b/src/v2/routes/asset/handler.ts index ff8f15b..1bea453 100644 --- a/src/v2/routes/asset/handler.ts +++ b/src/v2/routes/asset/handler.ts @@ -9,6 +9,8 @@ import { ModifyAssetRoute } from "./modify-asset" import { UploadAssetRoute } from "./upload-asset" import { DeleteAssetByIdRoute } from "./delete-asset" +import { ViewAssetCommentsRoute } from "./get-asset-comments" + const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>() AssetSearchAllFilterRoute(handler) @@ -23,4 +25,6 @@ UnlikeAssetByIdRoute(handler) GetAssetLikesRoute(handler) +ViewAssetCommentsRoute(handler) + export default handler