import { NextRequest, NextResponse } from 'next/server'; import { getDataSource, Customer } from '@/lib/database'; // GET /api/customers - Get all customers export async function GET(request: NextRequest) { try { const dataSource = await getDataSource(); const customerRepository = dataSource.getRepository(Customer); const customers = await customerRepository.find({ order: { createdAt: 'DESC' } }); return NextResponse.json(customers); } catch (error) { console.error('Error fetching customers:', error); return NextResponse.json( { error: 'Failed to fetch customers' }, { status: 500 } ); } } // POST /api/customers - Create a new customer export async function POST(request: NextRequest) { try { const dataSource = await getDataSource(); const customerRepository = dataSource.getRepository(Customer); const data = await request.json(); const { name, url, email } = data; // Validate required fields if (!name || !email) { return NextResponse.json( { error: 'Name and email are required' }, { status: 400 } ); } // Create and save the new customer const customer = new Customer(); customer.name = name; customer.url = url || ''; customer.email = email; const savedCustomer = await customerRepository.save(customer); return NextResponse.json(savedCustomer, { status: 201 }); } catch (error) { console.error('Error creating customer:', error); return NextResponse.json( { error: 'Failed to create customer' }, { status: 500 } ); } }