route.ts 879 B

123456789101112131415161718192021222324252627282930
  1. import { getCustomers, createCustomer } from "@/lib/db/store";
  2. export async function GET(req: Request) {
  3. const { searchParams } = new URL(req.url);
  4. const status = searchParams.get("status") || undefined;
  5. const search = searchParams.get("search") || undefined;
  6. const limit = searchParams.get("limit")
  7. ? parseInt(searchParams.get("limit")!, 10)
  8. : undefined;
  9. const sort = (searchParams.get("sort") as "newest" | "oldest") || undefined;
  10. const customerList = await getCustomers({ status, search, limit, sort });
  11. return Response.json({
  12. data: customerList,
  13. total: customerList.length,
  14. });
  15. }
  16. export async function POST(req: Request) {
  17. const body = await req.json();
  18. const customer = await createCustomer({
  19. name: body.name,
  20. email: body.email,
  21. phone: body.phone || undefined,
  22. });
  23. return Response.json(customer, { status: 201 });
  24. }