IcostPro/app/api/suppliers/route.ts
2026-02-11 21:11:19 +01:00

32 lines
783 B
TypeScript

import { NextResponse } from 'next/server';
import { getDb } from '@/lib/mongodb';
export async function GET() {
const db = await getDb();
const suppliers = await db.collection('suppliers')
.find()
.sort({ name: 1 })
.toArray();
return NextResponse.json(suppliers);
}
export async function POST(request: Request) {
const body = await request.json();
const { name } = body;
if (!name) {
return NextResponse.json({ error: 'Name is required' }, { status: 400 });
}
const db = await getDb();
const now = new Date();
const result = await db.collection('suppliers').insertOne({
name,
createdAt: now,
updatedAt: now,
});
return NextResponse.json({ _id: result.insertedId, name, createdAt: now, updatedAt: now }, { status: 201 });
}