-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from kumarkrishh/edit-recipe
Edit recipe
- Loading branch information
Showing
3 changed files
with
143 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { NextResponse } from 'next/server'; | ||
import { getServerSession } from 'next-auth/next'; | ||
import { authOptions } from '../auth/[...nextauth]/route'; | ||
import { getClientPromise } from '@/lib/mongodb'; | ||
|
||
export async function PUT(request: Request) { | ||
const session = await getServerSession(authOptions); | ||
|
||
if (!session?.user?.id) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
} | ||
|
||
try { | ||
const recipeData = await request.json(); | ||
const client = await getClientPromise(); | ||
const db = client.db(); | ||
|
||
// Ensure the 'id' field exists and is a string | ||
if (!recipeData.id) { | ||
return NextResponse.json({ error: 'Recipe ID is missing' }, { status: 400 }); | ||
} | ||
|
||
recipeData.id = recipeData.id.toString(); | ||
|
||
// Remove '_id' field if it exists to prevent immutable field update error | ||
if (recipeData._id) { | ||
delete recipeData._id; | ||
} | ||
|
||
// Update the recipe in the database | ||
const result = await db.collection('recipes').updateOne( | ||
{ id: recipeData.id, userId: session.user.id }, | ||
{ $set: recipeData }, | ||
{ upsert: true } | ||
); | ||
|
||
if (result.modifiedCount > 0 || result.upsertedCount > 0) { | ||
return NextResponse.json({ message: 'Recipe updated successfully!' }, { status: 200 }); | ||
} else { | ||
return NextResponse.json({ error: 'Failed to update recipe' }, { status: 500 }); | ||
} | ||
} catch (error) { | ||
console.error('Error updating recipe:', error); | ||
return NextResponse.json({ error: 'Failed to update recipe' }, { status: 500 }); | ||
} | ||
} |
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
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