-
Notifications
You must be signed in to change notification settings - Fork 44
feat: implement comment API routes #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /** | ||
| * POST /api/comment/create | ||
| * | ||
| * Body: { message: string, user_id: string, payout_id: string } | ||
| * Returns: { success: boolean, comment: Comment, message: string } | ||
| */ | ||
| import { commentCreateSchema } from "@/components/modules/comment/schema/comment.schema"; | ||
| import { handleDatabaseError, prisma } from "@/lib/prisma"; | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| export async function POST(request: Request) { | ||
| try { | ||
| const body = await request.json(); | ||
| const parsed = commentCreateSchema.safeParse(body); | ||
|
|
||
| if (!parsed.success) { | ||
| return NextResponse.json( | ||
| { error: "Invalid data", details: parsed.error.errors }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const { message, user_id, payout_id } = parsed.data; | ||
|
|
||
coxmars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Find the user in the database | ||
| const user = await prisma.user.findUnique({ | ||
| where: { user_id }, | ||
| select: { user_id: true, is_active: true } | ||
| }); | ||
|
|
||
| if (!user) { | ||
| return NextResponse.json( | ||
| { error: "User not found" }, | ||
| { status: 404 } | ||
| ); | ||
| } | ||
|
|
||
| if (!user.is_active) { | ||
| return NextResponse.json( | ||
| { error: "User account is not active" }, | ||
| { status: 403 } | ||
| ); | ||
| } | ||
|
|
||
| // Find the payout in the database | ||
| const payout = await prisma.payout.findUnique({ | ||
| where: { payout_id }, | ||
| select: { | ||
| payout_id: true, | ||
| status: true, | ||
| created_by: true | ||
| } | ||
| }); | ||
|
|
||
| if (!payout) { | ||
| return NextResponse.json( | ||
| { error: "Payout not found" }, | ||
| { status: 404 } | ||
| ); | ||
| } | ||
|
|
||
| // Create the comment | ||
| const comment = await prisma.comment.create({ | ||
| data: { | ||
| message, | ||
| user_id, | ||
| payout_id, | ||
| }, | ||
| include: { | ||
| user: { | ||
| select: { | ||
| user_id: true, | ||
| username: true, | ||
| profile_url: true, | ||
| bio: true, | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return NextResponse.json( | ||
| { | ||
| success: true, | ||
| comment, | ||
| message: "Comment created successfully" | ||
| }, | ||
| { status: 201 } | ||
| ); | ||
| } catch (error) { | ||
| const { message, status } = handleDatabaseError(error); | ||
| return NextResponse.json({ error: message }, { status }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * DELETE /api/comment/delete/[id] | ||
| * Deletes an existing comment (only author can edit) | ||
| * | ||
| * Params: { id: string } | ||
| * Body: { user_id: string } | ||
| * Returns: { success: boolean, message: string } | ||
| */ | ||
| import { handleDatabaseError, prisma } from "@/lib/prisma"; | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| export async function DELETE( | ||
| request: Request, | ||
| { params }: { params: Promise<{ id: string }> } | ||
| ) { | ||
| try { | ||
| const { id } = await params; | ||
| const body = await request.json(); | ||
|
|
||
| if (!id || id === "undefined" || id === "null") { | ||
| return NextResponse.json( | ||
| { error: "Invalid comment ID parameter" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // Get user_id from request body | ||
| let user_id: string = body.user_id; | ||
|
|
||
| if (!user_id) { | ||
| return NextResponse.json( | ||
| { error: "User ID is required" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
coxmars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Find the comment in the database | ||
| const existingComment = await prisma.comment.findUnique({ | ||
| where: { comment_id: id }, | ||
| select: { | ||
| comment_id: true, | ||
| user_id: true, | ||
| payout_id: true | ||
| } | ||
| }); | ||
|
|
||
| if (!existingComment) { | ||
| return NextResponse.json( | ||
| { error: "Comment not found" }, | ||
| { status: 404 } | ||
| ); | ||
| } | ||
|
|
||
| // Verify that the user owns this comment | ||
| if (existingComment.user_id !== user_id) { | ||
| return NextResponse.json( | ||
| { error: "You can only delete your own comments" }, | ||
| { status: 403 } | ||
| ); | ||
| } | ||
|
|
||
| // Delete the comment | ||
| await prisma.comment.delete({ | ||
| where: { comment_id: id } | ||
| }); | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| message: "Comment deleted successfully" | ||
| }); | ||
| } catch (error) { | ||
| const { message, status } = handleDatabaseError(error); | ||
| return NextResponse.json({ error: message }, { status }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * GET /api/comment/find-by-payout/[payout_id] | ||
| * Retrieves comments from a specific payout | ||
| * | ||
| * Params: { payout_id: string } | ||
| * Returns: { success: boolean, comments: Comment[], count: number } | ||
| */ | ||
| import { handleDatabaseError, prisma } from "@/lib/prisma"; | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| export async function GET( | ||
| { params }: { params: Promise<{ payout_id: string }> } | ||
| ) { | ||
| try { | ||
| const { payout_id } = await params; | ||
|
|
||
coxmars marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!payout_id || payout_id === "undefined" || payout_id === "null") { | ||
| return NextResponse.json( | ||
| { error: "Invalid payout_id parameter" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // Find the payout in the database | ||
| const payout = await prisma.payout.findUnique({ | ||
| where: { payout_id }, | ||
| select: { payout_id: true } | ||
| }); | ||
|
|
||
| if (!payout) { | ||
| return NextResponse.json( | ||
| { error: "Payout not found" }, | ||
| { status: 404 } | ||
| ); | ||
| } | ||
|
|
||
| // Get comments for the payout, ordered chronologically | ||
| const comments = await prisma.comment.findMany({ | ||
| where: { payout_id }, | ||
| include: { | ||
| user: { | ||
| select: { | ||
| user_id: true, | ||
| username: true, | ||
| profile_url: true, | ||
| bio: true, | ||
| } | ||
| } | ||
| }, | ||
| orderBy: { created_at: 'asc' } | ||
| }); | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| comments, | ||
| count: comments.length | ||
| }); | ||
| } catch (error) { | ||
| const { message, status } = handleDatabaseError(error); | ||
| return NextResponse.json({ error: message }, { status }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * PATCH /api/comment/update/[id] | ||||||||||||||||||||||||||||||||||
| * Update an existing comment (only author can edit) | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * Params: { id: string } | ||||||||||||||||||||||||||||||||||
| * Body: { message: string, user_id: string } | ||||||||||||||||||||||||||||||||||
| * Returns: { success: boolean, comment: Comment, message: string } | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+8
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update JSDoc to reflect actual request body. The JSDoc states Apply this diff to correct the documentation: /**
* PATCH /api/comment/update/[id]
* Update an existing comment (only author can edit)
*
* Params: { id: string }
- * Body: { message: string, user_id: string }
+ * Body: { message: string }
* Returns: { success: boolean, comment: Comment, message: string }
*/📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| import { commentUpdateSchema } from "@/components/modules/comment/schema/comment.schema"; | ||||||||||||||||||||||||||||||||||
| import { handleDatabaseError, prisma } from "@/lib/prisma"; | ||||||||||||||||||||||||||||||||||
| import { NextResponse } from "next/server"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export async function PATCH( | ||||||||||||||||||||||||||||||||||
| request: Request, | ||||||||||||||||||||||||||||||||||
| { params }: { params: Promise<{ id: string }> } | ||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||
| const { id } = await params; | ||||||||||||||||||||||||||||||||||
| const body = await request.json(); | ||||||||||||||||||||||||||||||||||
| const parsed = commentUpdateSchema.safeParse(body); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (!parsed.success) { | ||||||||||||||||||||||||||||||||||
| return NextResponse.json( | ||||||||||||||||||||||||||||||||||
| { error: "Invalid data", details: parsed.error.errors }, | ||||||||||||||||||||||||||||||||||
| { status: 400 } | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const { message } = parsed.data; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Get user_id from request body | ||||||||||||||||||||||||||||||||||
| let user_id: string = body.user_id; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (!user_id) { | ||||||||||||||||||||||||||||||||||
| return NextResponse.json( | ||||||||||||||||||||||||||||||||||
| { error: "User ID is required" }, | ||||||||||||||||||||||||||||||||||
| { status: 400 } | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Find the comment and verify ownership | ||||||||||||||||||||||||||||||||||
| const existingComment = await prisma.comment.findUnique({ | ||||||||||||||||||||||||||||||||||
| where: { comment_id: id }, | ||||||||||||||||||||||||||||||||||
| select: { | ||||||||||||||||||||||||||||||||||
| comment_id: true, | ||||||||||||||||||||||||||||||||||
| user_id: true, | ||||||||||||||||||||||||||||||||||
| payout_id: true | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (!existingComment) { | ||||||||||||||||||||||||||||||||||
| return NextResponse.json( | ||||||||||||||||||||||||||||||||||
| { error: "Comment not found" }, | ||||||||||||||||||||||||||||||||||
| { status: 404 } | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Verify that the user owns this comment | ||||||||||||||||||||||||||||||||||
| if (existingComment.user_id !== user_id) { | ||||||||||||||||||||||||||||||||||
| return NextResponse.json( | ||||||||||||||||||||||||||||||||||
| { error: "You can only update your own comments" }, | ||||||||||||||||||||||||||||||||||
| { status: 403 } | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Update the comment | ||||||||||||||||||||||||||||||||||
| const updatedComment = await prisma.comment.update({ | ||||||||||||||||||||||||||||||||||
| where: { comment_id: id }, | ||||||||||||||||||||||||||||||||||
| data: { message }, | ||||||||||||||||||||||||||||||||||
| include: { | ||||||||||||||||||||||||||||||||||
| user: { | ||||||||||||||||||||||||||||||||||
| select: { | ||||||||||||||||||||||||||||||||||
| user_id: true, | ||||||||||||||||||||||||||||||||||
| username: true, | ||||||||||||||||||||||||||||||||||
| profile_url: true, | ||||||||||||||||||||||||||||||||||
| bio: true, | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return NextResponse.json({ | ||||||||||||||||||||||||||||||||||
| success: true, | ||||||||||||||||||||||||||||||||||
| comment: updatedComment, | ||||||||||||||||||||||||||||||||||
| message: "Comment updated successfully" | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||
| const { message, status } = handleDatabaseError(error); | ||||||||||||||||||||||||||||||||||
| return NextResponse.json({ error: message }, { status }); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.