Initial food blog app
This commit is contained in:
27
app/api/restaurants/[id]/visits/route.ts
Normal file
27
app/api/restaurants/[id]/visits/route.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { readRestaurants, writeRestaurants } from '@/app/lib/restaurants'
|
||||
import { Visit } from '@/app/types'
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id } = await params
|
||||
const body = await request.json() as Omit<Visit, 'id'>
|
||||
const restaurants = readRestaurants()
|
||||
const index = restaurants.findIndex((r) => r.id === id)
|
||||
|
||||
if (index === -1) {
|
||||
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
const newVisit: Visit = {
|
||||
...body,
|
||||
id: crypto.randomUUID(),
|
||||
}
|
||||
|
||||
restaurants[index].visits.push(newVisit)
|
||||
writeRestaurants(restaurants)
|
||||
|
||||
return NextResponse.json(newVisit, { status: 201 })
|
||||
}
|
||||
Reference in New Issue
Block a user