Change Database
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import pool from '@/lib/db';
|
||||
import { RowDataPacket } from 'mysql2';
|
||||
import supabase from '@/lib/db';
|
||||
|
||||
interface AsalDaerahStatus extends RowDataPacket {
|
||||
interface AsalDaerahStatus {
|
||||
kabupaten: string;
|
||||
tahun_angkatan?: number;
|
||||
status_kuliah: string;
|
||||
@@ -13,39 +12,78 @@ export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tahunAngkatan = searchParams.get('tahun_angkatan');
|
||||
const statusKuliah = searchParams.get('status_kuliah');
|
||||
|
||||
const connection = await pool.getConnection();
|
||||
|
||||
try {
|
||||
let query = `
|
||||
SELECT
|
||||
m.kabupaten,
|
||||
${tahunAngkatan && tahunAngkatan !== 'all' ? 'm.tahun_angkatan,' : ''}
|
||||
s.status_kuliah,
|
||||
COUNT(m.nim) AS total_mahasiswa
|
||||
FROM
|
||||
mahasiswa m
|
||||
JOIN
|
||||
status_mahasiswa s ON m.nim = s.nim
|
||||
WHERE
|
||||
s.status_kuliah = ?
|
||||
`;
|
||||
|
||||
const params: any[] = [statusKuliah];
|
||||
try {
|
||||
let query = supabase
|
||||
.from('status_mahasiswa')
|
||||
.select('status_kuliah, mahasiswa!inner(kabupaten, tahun_angkatan, nim)')
|
||||
.eq('status_kuliah', statusKuliah);
|
||||
|
||||
if (tahunAngkatan && tahunAngkatan !== 'all') {
|
||||
query += ` AND m.tahun_angkatan = ?`;
|
||||
params.push(tahunAngkatan);
|
||||
query = query.eq('mahasiswa.tahun_angkatan', parseInt(tahunAngkatan));
|
||||
}
|
||||
|
||||
query += `
|
||||
GROUP BY
|
||||
m.kabupaten${tahunAngkatan && tahunAngkatan !== 'all' ? ', m.tahun_angkatan' : ''}, s.status_kuliah
|
||||
ORDER BY
|
||||
${tahunAngkatan && tahunAngkatan !== 'all' ? 'm.tahun_angkatan ASC,' : ''} m.kabupaten, s.status_kuliah
|
||||
`;
|
||||
const { data, error } = await query;
|
||||
|
||||
const [results] = await connection.query<AsalDaerahStatus[]>(query, params);
|
||||
if (error) {
|
||||
console.error('Error fetching asal daerah status:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch asal daerah status data' },
|
||||
{
|
||||
status: 500,
|
||||
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',
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Group by kabupaten, tahun_angkatan (optional), status_kuliah
|
||||
const groupedData = data.reduce((acc, item: any) => {
|
||||
const kabupaten = item.mahasiswa.kabupaten;
|
||||
const tahun_angkatan = tahunAngkatan && tahunAngkatan !== 'all' ? item.mahasiswa.tahun_angkatan : undefined;
|
||||
const status_kuliah = item.status_kuliah;
|
||||
const key = tahun_angkatan !== undefined
|
||||
? `${kabupaten}-${tahun_angkatan}-${status_kuliah}`
|
||||
: `${kabupaten}-${status_kuliah}`;
|
||||
acc[key] = (acc[key] || 0) + 1;
|
||||
return acc;
|
||||
}, {} as Record<string, number>);
|
||||
|
||||
// Convert to final format and sort
|
||||
const results: AsalDaerahStatus[] = Object.entries(groupedData)
|
||||
.map(([key, total_mahasiswa]) => {
|
||||
const parts = key.split('-');
|
||||
if (tahunAngkatan && tahunAngkatan !== 'all') {
|
||||
const [kabupaten, tahun_angkatan, status_kuliah] = parts;
|
||||
return {
|
||||
kabupaten,
|
||||
tahun_angkatan: parseInt(tahun_angkatan),
|
||||
status_kuliah,
|
||||
total_mahasiswa
|
||||
};
|
||||
} else {
|
||||
const [kabupaten, status_kuliah] = parts;
|
||||
return {
|
||||
kabupaten,
|
||||
status_kuliah,
|
||||
total_mahasiswa
|
||||
};
|
||||
}
|
||||
})
|
||||
.sort((a, b) => {
|
||||
// Sort by tahun_angkatan ASC (if exists), kabupaten ASC, status_kuliah ASC
|
||||
if (a.tahun_angkatan !== undefined && b.tahun_angkatan !== undefined && a.tahun_angkatan !== b.tahun_angkatan) {
|
||||
return a.tahun_angkatan - b.tahun_angkatan;
|
||||
}
|
||||
if (a.kabupaten !== b.kabupaten) {
|
||||
return a.kabupaten.localeCompare(b.kabupaten);
|
||||
}
|
||||
return a.status_kuliah.localeCompare(b.status_kuliah);
|
||||
});
|
||||
|
||||
return NextResponse.json(results, {
|
||||
headers: {
|
||||
@@ -59,16 +97,15 @@ export async function GET(request: Request) {
|
||||
console.error('Error fetching asal daerah status:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch asal daerah status data' },
|
||||
{
|
||||
{
|
||||
status: 500,
|
||||
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',
|
||||
},
|
||||
}
|
||||
);
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user