Initial food blog app

This commit is contained in:
Andy
2026-03-21 11:57:08 +00:00
commit b83762bfc3
33 changed files with 8621 additions and 0 deletions

View 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 })
}

View File

@@ -0,0 +1,26 @@
import { NextResponse } from 'next/server'
import { readRestaurants, writeRestaurants } from '@/app/lib/restaurants'
export async function DELETE(
_request: Request,
{ params }: { params: Promise<{ id: string; visitId: string }> }
) {
const { id, visitId } = await params
const restaurants = readRestaurants()
const index = restaurants.findIndex((r) => r.id === id)
if (index === -1) {
return NextResponse.json({ error: 'Restaurant not found' }, { status: 404 })
}
const visitIndex = restaurants[index].visits.findIndex((v) => v.id === visitId)
if (visitIndex === -1) {
return NextResponse.json({ error: 'Visit not found' }, { status: 404 })
}
restaurants[index].visits.splice(visitIndex, 1)
writeRestaurants(restaurants)
return NextResponse.json({ success: true })
}

View 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 })
}

View File

@@ -0,0 +1,24 @@
import { NextResponse } from 'next/server'
import { readRestaurants, writeRestaurants } from '@/app/lib/restaurants'
import { Restaurant } from '@/app/types'
export async function GET() {
const restaurants = readRestaurants()
return NextResponse.json(restaurants)
}
export async function POST(request: Request) {
const body = await request.json() as Omit<Restaurant, 'id' | 'visits'>
const restaurants = readRestaurants()
const newRestaurant: Restaurant = {
...body,
id: crypto.randomUUID(),
visits: [],
}
restaurants.push(newRestaurant)
writeRestaurants(restaurants)
return NextResponse.json(newRestaurant, { status: 201 })
}