Change Database
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import pool from '@/lib/db';
|
||||
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);
|
||||
@@ -12,15 +18,39 @@ export async function GET(request: Request) {
|
||||
);
|
||||
}
|
||||
|
||||
const connection = await pool.getConnection();
|
||||
|
||||
try {
|
||||
const [results] = await connection.query(`
|
||||
SELECT tahun_angkatan, jk, COUNT(*) AS jumlah
|
||||
FROM mahasiswa
|
||||
WHERE tahun_angkatan = ?
|
||||
GROUP BY jk
|
||||
`, [tahun]);
|
||||
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: {
|
||||
@@ -43,7 +73,5 @@ export async function GET(request: Request) {
|
||||
},
|
||||
}
|
||||
);
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user