project/app/api/work-items/route.ts

58 lines
1.5 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
export const dynamic = 'force-dynamic';
const API_BASE_URL = process.env.NEXT_PUBLIC_BACKEND_API_URL || 'http://localhost:5104';
// GET /api/work-items - Get all work items
export async function GET() {
try {
const response = await fetch(`${API_BASE_URL}/api/WorkItem`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Backend API error: ${response.status}`);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Error fetching work items:', error);
return NextResponse.json(
{ error: 'Failed to fetch work items' },
{ status: 500 }
);
}
}
// POST /api/work-items - Create a new work item
export async function POST(request: NextRequest) {
try {
const body = await request.json();
debugger;
const response = await fetch(`${API_BASE_URL}/api/WorkItem`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
if (!response.ok) {
throw new Error(`Backend API error: ${response.status}`);
}
const data = await response.json();
return NextResponse.json(data, { status: 201 });
} catch (error) {
console.error('Error creating work item:', error);
return NextResponse.json(
{ error: 'Failed to create work item' },
{ status: 500 }
);
}
}