54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { readRestaurants, writeRestaurants } from '@/app/lib/restaurants'
|
|
import { Restaurant } from '@/app/types'
|
|
|
|
export async function GET(
|
|
_request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params
|
|
const restaurants = readRestaurants()
|
|
const restaurant = restaurants.find((r) => r.id === id)
|
|
|
|
if (!restaurant) {
|
|
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
|
}
|
|
|
|
return NextResponse.json(restaurant)
|
|
}
|
|
|
|
export async function PUT(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params
|
|
const body = await request.json() as Partial<Restaurant>
|
|
const restaurants = readRestaurants()
|
|
const index = restaurants.findIndex((r) => r.id === id)
|
|
|
|
if (index === -1) {
|
|
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
|
}
|
|
|
|
restaurants[index] = { ...restaurants[index], ...body, id }
|
|
writeRestaurants(restaurants)
|
|
|
|
return NextResponse.json(restaurants[index])
|
|
}
|
|
|
|
export async function DELETE(
|
|
_request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params
|
|
const restaurants = readRestaurants()
|
|
const filtered = restaurants.filter((r) => r.id !== id)
|
|
|
|
if (filtered.length === restaurants.length) {
|
|
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
|
}
|
|
|
|
writeRestaurants(filtered)
|
|
return NextResponse.json({ success: true })
|
|
}
|