site-panzai/server/api/menu/[id].patch.ts
2026-11-13 13:57:04 +01:00

18 lines
857 B
TypeScript

import type { Dish } from '../../utils/store'
// Mise à jour d'un plat : disponibilité, prix, description
export default defineEventHandler(async (event) => {
const id = getRouterParam(event, 'id')
const dish = db.dishes.find(d => d.id === id)
if (!dish) throw createError({ statusCode: 404, statusMessage: 'Plat introuvable' })
const body = await readBody<Partial<Pick<Dish, 'available' | 'desc' | 'name'>> & { prices?: Dish['prices'] }>(event)
if (typeof body.available === 'boolean') dish.available = body.available
if (typeof body.desc === 'string') dish.desc = body.desc
if (typeof body.name === 'string' && body.name.trim()) dish.name = body.name.trim()
if (body.prices) {
for (const k of ['L', 'XL'] as const) {
if (k in body.prices && k in dish.prices) dish.prices[k] = body.prices[k] ?? null
}
}
return dish
})