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

32 lines
787 B
TypeScript

import { NextResponse } from 'next/server';
import { getDb } from '@/lib/mongodb';
export async function GET() {
const db = await getDb();
const categories = await db.collection('categories')
.find()
.sort({ name: 1 })
.toArray();
return NextResponse.json(categories);
}
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('categories').insertOne({
name,
createdAt: now,
updatedAt: now,
});
return NextResponse.json({ _id: result.insertedId, name, createdAt: now, updatedAt: now }, { status: 201 });
}