First commit
This commit is contained in:
74
app/api/mahasiswa/asal-daerah-status/route.ts
Normal file
74
app/api/mahasiswa/asal-daerah-status/route.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import pool from '@/lib/db';
|
||||
import { RowDataPacket } from 'mysql2';
|
||||
|
||||
interface AsalDaerahStatus extends RowDataPacket {
|
||||
kabupaten: string;
|
||||
tahun_angkatan?: number;
|
||||
status_kuliah: string;
|
||||
total_mahasiswa: number;
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
if (tahunAngkatan && tahunAngkatan !== 'all') {
|
||||
query += ` AND m.tahun_angkatan = ?`;
|
||||
params.push(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 [results] = await connection.query<AsalDaerahStatus[]>(query, params);
|
||||
|
||||
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 asal daerah status:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch asal daerah status data' },
|
||||
{
|
||||
status: 500,
|
||||
headers: {
|
||||
'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