add customer
This commit is contained in:
119
src/app/api/customers/[id]/route.ts
Normal file
119
src/app/api/customers/[id]/route.ts
Normal file
@ -0,0 +1,119 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getDataSource, Customer } from '@/lib/database';
|
||||
|
||||
// GET /api/customers/[id] - Get a specific customer
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
props: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await props.params;
|
||||
const dataSource = await getDataSource();
|
||||
const customerRepository = dataSource.getRepository(Customer);
|
||||
|
||||
const customer = await customerRepository.findOne({
|
||||
where: { id: id }
|
||||
});
|
||||
|
||||
if (!customer) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Customer not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(customer);
|
||||
} catch (error) {
|
||||
console.error('Error fetching customer:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch customer' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT /api/customers/[id] - Update a customer
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
props: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await props.params;
|
||||
const dataSource = await getDataSource();
|
||||
const customerRepository = dataSource.getRepository(Customer);
|
||||
|
||||
// Find the customer to update
|
||||
const customer = await customerRepository.findOne({
|
||||
where: { id: id }
|
||||
});
|
||||
|
||||
if (!customer) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Customer not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
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 }
|
||||
);
|
||||
}
|
||||
|
||||
// Update customer fields
|
||||
customer.name = name;
|
||||
customer.url = url || '';
|
||||
customer.email = email;
|
||||
|
||||
// Save the updated customer
|
||||
const updatedCustomer = await customerRepository.save(customer);
|
||||
|
||||
return NextResponse.json(updatedCustomer);
|
||||
} catch (error) {
|
||||
console.error('Error updating customer:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update customer' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/customers/[id] - Delete a customer
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
props: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await props.params;
|
||||
const dataSource = await getDataSource();
|
||||
const customerRepository = dataSource.getRepository(Customer);
|
||||
|
||||
// Find the customer to delete
|
||||
const customer = await customerRepository.findOne({
|
||||
where: { id: id }
|
||||
});
|
||||
|
||||
if (!customer) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Customer not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Delete the customer
|
||||
await customerRepository.remove(customer);
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error deleting customer:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete customer' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user