Skip to content

Commit

Permalink
Merge pull request #246 from ShivanshPlays/EDIT-DISH
Browse files Browse the repository at this point in the history
edit-dish feature
  • Loading branch information
Vimall03 authored Nov 8, 2024
2 parents 175f9af + 705d290 commit 30a93e3
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 16 deletions.
58 changes: 44 additions & 14 deletions alimento-nextjs/actions/dish/dishEDIT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,80 @@
import prismadb from '@/lib/prismadb';
import { Dish, Category, Tag } from '@prisma/client';

export async function editDish({
id,
export async function UpdateDish({
name,
description,
price,
category,
tags,
vendorId,
images,
dishId
}: {
id: string;
name?: string;
description?: string;
price?: number;
category?: Category;
tags?: Tag[];
vendorId: string;
dishId: string;
name: string;
description: string;
price: number;
category: Category;
tags: Tag[];
images: string[]
}): Promise<{ success: boolean; error?: string; data?: Dish }> {
// Validate input data
if (
!vendorId ||
!dishId ||
!name ||
!category ||
!description ||
!price ||
!images ||
images.length === 0
) {
return { success: false, error: 'All entries are required!' };
}

try {
// Check if Dish belongs to vendor to prevent unauthorized updates
const existingDish = await prismadb.dish.findUnique({
where: { id },
where: { id: dishId },
select: { vendorId: true },
});

if (!existingDish) {
return { success: false, error: 'Dish not found' };
}

if (existingDish.vendorId !== vendorId) {
return { success: false, error: 'Unauthorized: You do not own this dish' };
return { success: false, error: 'Unauthorized: You do not own this Dish' };
}

// Delete existing images and then add new images to the Dish
await prismadb.image.deleteMany({
where: { dishId },
});

const updatedDish = await prismadb.dish.update({
where: { id },
where: { id: dishId },
data: {
name,
description,
price,
category,
tags,
vendorId,
images: {
create: images.map(url => ({ url })), // Assuming you're passing URLs
},
},
});

return { success: true, data: updatedDish };
} catch (error) {
console.error('[EDIT_DISH_ERROR]', error);
return { success: false, error: 'Error updating dish' };
} catch (err) {
console.error('[UPDATE_Dish_ERROR]', err);
if (err instanceof Error) {
return { success: false, error: err.message };
}
return { success: false, error: 'An unknown error occurred during Dish update' };
}
}
11 changes: 9 additions & 2 deletions alimento-nextjs/actions/dish/dishGET.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
'use server';

import prismadb from '@/lib/prismadb';
import { Dish } from '@prisma/client';
import { Dish, Image } from '@prisma/client';

interface DishWithImage extends Dish{
images:Image[]
}

export async function getDishById({
id,
}: {
id: string;
}): Promise<{ success: boolean; error?: string; data?: Dish }> {
}): Promise<{ success: boolean; error?: string; data?: DishWithImage }> {
try {
const dish = await prismadb.dish.findUnique({
where: { id },
include:{
images:true
}
});

if (!dish) {
Expand Down
Loading

0 comments on commit 30a93e3

Please sign in to comment.