Initial food blog app
This commit is contained in:
53
app/api/restaurants/[id]/route.ts
Normal file
53
app/api/restaurants/[id]/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
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 })
|
||||
}
|
||||
Reference in New Issue
Block a user