-
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
Open
FabianSanchezD
wants to merge
2
commits into
GrantChain:develop
Choose a base branch
from
FabianSanchezD:feat--implement-comment-API-routes
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /** | ||
| * 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 }); | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /** | ||
| * 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 | ||
| const 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 }); | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
src/app/api/(comment)/comment/find-by-payout/[payout_id]/route.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * 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; | ||
|
|
||
| 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 }); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.