Change Database
This commit is contained in:
@@ -1,21 +1,49 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import pool from '@/lib/db';
|
||||
import { RowDataPacket } from 'mysql2';
|
||||
import supabase from '@/lib/db';
|
||||
|
||||
interface IPKData extends RowDataPacket {
|
||||
interface IPKData {
|
||||
tahun_angkatan: number;
|
||||
rata_rata_ipk: number;
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const connection = await pool.getConnection();
|
||||
|
||||
try {
|
||||
const [results] = await connection.query<IPKData[]>(`
|
||||
SELECT tahun_angkatan, ROUND(AVG(ipk), 2) AS rata_rata_ipk
|
||||
FROM mahasiswa
|
||||
GROUP BY tahun_angkatan
|
||||
`);
|
||||
const { data, error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.select('tahun_angkatan, ipk')
|
||||
.not('ipk', 'is', null);
|
||||
|
||||
if (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',
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Group by tahun_angkatan and calculate average IPK
|
||||
const groupedData = data.reduce((acc, item) => {
|
||||
const tahun = item.tahun_angkatan;
|
||||
if (!acc[tahun]) {
|
||||
acc[tahun] = { sum: 0, count: 0 };
|
||||
}
|
||||
acc[tahun].sum += item.ipk || 0;
|
||||
acc[tahun].count += 1;
|
||||
return acc;
|
||||
}, {} as Record<number, { sum: number; count: number }>);
|
||||
|
||||
// Convert to final format
|
||||
const results: IPKData[] = Object.entries(groupedData).map(([tahun, data]) => ({
|
||||
tahun_angkatan: parseInt(tahun),
|
||||
rata_rata_ipk: Math.round((data.sum / data.count) * 100) / 100
|
||||
}));
|
||||
|
||||
return NextResponse.json(results, {
|
||||
headers: {
|
||||
@@ -38,7 +66,5 @@ export async function GET() {
|
||||
},
|
||||
}
|
||||
);
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user