Change Database
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import pool from '@/lib/db';
|
||||
import { RowDataPacket } from 'mysql2';
|
||||
import supabase from '@/lib/db';
|
||||
import { cookies } from 'next/headers';
|
||||
import { jwtVerify } from 'jose';
|
||||
|
||||
interface MahasiswaProfile extends RowDataPacket {
|
||||
interface MahasiswaProfile {
|
||||
nim: string;
|
||||
nama: string;
|
||||
jk: 'Pria' | 'Wanita';
|
||||
@@ -20,7 +19,6 @@ interface MahasiswaProfile extends RowDataPacket {
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
let connection;
|
||||
try {
|
||||
// Get token from cookies
|
||||
const cookieStore = await cookies();
|
||||
@@ -41,47 +39,57 @@ export async function GET(request: Request) {
|
||||
|
||||
const nim = payload.nim as string;
|
||||
|
||||
// Get connection from pool
|
||||
connection = await pool.getConnection();
|
||||
// Get mahasiswa data
|
||||
const { data: mahasiswaData, error: mahasiswaError } = await supabase
|
||||
.from('mahasiswa')
|
||||
.select(`
|
||||
nim,
|
||||
nama,
|
||||
jk,
|
||||
agama,
|
||||
kabupaten,
|
||||
provinsi,
|
||||
jenis_pendaftaran,
|
||||
status_beasiswa,
|
||||
tahun_angkatan,
|
||||
ipk,
|
||||
prestasi
|
||||
`)
|
||||
.eq('nim', nim)
|
||||
.single();
|
||||
|
||||
const query = `
|
||||
SELECT
|
||||
m.nim,
|
||||
m.nama,
|
||||
m.jk,
|
||||
m.agama,
|
||||
m.kabupaten,
|
||||
m.provinsi,
|
||||
m.jenis_pendaftaran,
|
||||
m.status_beasiswa,
|
||||
m.tahun_angkatan,
|
||||
m.ipk,
|
||||
m.prestasi,
|
||||
s.status_kuliah
|
||||
FROM
|
||||
mahasiswa m
|
||||
LEFT JOIN
|
||||
status_mahasiswa s ON m.nim = s.nim
|
||||
WHERE
|
||||
m.nim = ?
|
||||
`;
|
||||
|
||||
const [rows] = await connection.query<MahasiswaProfile[]>(query, [nim]);
|
||||
|
||||
if (rows.length === 0) {
|
||||
connection.release();
|
||||
if (mahasiswaError || !mahasiswaData) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Data mahasiswa tidak ditemukan' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
connection.release();
|
||||
return NextResponse.json(rows[0]);
|
||||
// Get status_kuliah separately
|
||||
const { data: statusData, error: statusError } = await supabase
|
||||
.from('status_mahasiswa')
|
||||
.select('status_kuliah')
|
||||
.eq('nim', nim)
|
||||
.single();
|
||||
|
||||
// Transform the data to match the expected interface
|
||||
const profile: MahasiswaProfile = {
|
||||
nim: mahasiswaData.nim,
|
||||
nama: mahasiswaData.nama,
|
||||
jk: mahasiswaData.jk,
|
||||
agama: mahasiswaData.agama,
|
||||
kabupaten: mahasiswaData.kabupaten,
|
||||
provinsi: mahasiswaData.provinsi,
|
||||
jenis_pendaftaran: mahasiswaData.jenis_pendaftaran,
|
||||
status_beasiswa: mahasiswaData.status_beasiswa,
|
||||
tahun_angkatan: mahasiswaData.tahun_angkatan,
|
||||
ipk: mahasiswaData.ipk,
|
||||
prestasi: mahasiswaData.prestasi,
|
||||
status_kuliah: statusData?.status_kuliah || ''
|
||||
};
|
||||
|
||||
return NextResponse.json(profile);
|
||||
} catch (error) {
|
||||
if (connection) {
|
||||
connection.release();
|
||||
}
|
||||
console.error('Error fetching profile data:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal Server Error' },
|
||||
|
||||
Reference in New Issue
Block a user