Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edit-dish feature #246

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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