32 lines
787 B
TypeScript
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 });
|
|
}
|