Skip to content

Commit

Permalink
feat: implement first draft of comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CaliCastle committed Jun 4, 2023
1 parent 0046e86 commit adc793f
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 118 deletions.
14 changes: 6 additions & 8 deletions app/api/comments/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { currentUser } from '@clerk/nextjs'
import { desc, eq } from 'drizzle-orm'
import { asc, eq } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'

Expand All @@ -20,14 +20,14 @@ export async function GET(req: NextRequest, { params }: Params) {
const data = await db
.select({
id: comments.id,
userId: comments.userId,
userInfo: comments.userInfo,
body: comments.body,
createdAt: comments.createdAt,
})
.from(comments)
.where(eq(comments.postId, postId))
.orderBy(desc(comments.createdAt))
.limit(100)
.orderBy(asc(comments.createdAt))

return NextResponse.json(
data.map(
Expand All @@ -46,7 +46,7 @@ export async function GET(req: NextRequest, { params }: Params) {
const CreateCommentSchema = z.object({
body: z.object({
blockId: z.string().optional(),
text: z.string().min(1),
text: z.string().min(1).max(500),
}),
})

Expand All @@ -73,17 +73,15 @@ export async function POST(req: NextRequest, { params }: Params) {

const commentData = {
postId,
userId: user.id,
body,
userInfo: {
firstName: user.firstName,
lastName: user.lastName,
imageUrl: user.imageUrl,
},
}
const { insertId } = await db.insert(comments).values({
userId: user.id,
...commentData,
})
const { insertId } = await db.insert(comments).values(commentData)

return NextResponse.json({
...commentData,
Expand Down
Loading

0 comments on commit adc793f

Please sign in to comment.