55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import pool from '@/lib/db';
|
|
import { RowDataPacket } from 'mysql2';
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const tahunAngkatan = searchParams.get('tahun_angkatan');
|
|
|
|
if (!tahunAngkatan) {
|
|
return NextResponse.json(
|
|
{ error: 'Tahun angkatan diperlukan' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const connection = await pool.getConnection();
|
|
|
|
try {
|
|
const query = `
|
|
SELECT
|
|
jk,
|
|
ROUND(AVG(ipk), 2) as rata_rata_ipk
|
|
FROM mahasiswa
|
|
WHERE tahun_angkatan = ?
|
|
GROUP BY jk
|
|
ORDER BY jk ASC
|
|
`;
|
|
|
|
const [results] = await connection.query<RowDataPacket[]>(query, [tahunAngkatan]);
|
|
|
|
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 IPK data:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch IPK data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
} finally {
|
|
connection.release();
|
|
}
|
|
}
|