Change Alur Aplikasi
This commit is contained in:
241
app/api/keloladata/data-mahasiswa/route.ts
Normal file
241
app/api/keloladata/data-mahasiswa/route.ts
Normal file
@@ -0,0 +1,241 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import supabase from '@/lib/db';
|
||||
|
||||
// GET - Fetch all mahasiswa or a specific one by NIM
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const nim = searchParams.get('nim');
|
||||
|
||||
if (nim) {
|
||||
// Fetch specific mahasiswa by NIM with joins
|
||||
const { data, error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.select(`
|
||||
*,
|
||||
kelompok_keahlian!id_kelompok_keahlian(id_kk, nama_kelompok)
|
||||
`)
|
||||
.eq('nim', nim)
|
||||
.single();
|
||||
|
||||
if (error || !data) {
|
||||
return NextResponse.json({ message: 'Mahasiswa not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Transform the data to flatten the joined fields
|
||||
const transformedData = {
|
||||
...data,
|
||||
nama_kelompok_keahlian: data.kelompok_keahlian?.nama_kelompok || null
|
||||
};
|
||||
delete transformedData.kelompok_keahlian;
|
||||
|
||||
return NextResponse.json(transformedData);
|
||||
} else {
|
||||
// Fetch all mahasiswa with joins
|
||||
const { data, error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.select(`
|
||||
*,
|
||||
kelompok_keahlian!id_kelompok_keahlian(id_kk, nama_kelompok)
|
||||
`)
|
||||
.order('nim');
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching data:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
|
||||
// Transform the data to flatten the joined fields
|
||||
const transformedData = data.map(item => ({
|
||||
...item,
|
||||
nama_kelompok_keahlian: item.kelompok_keahlian?.nama_kelompok || null
|
||||
})).map(({ kelompok_keahlian, ...rest }) => rest);
|
||||
|
||||
return NextResponse.json(transformedData);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching data:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Create a new mahasiswa
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const {
|
||||
nim,
|
||||
nama,
|
||||
jk,
|
||||
agama,
|
||||
kabupaten,
|
||||
provinsi,
|
||||
jenis_pendaftaran,
|
||||
tahun_angkatan,
|
||||
ipk,
|
||||
id_kelompok_keahlian,
|
||||
status_kuliah,
|
||||
semester
|
||||
} = body;
|
||||
|
||||
// Validate required fields
|
||||
if (!nim || !nama || !jk || !tahun_angkatan) {
|
||||
return NextResponse.json(
|
||||
{ message: 'Missing required fields: nim, nama, jk, tahun_angkatan' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if mahasiswa already exists
|
||||
const { data: existing, error: checkError } = await supabase
|
||||
.from('mahasiswa')
|
||||
.select('nim')
|
||||
.eq('nim', nim)
|
||||
.single();
|
||||
|
||||
if (existing) {
|
||||
return NextResponse.json(
|
||||
{ message: 'Mahasiswa with this NIM already exists' },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Insert new mahasiswa
|
||||
const { data, error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.insert({
|
||||
nim,
|
||||
nama,
|
||||
jk,
|
||||
agama: agama || null,
|
||||
kabupaten: kabupaten || null,
|
||||
provinsi: provinsi || null,
|
||||
jenis_pendaftaran: jenis_pendaftaran || null,
|
||||
tahun_angkatan,
|
||||
ipk: ipk || null,
|
||||
id_kelompok_keahlian: id_kelompok_keahlian || null,
|
||||
status_kuliah: status_kuliah || "Aktif",
|
||||
semester: semester || 1
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('Error creating mahasiswa:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Mahasiswa created successfully', nim },
|
||||
{ status: 201 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error creating mahasiswa:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - Update an existing mahasiswa
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const nim = searchParams.get('nim');
|
||||
|
||||
if (!nim) {
|
||||
return NextResponse.json({ message: 'NIM is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const {
|
||||
nama,
|
||||
jk,
|
||||
agama,
|
||||
kabupaten,
|
||||
provinsi,
|
||||
jenis_pendaftaran,
|
||||
tahun_angkatan,
|
||||
ipk,
|
||||
id_kelompok_keahlian,
|
||||
status_kuliah,
|
||||
semester
|
||||
} = body;
|
||||
|
||||
// Check if mahasiswa exists
|
||||
const { data: existing, error: checkError } = await supabase
|
||||
.from('mahasiswa')
|
||||
.select('*')
|
||||
.eq('nim', nim)
|
||||
.single();
|
||||
|
||||
if (checkError || !existing) {
|
||||
return NextResponse.json({ message: 'Mahasiswa not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Update mahasiswa
|
||||
const { error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.update({
|
||||
nama: nama || existing.nama,
|
||||
jk: jk || existing.jk,
|
||||
agama: agama || existing.agama,
|
||||
kabupaten: kabupaten || existing.kabupaten,
|
||||
provinsi: provinsi || existing.provinsi,
|
||||
jenis_pendaftaran: jenis_pendaftaran || existing.jenis_pendaftaran,
|
||||
tahun_angkatan: tahun_angkatan || existing.tahun_angkatan,
|
||||
ipk: ipk || existing.ipk,
|
||||
id_kelompok_keahlian: id_kelompok_keahlian || existing.id_kelompok_keahlian,
|
||||
status_kuliah: status_kuliah || existing.status_kuliah,
|
||||
semester: semester || existing.semester
|
||||
})
|
||||
.eq('nim', nim);
|
||||
|
||||
if (error) {
|
||||
console.error('Error updating mahasiswa:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: 'Mahasiswa updated successfully', nim });
|
||||
} catch (error) {
|
||||
console.error('Error updating mahasiswa:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Delete a mahasiswa
|
||||
export async function DELETE(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const nim = searchParams.get('nim');
|
||||
|
||||
if (!nim) {
|
||||
return NextResponse.json({ message: 'NIM is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Check if mahasiswa exists
|
||||
const { data: existing, error: checkError } = await supabase
|
||||
.from('mahasiswa')
|
||||
.select('nim')
|
||||
.eq('nim', nim)
|
||||
.single();
|
||||
|
||||
if (checkError || !existing) {
|
||||
return NextResponse.json({ message: 'Mahasiswa not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Delete mahasiswa
|
||||
const { error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.delete()
|
||||
.eq('nim', nim);
|
||||
|
||||
if (error) {
|
||||
console.error('Error deleting mahasiswa:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: 'Mahasiswa deleted successfully' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting mahasiswa:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user