-
Notifications
You must be signed in to change notification settings - Fork 59
feat: Implement College Marketplace with Authentication, Media Uploads, and Seller Controls #241
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
Rohank3
wants to merge
4
commits into
iiitl:main
Choose a base branch
from
Rohank3:marketplace
base: main
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
4 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
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 |
|---|---|---|
|
|
@@ -157,4 +157,3 @@ export async function POST(req: Request) { | |
| ) | ||
| } | ||
| } | ||
|
|
||
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,80 @@ | ||
| import dbConnect from '@/lib/dbConnect' | ||
| import { NextRequest, NextResponse } from 'next/server' | ||
| import mongoose from 'mongoose' | ||
| import Product from '@/model/Product' | ||
| import { verifyJwt } from '@/lib/auth-utils' | ||
|
|
||
| type RouteContext = { params: Promise<{ id: string }> } | ||
|
|
||
| /** | ||
| * PATCH /api/products/:id/available | ||
| * Mark a product as available again. Only seller can do this. | ||
| */ | ||
| export async function PATCH(req: NextRequest, context: RouteContext) { | ||
| try { | ||
| await dbConnect() | ||
|
|
||
| const { id } = await context.params | ||
|
|
||
| if (!mongoose.Types.ObjectId.isValid(id)) { | ||
| return NextResponse.json( | ||
| { message: 'Invalid product ID' }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| // Authenticate the user | ||
| const authResponse = await verifyJwt(req) | ||
| if (authResponse.status !== 200) { | ||
| return authResponse | ||
| } | ||
|
|
||
| const authData = await authResponse.json() | ||
| const userId = authData.userId as string | ||
|
|
||
| const product = await Product.findById(id) | ||
| if (!product) { | ||
| return NextResponse.json( | ||
| { message: 'Product not found' }, | ||
| { status: 404 } | ||
| ) | ||
| } | ||
|
|
||
| // Verify ownership — only the seller can mark as available | ||
| if (product.seller.toString() !== userId) { | ||
| return NextResponse.json( | ||
| { message: 'Forbidden — you can only update your own listings' }, | ||
| { status: 403 } | ||
| ) | ||
| } | ||
|
|
||
| if (!product.is_sold) { | ||
| return NextResponse.json( | ||
| { message: 'Product is already available' }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| product.is_sold = false | ||
| product.show_when_sold = false | ||
| if (product.quantity <= 0) { | ||
| product.quantity = 1 | ||
| } | ||
|
|
||
| await product.save() | ||
|
|
||
| return NextResponse.json( | ||
| { message: 'Product is available again', product }, | ||
| { status: 200 } | ||
| ) | ||
| } catch (error: unknown) { | ||
| console.error('PATCH /api/products/[id]/available error:', error) | ||
| return NextResponse.json( | ||
| { | ||
| message: | ||
| error instanceof Error ? error.message : 'Internal Server Error', | ||
| }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| } |
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,122 @@ | ||
| import dbConnect from '@/lib/dbConnect' | ||
| import { NextRequest, NextResponse } from 'next/server' | ||
| import mongoose from 'mongoose' | ||
| import Product, { IComment } from '@/model/Product' | ||
| import User from '@/model/User' | ||
| import { verifyJwt } from '@/lib/auth-utils' | ||
|
|
||
| type RouteContext = { params: Promise<{ id: string }> } | ||
|
|
||
| /** | ||
| * POST /api/products/:id/comments | ||
| * Add a new comment or reply to an existing comment. | ||
| */ | ||
| export async function POST(req: NextRequest, context: RouteContext) { | ||
| try { | ||
| await dbConnect() | ||
|
|
||
| const { id } = await context.params | ||
|
|
||
| if (!mongoose.Types.ObjectId.isValid(id)) { | ||
| return NextResponse.json( | ||
| { message: 'Invalid product ID' }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| // Authenticate the user | ||
| const authResponse = await verifyJwt(req) | ||
| if (authResponse.status !== 200) { | ||
| return authResponse | ||
| } | ||
|
|
||
| const authData = await authResponse.json() | ||
| const userId = authData.userId as string | ||
|
|
||
| const body = await req.json() | ||
| const { text, offerPrice, parentCommentId } = body | ||
|
|
||
| if (!text || !String(text).trim()) { | ||
| return NextResponse.json( | ||
| { message: 'Comment text is required' }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| const product = await Product.findById(id) | ||
| if (!product) { | ||
| return NextResponse.json( | ||
| { message: 'Product not found' }, | ||
| { status: 404 } | ||
| ) | ||
| } | ||
|
|
||
| if (!product.comments) { | ||
| product.comments = [] | ||
| } | ||
|
|
||
| const newReply = { | ||
| user: new mongoose.Types.ObjectId(userId), | ||
| text: String(text).trim().substring(0, 500), | ||
| createdAt: new Date(), | ||
| } | ||
|
|
||
| if (parentCommentId) { | ||
| // It's a reply | ||
| if (!mongoose.Types.ObjectId.isValid(parentCommentId)) { | ||
| return NextResponse.json( | ||
| { message: 'Invalid parent comment ID' }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| const parentComment = product.comments.find( | ||
| (c: any) => c._id && c._id.toString() === parentCommentId | ||
| ) as any | ||
| if (!parentComment) { | ||
| return NextResponse.json( | ||
| { message: 'Parent comment not found' }, | ||
| { status: 404 } | ||
| ) | ||
| } | ||
|
|
||
| parentComment.replies.push(newReply) | ||
| } else { | ||
| // Top level comment | ||
| const newComment = { | ||
| user: new mongoose.Types.ObjectId(userId), | ||
| text: String(text).trim().substring(0, 1000), | ||
| offerPrice: | ||
| offerPrice !== undefined && offerPrice !== null && offerPrice !== '' | ||
| ? Number(offerPrice) | ||
| : undefined, | ||
| createdAt: new Date(), | ||
| replies: [], | ||
| } | ||
| product.comments.push(newComment as any) | ||
| } | ||
|
|
||
| await product.save() | ||
|
|
||
| // We want the populated user info back, so we populate the newly added part, or just fetch the whole product populated | ||
| const populatedProduct = await Product.findById(product._id) | ||
| .populate('seller', 'name email image') | ||
| .populate({ path: 'comments.user', select: 'name email image' }) | ||
| .populate({ path: 'comments.replies.user', select: 'name email image' }) | ||
| .lean() | ||
|
|
||
| return NextResponse.json( | ||
| { message: 'Comment added successfully', product: populatedProduct }, | ||
| { status: 201 } | ||
| ) | ||
| } catch (error: unknown) { | ||
| console.error('POST /api/products/[id]/comments error:', error) | ||
| return NextResponse.json( | ||
| { | ||
| message: | ||
| error instanceof Error ? error.message : 'Internal Server Error', | ||
| }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate
offerPriceis a valid positive number.The current check allows any truthy value to be converted to
Number(), which could result inNaNbeing stored if a non-numeric string is passed.🛡️ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents