Skip to content

Commit

Permalink
asset comments show likes count
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 2815656 commit b6bd6bc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
30 changes: 17 additions & 13 deletions src/v2/db/schema/asset/asset-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
53 changes: 32 additions & 21 deletions src/v2/routes/asset/get-asset-comments.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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({
Expand Down Expand Up @@ -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(
{
Expand Down
4 changes: 4 additions & 0 deletions src/v2/routes/asset/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -23,4 +25,6 @@ UnlikeAssetByIdRoute(handler)

GetAssetLikesRoute(handler)

ViewAssetCommentsRoute(handler)

export default handler

0 comments on commit b6bd6bc

Please sign in to comment.