19 lines
857 B
TypeScript
19 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
|
||
|
|
})
|