"" import { NextRequest, NextResponse } from 'next/server'; import { albumDb } from '@/lib/db'; import { Album } from '@/lib/types'; // GET - Get all albums export async function GET() { try { const albums = albumDb.getAll(); return NextResponse.json(albums, { status: 200 }); } catch (error) { console.error('Error fetching albums:', error); return NextResponse.json( { error: 'Failed to fetch albums' }, { status: 500 } ); } } // POST - Create new album export async function POST(request: NextRequest) { try { const album: Album = await request.json(); // Validate required fields if (!album.id || !album.title || !album.year || !album.genre || !album.price) { return NextResponse.json( { error: 'Missing required fields' }, { status: 400 } ); } // Check if album already exists const existing = albumDb.getById(album.id); if (existing) { return NextResponse.json( { error: 'Album with this ID already exists' }, { status: 409 } ); } albumDb.create(album); return NextResponse.json(album, { status: 201 }); } catch (error) { console.error('Error creating album:', error); return NextResponse.json( { error: 'Failed to create album' }, { status: 500 } ); } } // PUT - Update album export async function PUT(request: NextRequest) { try { const album: Album = await request.json(); if (!album.id) { return NextResponse.json( { error: 'Album ID is required' }, { status: 400 } ); } // Check if album exists const existing = albumDb.getById(album.id); if (!existing) { return NextResponse.json( { error: 'Album not found' }, { status: 404 } ); } albumDb.update(album.id, album); return NextResponse.json(album, { status: 200 }); } catch (error) { console.error('Error updating album:', error); return NextResponse.json( { error: 'Failed to update album' }, { status: 500 } ); } } // DELETE - Delete album export async function DELETE(request: NextRequest) { try { const { searchParams } = new URL(request.url); const id = searchParams.get('id'); if (!id) { return NextResponse.json( { error: 'Album ID is required' }, { status: 400 } ); } // Check if album exists const existing = albumDb.getById(id); if (!existing) { return NextResponse.json( { error: 'Album not found' }, { status: 404 } ); } albumDb.delete(id); return NextResponse.json({ message: 'Album deleted successfully' }, { status: 200 }); } catch (error) { console.error('Error deleting album:', error); return NextResponse.json( { error: 'Failed to delete album' }, { status: 500 } ); } }