-
Notifications
You must be signed in to change notification settings - Fork 69
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 #264 from ShivanshPlays/wishlist
- Loading branch information
Showing
7 changed files
with
246 additions
and
1 deletion.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
alimento-nextjs/actions/customer/wishlist/CREATE_wishlist.ts
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,35 @@ | ||
'use server'; | ||
|
||
import prismadb from '@/lib/prismadb'; | ||
import { Wishlist } from '@prisma/client'; | ||
|
||
export async function createWishlist({ | ||
customerId, | ||
dishId, | ||
}: { | ||
customerId: string; | ||
dishId: string; | ||
}): Promise<{ success: boolean; error?: string; data?: Wishlist }> { | ||
try { | ||
const existingWishlist = await prismadb.wishlist.findUnique({ | ||
where: { customerId_dishId: { customerId, dishId } }, | ||
}); | ||
|
||
if (existingWishlist) { | ||
return { success: false, error: 'Wishlist already exists' }; | ||
} | ||
|
||
const Wishlist = await prismadb.wishlist.create({ | ||
data: { | ||
customerId, | ||
dishId, | ||
}, | ||
}); | ||
|
||
return { success: true, data: Wishlist }; | ||
} catch (error) { | ||
console.error('[CREATE_Wishlist_ERROR]', error); | ||
return { success: false, error: 'Error creating Wishlist' }; | ||
} | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
alimento-nextjs/actions/customer/wishlist/DELETE_wishlist.ts
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,23 @@ | ||
'use server'; | ||
|
||
import prismadb from '@/lib/prismadb'; | ||
import { Wishlist } from '@prisma/client'; | ||
|
||
export async function deleteWishlist({ | ||
customerId, | ||
dishId, | ||
}: { | ||
customerId: string; | ||
dishId: string; | ||
}): Promise<{ success: boolean; error?: string; data?: Wishlist }> { | ||
try { | ||
const Wishlist = await prismadb.wishlist.delete({ | ||
where: { customerId_dishId: { customerId, dishId } }, | ||
}); | ||
|
||
return { success: true, data: Wishlist }; | ||
} catch (error) { | ||
console.error('[DELETE_Wishlist_ERROR]', error); | ||
return { success: false, error: 'Error deleting Wishlist' }; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
alimento-nextjs/actions/customer/wishlist/EXISTS_wishlist.ts
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,22 @@ | ||
'use server'; | ||
|
||
import prismadb from '@/lib/prismadb'; | ||
|
||
export async function checkWishlistExists({ | ||
customerId, | ||
dishId, | ||
}: { | ||
customerId: string; | ||
dishId: string; | ||
}): Promise<{ success: boolean; error?: string; exists: boolean }> { | ||
try { | ||
const existingwishlist = await prismadb.wishlist.findUnique({ | ||
where: { customerId_dishId: { customerId, dishId } }, | ||
}); | ||
|
||
return { success: true, exists: !!existingwishlist }; | ||
} catch (error) { | ||
console.error('[CHECK_wishlist_EXISTS_ERROR]', error); | ||
return { success: false, error: 'Error checking wishlist existence', exists: false }; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
alimento-nextjs/actions/customer/wishlist/GETBYCUSTOMER_wishlist.ts
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,36 @@ | ||
'use server'; | ||
|
||
import prismadb from '@/lib/prismadb'; | ||
import { Wishlist, Image, Dish } from '@prisma/client'; | ||
|
||
// Interface for the Wishlist with Dish and Images | ||
export interface WishlistWithDish extends Wishlist { | ||
dish: Dish & { | ||
images: Image[]; // Assuming images is an array of strings (URLs) | ||
}; | ||
} | ||
|
||
export async function getWishlistsBycustomer({ | ||
customerId, | ||
}: { | ||
customerId: string; | ||
}): Promise<{ success: boolean; error?: string; data?: WishlistWithDish[] }> { | ||
try { | ||
// Fetch Wishlists including related Dish data and images | ||
const Wishlists = await prismadb.wishlist.findMany({ | ||
where: { customerId }, | ||
include: { | ||
dish: { | ||
include: { | ||
images: true, // Include the images field from the Dish | ||
}, | ||
}, | ||
}, | ||
}); | ||
// console.log(Wishlists) | ||
return { success: true, data: Wishlists as WishlistWithDish[] }; | ||
} catch (error) { | ||
console.error('[GET_WishlistS_BY_customer_ERROR]', error); | ||
return { success: false, error: 'Error fetching Wishlists' }; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
alimento-nextjs/app/api/testing/wishlist/[customerId]/[dishId]/route.ts
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,82 @@ | ||
|
||
import { createWishlist } from '@/actions/customer/wishlist/CREATE_wishlist'; | ||
import { deleteWishlist } from '@/actions/customer/wishlist/DELETE_wishlist'; | ||
import { checkWishlistExists } from '@/actions/customer/wishlist/EXISTS_wishlist'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
export type Params = Promise<{ | ||
customerId: string; | ||
dishId: string; | ||
}>; | ||
|
||
export async function POST( | ||
request: NextRequest, | ||
{ params }: { params: Params } | ||
) { | ||
const { customerId, dishId } = await params; | ||
|
||
if (!customerId || !dishId) { | ||
return new NextResponse('Necessary params are required', { status: 400 }); | ||
} | ||
|
||
try { | ||
const resp = await createWishlist({ customerId, dishId }); | ||
|
||
if (resp.success) { | ||
return NextResponse.json(resp.data); | ||
} else { | ||
return NextResponse.json({ err: resp.error }, { status: 400 }); | ||
} | ||
} catch (err) { | ||
console.log('[Wishlist_CREATE]', err); | ||
return new NextResponse('Internal Server Error', { status: 500 }); | ||
} | ||
} | ||
|
||
export async function DELETE( | ||
request: NextRequest, | ||
{ params }: { params: Params } | ||
) { | ||
const { customerId, dishId } = await params; | ||
|
||
if (!customerId || !dishId) { | ||
return new NextResponse('Necessary params are required', { status: 400 }); | ||
} | ||
|
||
try { | ||
const resp = await deleteWishlist({ customerId, dishId }); | ||
|
||
if (resp.success) { | ||
return NextResponse.json(resp.data); | ||
} else { | ||
return NextResponse.json({ err: resp.error }, { status: 400 }); | ||
} | ||
} catch (err) { | ||
console.log('[Wishlist_DELETE]', err); | ||
return new NextResponse('Internal Server Error', { status: 500 }); | ||
} | ||
} | ||
|
||
export async function GET( | ||
request: NextRequest, | ||
{ params }: { params: Params } | ||
) { | ||
const { customerId, dishId } = await params; | ||
|
||
if (!customerId || !dishId) { | ||
return new NextResponse('Necessary params are required', { status: 400 }); | ||
} | ||
|
||
try { | ||
const resp = await checkWishlistExists({ customerId, dishId }); | ||
|
||
if (resp.success) { | ||
return NextResponse.json({ exists: resp.exists }); | ||
} else { | ||
return NextResponse.json({ err: resp.error }, { status: 400 }); | ||
} | ||
} catch (err) { | ||
console.log('[Wishlist_CHECK_EXISTS]', err); | ||
return new NextResponse('Internal Server Error', { status: 500 }); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
alimento-nextjs/app/api/testing/wishlist/[customerId]/route.ts
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,31 @@ | ||
|
||
import { getWishlistsBycustomer } from '@/actions/customer/wishlist/GETBYCUSTOMER_wishlist'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
export type Params = Promise<{ | ||
customerId: string; | ||
}>; | ||
|
||
export async function GET( | ||
request: NextRequest, | ||
{ params }: { params: Params } | ||
) { | ||
const { customerId } = await params; | ||
|
||
if (!customerId) { | ||
return new NextResponse('customer ID is required', { status: 400 }); | ||
} | ||
|
||
try { | ||
const resp = await getWishlistsBycustomer({ customerId }); | ||
|
||
if (resp.success) { | ||
return NextResponse.json(resp.data); | ||
} else { | ||
return NextResponse.json({ err: resp.error }, { status: 400 }); | ||
} | ||
} catch (err) { | ||
console.log('[BOOKMARKS_GET_BY_customer]', err); | ||
return new NextResponse('Internal Server Error', { 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