First commit
This commit is contained in:
63
app/api/mahasiswa/status-kuliah/route.ts
Normal file
63
app/api/mahasiswa/status-kuliah/route.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import pool from '@/lib/db';
|
||||
import { RowDataPacket } from 'mysql2';
|
||||
|
||||
interface StatusKuliah extends RowDataPacket {
|
||||
tahun_angkatan: number;
|
||||
status_kuliah: string;
|
||||
jumlah: number;
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tahunAngkatan = searchParams.get('tahun_angkatan');
|
||||
|
||||
const connection = await pool.getConnection();
|
||||
|
||||
try {
|
||||
let query = `
|
||||
SELECT m.tahun_angkatan, s.status_kuliah, COUNT(*) AS jumlah
|
||||
FROM mahasiswa m
|
||||
JOIN status_mahasiswa s ON m.nim = s.nim
|
||||
WHERE s.status_kuliah IN ('Lulus', 'Cuti', 'Aktif', 'DO')
|
||||
`;
|
||||
|
||||
const params: any[] = [];
|
||||
|
||||
if (tahunAngkatan && tahunAngkatan !== 'all') {
|
||||
query += ` AND m.tahun_angkatan = ?`;
|
||||
params.push(tahunAngkatan);
|
||||
}
|
||||
|
||||
query += `
|
||||
GROUP BY m.tahun_angkatan, s.status_kuliah
|
||||
ORDER BY m.tahun_angkatan, s.status_kuliah
|
||||
`;
|
||||
|
||||
const [results] = await connection.query<StatusKuliah[]>(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 status kuliah:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch status kuliah 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