import { Book } from '@/books/Book';
import { NextRequest, NextResponse } from 'next/server';
import data from '@/data/books.json';

let books: Book[] = data.books as Book[];

export async function GET() {
  return NextResponse.json(books, { status: 200 });
}

export async function POST(request: NextRequest) {
  try {
    const data = await request.json();
    books.push(data);
    return NextResponse.json(data, { status: 201 });
  } catch (error) {
    return NextResponse.json(
      { error: 'Invalid request body' },
      { status: 400 },
    );
  }
}
