-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Expand file tree
/
Copy pathroute.ts
More file actions
29 lines (25 loc) · 950 Bytes
/
route.ts
File metadata and controls
29 lines (25 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { NextRequest, NextResponse } from 'next/server';
import { Product, products } from './db'; // Import from db.ts
// GET /api/products - List all products
export async function GET(request: NextRequest) {
return NextResponse.json(products);
}
// POST /api/products - Create a new product
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { name, price } = body;
if (!name || typeof price !== 'number') {
return NextResponse.json({ error: 'Name and price are required' }, { status: 400 });
}
const newProduct: Product = {
id: String(products.length + 1), // Simple ID generation (consider a more robust solution for real apps)
name,
price,
};
products.push(newProduct);
return NextResponse.json(newProduct, { status: 201 });
} catch (error) {
return NextResponse.json({ error: 'Invalid request body' }, { status: 400 });
}
}