77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
interface GenderData {
|
|
tahun_angkatan: number;
|
|
jk: string;
|
|
jumlah: number;
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const tahun = searchParams.get('tahun');
|
|
|
|
if (!tahun) {
|
|
return NextResponse.json(
|
|
{ error: 'Tahun angkatan is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
const { data, error } = await supabase
|
|
.from('mahasiswa')
|
|
.select('tahun_angkatan, jk')
|
|
.eq('tahun_angkatan', parseInt(tahun));
|
|
|
|
if (error) {
|
|
console.error('Error fetching gender per angkatan:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch gender per angkatan data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
// Group by jk and count
|
|
const groupedData = data.reduce((acc, item) => {
|
|
acc[item.jk] = (acc[item.jk] || 0) + 1;
|
|
return acc;
|
|
}, {} as Record<string, number>);
|
|
|
|
// Convert to final format
|
|
const results: GenderData[] = Object.entries(groupedData).map(([jk, jumlah]) => ({
|
|
tahun_angkatan: parseInt(tahun),
|
|
jk,
|
|
jumlah
|
|
}));
|
|
|
|
return NextResponse.json(results, {
|
|
headers: {
|
|
'Cache-Control': 'public, max-age=60, stale-while-revalidate=30',
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching gender per angkatan:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch gender per angkatan data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}
|