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 })
|
||||
}
|
||||
26
app/api/restaurants/[id]/visits/[visitId]/route.ts
Normal file
26
app/api/restaurants/[id]/visits/[visitId]/route.ts
Normal 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 })
|
||||
}
|
||||
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 })
|
||||
}
|
||||
24
app/api/restaurants/route.ts
Normal file
24
app/api/restaurants/route.ts
Normal 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 })
|
||||
}
|
||||
Reference in New Issue
Block a user