28 lines
746 B
TypeScript
28 lines
746 B
TypeScript
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 })
|
|
}
|