Skip to content

Commit

Permalink
refactor: working front-end & api route forum
Browse files Browse the repository at this point in the history
  • Loading branch information
bntlyr committed Jan 6, 2025
1 parent e9664a7 commit 15cad30
Show file tree
Hide file tree
Showing 35 changed files with 1,070 additions and 1,343 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"@radix-ui/react-icons": "^1.3.1",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-select": "^2.1.2",
"@radix-ui/react-separator": "^1.1.1",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-switch": "^1.1.2",
"@radix-ui/react-tabs": "^1.1.1",
"@t3-oss/env-nextjs": "^0.11.1",
"@tanstack/react-query": "^5.59.20",
Expand All @@ -38,6 +40,7 @@
"adm-zip": "^0.5.16",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"dotenv": "^16.4.7",
"framer-motion": "^11.11.11",
"geist": "^1.3.1",
"jszip": "^3.10.1",
Expand Down Expand Up @@ -103,5 +106,4 @@
"browser": {
"original-fs": false
}

}
68 changes: 68 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions src/app/api/forum/questions/[id]/replies/route.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/app/api/forum/questions/[id]/route.ts

This file was deleted.

81 changes: 25 additions & 56 deletions src/app/api/forum/questions/route.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,32 @@
import { NextResponse } from 'next/server'
import { prisma } from '~/lib/forum/db'
import { getServerAuthSession } from '~/server/auth'

export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const page = parseInt(searchParams.get('page') || '1')
const limit = parseInt(searchParams.get('limit') || '10')

const questions = await prisma.question.findMany({
take: limit,
skip: (page - 1) * limit,
orderBy: { createdAt: 'desc' },
include: {
author: true,
tags: true,
_count: {
select: {
replies: true,
votes: true,
},
},
},
})

const total = await prisma.question.count()

return NextResponse.json({
questions,
total,
pages: Math.ceil(total / limit),
})
}
import prisma from '~/lib/prisma'

export async function POST(request: Request) {
const session = await getServerAuthSession() // Use the correct auth function to get the session
if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}

const body = await request.json()
const { title, content, tags } = body

const question = await prisma.question.create({
data: {
title,
content,
authorId: session.user.id,
tags: {
connectOrCreate: tags.map((tag: string) => ({
where: { name: tag },
create: { name: tag },
})),
const { title, content, tags, authorId } = body

try {
const question = await prisma.question.create({
data: {
title,
content,
author: { connect: { id: authorId } },
tags: {
connectOrCreate: tags.map((tag: string) => ({
where: { name: tag },
create: { name: tag },
})),
},
},
},
include: {
author: true,
tags: true,
},
})
include: {
author: true,
tags: true,
},
})

return NextResponse.json(question)
return NextResponse.json(question)
} catch (error) {
return NextResponse.json({ error: 'Error creating question' }, { status: 500 })
}
}

50 changes: 0 additions & 50 deletions src/app/api/forum/tags/route.ts

This file was deleted.

Loading

0 comments on commit 15cad30

Please sign in to comment.