-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: working front-end & api route forum
- Loading branch information
Showing
35 changed files
with
1,070 additions
and
1,343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.