From d6fc144d86c4bc659180542a1cc6d34439111967 Mon Sep 17 00:00:00 2001 From: Shivansh Date: Sat, 9 Nov 2024 17:16:02 +0530 Subject: [PATCH] WISHLIST BACKEND: schema, serveractions and API --- .../customer/wishlist/CREATE_wishlist.ts | 35 ++++++++ .../customer/wishlist/DELETE_wishlist.ts | 23 ++++++ .../customer/wishlist/EXISTS_wishlist.ts | 22 +++++ .../wishlist/GETBYCUSTOMER_wishlist.ts | 36 ++++++++ .../wishlist/[customerId]/[dishId]/route.ts | 82 +++++++++++++++++++ .../testing/wishlist/[customerId]/route.ts | 31 +++++++ alimento-nextjs/prisma/schema.prisma | 17 ++++ 7 files changed, 246 insertions(+) create mode 100644 alimento-nextjs/actions/customer/wishlist/CREATE_wishlist.ts create mode 100644 alimento-nextjs/actions/customer/wishlist/DELETE_wishlist.ts create mode 100644 alimento-nextjs/actions/customer/wishlist/EXISTS_wishlist.ts create mode 100644 alimento-nextjs/actions/customer/wishlist/GETBYCUSTOMER_wishlist.ts create mode 100644 alimento-nextjs/app/api/testing/wishlist/[customerId]/[dishId]/route.ts create mode 100644 alimento-nextjs/app/api/testing/wishlist/[customerId]/route.ts diff --git a/alimento-nextjs/actions/customer/wishlist/CREATE_wishlist.ts b/alimento-nextjs/actions/customer/wishlist/CREATE_wishlist.ts new file mode 100644 index 0000000..93d2cf1 --- /dev/null +++ b/alimento-nextjs/actions/customer/wishlist/CREATE_wishlist.ts @@ -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' }; + } +} + \ No newline at end of file diff --git a/alimento-nextjs/actions/customer/wishlist/DELETE_wishlist.ts b/alimento-nextjs/actions/customer/wishlist/DELETE_wishlist.ts new file mode 100644 index 0000000..1b58366 --- /dev/null +++ b/alimento-nextjs/actions/customer/wishlist/DELETE_wishlist.ts @@ -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' }; + } +} diff --git a/alimento-nextjs/actions/customer/wishlist/EXISTS_wishlist.ts b/alimento-nextjs/actions/customer/wishlist/EXISTS_wishlist.ts new file mode 100644 index 0000000..d8d4542 --- /dev/null +++ b/alimento-nextjs/actions/customer/wishlist/EXISTS_wishlist.ts @@ -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 }; + } +} diff --git a/alimento-nextjs/actions/customer/wishlist/GETBYCUSTOMER_wishlist.ts b/alimento-nextjs/actions/customer/wishlist/GETBYCUSTOMER_wishlist.ts new file mode 100644 index 0000000..9b1166a --- /dev/null +++ b/alimento-nextjs/actions/customer/wishlist/GETBYCUSTOMER_wishlist.ts @@ -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' }; + } +} diff --git a/alimento-nextjs/app/api/testing/wishlist/[customerId]/[dishId]/route.ts b/alimento-nextjs/app/api/testing/wishlist/[customerId]/[dishId]/route.ts new file mode 100644 index 0000000..1f1c728 --- /dev/null +++ b/alimento-nextjs/app/api/testing/wishlist/[customerId]/[dishId]/route.ts @@ -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 }); + } +} diff --git a/alimento-nextjs/app/api/testing/wishlist/[customerId]/route.ts b/alimento-nextjs/app/api/testing/wishlist/[customerId]/route.ts new file mode 100644 index 0000000..885cec8 --- /dev/null +++ b/alimento-nextjs/app/api/testing/wishlist/[customerId]/route.ts @@ -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 }); + } +} diff --git a/alimento-nextjs/prisma/schema.prisma b/alimento-nextjs/prisma/schema.prisma index c5ef52b..2f8126b 100644 --- a/alimento-nextjs/prisma/schema.prisma +++ b/alimento-nextjs/prisma/schema.prisma @@ -15,6 +15,8 @@ model Customer { otp String? role String @default("customer") + wishlists Wishlist[] @relation("CustomerToWishlist") + createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } @@ -64,6 +66,8 @@ model Dish { images Image[] + wishlist Wishlist[] @relation("DishToWishlist") + vendorId String vendor Vendor @relation(fields: [vendorId], references: [id]) @@ -79,3 +83,16 @@ model Image { createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } + +model Wishlist { + id String @id @default(uuid()) @map("_id") + customerId String + dishId String + customer Customer @relation("CustomerToWishlist", fields: [customerId], references: [id]) + dish Dish @relation("DishToWishlist", fields: [dishId], references: [id]) + + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + @@unique([customerId, dishId]) // Ensures a customer can only Wishlist a dish once +}