57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
|
|
/**
|
||
|
|
* Projects Data API Endpoint
|
||
|
|
* GET /api/data/projects - Query projects from PostgreSQL
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { NextRequest, NextResponse } from 'next/server';
|
||
|
|
import postgresClient from '@/lib/services/postgres-client';
|
||
|
|
|
||
|
|
export async function GET(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const searchParams = request.nextUrl.searchParams;
|
||
|
|
const limit = parseInt(searchParams.get('limit') || '100');
|
||
|
|
const offset = parseInt(searchParams.get('offset') || '0');
|
||
|
|
const companyId = searchParams.get('companyId');
|
||
|
|
const status = searchParams.get('status');
|
||
|
|
|
||
|
|
// Build where clause
|
||
|
|
const where: Record<string, any> = {};
|
||
|
|
if (companyId) {
|
||
|
|
where.company_id = parseInt(companyId);
|
||
|
|
}
|
||
|
|
if (status) {
|
||
|
|
where.status = parseInt(status);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Query projects
|
||
|
|
const projects = await postgresClient.find(
|
||
|
|
'projects',
|
||
|
|
where,
|
||
|
|
{
|
||
|
|
limit,
|
||
|
|
offset,
|
||
|
|
orderBy: 'start_date_time DESC',
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// Get total count
|
||
|
|
const totalCount = await postgresClient.count('projects', where);
|
||
|
|
|
||
|
|
return NextResponse.json({
|
||
|
|
projects,
|
||
|
|
pagination: {
|
||
|
|
limit,
|
||
|
|
offset,
|
||
|
|
total: totalCount,
|
||
|
|
hasMore: offset + projects.length < totalCount,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Failed to fetch projects:', error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: 'Failed to fetch projects' },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|