Skip to content

Commit

Permalink
Merge pull request #264 from ShivanshPlays/wishlist
Browse files Browse the repository at this point in the history
  • Loading branch information
Vimall03 authored Nov 9, 2024
2 parents be4ef4b + 97095c1 commit f1ca9f9
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 1 deletion.
35 changes: 35 additions & 0 deletions alimento-nextjs/actions/customer/wishlist/CREATE_wishlist.ts
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 alimento-nextjs/actions/customer/wishlist/DELETE_wishlist.ts
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 alimento-nextjs/actions/customer/wishlist/EXISTS_wishlist.ts
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 };
}
}
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' };
}
}
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 alimento-nextjs/app/api/testing/wishlist/[customerId]/route.ts
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 });
}
}
18 changes: 17 additions & 1 deletion alimento-nextjs/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ model Customer {
email String @unique
otp String?
role String @default("customer")
wishlists Wishlist[] @relation("CustomerToWishlist")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Expand Down Expand Up @@ -64,6 +66,8 @@ model Dish {
images Image[]
wishlist Wishlist[] @relation("DishToWishlist")
vendorId String
vendor Vendor @relation(fields: [vendorId], references: [id])
Expand All @@ -80,7 +84,19 @@ model Image {
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
}
model Question {
id String @id @default(cuid()) @map("_id")
content String
Expand Down

0 comments on commit f1ca9f9

Please sign in to comment.