336 lines
10 KiB
TypeScript
336 lines
10 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
// GET - Fetch all prestasi mahasiswa or filter by criteria
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const id = searchParams.get('id');
|
|
const id_mahasiswa = searchParams.get('id_mahasiswa');
|
|
const search = searchParams.get('search');
|
|
const jenisPrestasi = searchParams.get('jenis_prestasi');
|
|
const tingkat = searchParams.get('tingkat');
|
|
|
|
// If ID is provided, fetch specific prestasi by ID with join
|
|
if (id) {
|
|
const { data, error } = await supabase
|
|
.from('prestasi_mahasiswa')
|
|
.select(`
|
|
*,
|
|
mahasiswa!inner(nama, nim)
|
|
`)
|
|
.eq('id_prestasi', id)
|
|
.single();
|
|
|
|
if (error || !data) {
|
|
return NextResponse.json({ message: 'Prestasi mahasiswa not found' }, { status: 404 });
|
|
}
|
|
|
|
// Transform the data to flatten the nama and nim fields
|
|
const transformedData = {
|
|
...data,
|
|
nama: data.mahasiswa.nama,
|
|
nim: data.mahasiswa.nim
|
|
};
|
|
delete transformedData.mahasiswa;
|
|
|
|
return NextResponse.json(transformedData);
|
|
}
|
|
|
|
// If id_mahasiswa is provided, fetch prestasi for specific student with join
|
|
if (id_mahasiswa) {
|
|
const { data, error } = await supabase
|
|
.from('prestasi_mahasiswa')
|
|
.select(`
|
|
*,
|
|
mahasiswa!inner(nama, nim)
|
|
`)
|
|
.eq('id_mahasiswa', id_mahasiswa)
|
|
.order('tanggal_prestasi', { ascending: false });
|
|
|
|
if (error) {
|
|
console.error('Error fetching data:', error);
|
|
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
|
|
// Transform the data to flatten the nama and nim fields
|
|
const transformedData = data.map(item => ({
|
|
...item,
|
|
nama: item.mahasiswa.nama,
|
|
nim: item.mahasiswa.nim
|
|
})).map(({ mahasiswa, ...rest }) => rest);
|
|
|
|
return NextResponse.json(transformedData);
|
|
}
|
|
|
|
// Build the query based on filters with join
|
|
let query = supabase.from('prestasi_mahasiswa').select(`
|
|
*,
|
|
mahasiswa!inner(nama, nim)
|
|
`);
|
|
|
|
// Add search condition if provided
|
|
if (search) {
|
|
query = query.or(`mahasiswa.nama.ilike.%${search}%,mahasiswa.nim.ilike.%${search}%,nama_prestasi.ilike.%${search}%,peringkat.ilike.%${search}%,keterangan.ilike.%${search}%`);
|
|
}
|
|
|
|
// Add jenis_prestasi filter if provided
|
|
if (jenisPrestasi) {
|
|
query = query.eq('jenis_prestasi', jenisPrestasi);
|
|
}
|
|
|
|
// Add tingkat filter if provided
|
|
if (tingkat) {
|
|
query = query.eq('tingkat_prestasi', tingkat);
|
|
}
|
|
|
|
// Add order by
|
|
query = query.order('tanggal_prestasi', { ascending: false });
|
|
|
|
// Execute the query
|
|
const { data, error } = await query;
|
|
|
|
if (error) {
|
|
console.error('Error fetching data:', error);
|
|
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
|
|
// Transform the data to flatten the nama and nim fields
|
|
const transformedData = data.map(item => ({
|
|
...item,
|
|
nama: item.mahasiswa.nama,
|
|
nim: item.mahasiswa.nim
|
|
})).map(({ mahasiswa, ...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 prestasi mahasiswa
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const {
|
|
nim,
|
|
jenis_prestasi,
|
|
nama_prestasi,
|
|
tingkat_prestasi,
|
|
peringkat,
|
|
tanggal_prestasi,
|
|
keterangan
|
|
} = body;
|
|
|
|
// Validate required fields
|
|
if (!nim || !jenis_prestasi || !nama_prestasi || !tingkat_prestasi || !peringkat || !tanggal_prestasi) {
|
|
return NextResponse.json(
|
|
{ message: 'Missing required fields: nim, jenis_prestasi, nama_prestasi, tingkat_prestasi, peringkat, tanggal_prestasi' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Check if mahasiswa exists by NIM and get id_mahasiswa
|
|
const { data: mahasiswaExists, error: checkError } = await supabase
|
|
.from('mahasiswa')
|
|
.select('id_mahasiswa, nama')
|
|
.eq('nim', nim)
|
|
.single();
|
|
|
|
if (checkError || !mahasiswaExists) {
|
|
return NextResponse.json(
|
|
{ message: `Mahasiswa dengan NIM ${nim} tidak terdaftar` },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Validate enum values
|
|
const validJenisPrestasi = ['Akademik', 'Non-Akademik'];
|
|
const validTingkat = ['Kabupaten', 'Provinsi', 'Nasional', 'Internasional'];
|
|
|
|
if (!validJenisPrestasi.includes(jenis_prestasi)) {
|
|
return NextResponse.json(
|
|
{ message: 'Invalid jenis_prestasi value. Must be one of: Akademik, Non-Akademik' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (!validTingkat.includes(tingkat_prestasi)) {
|
|
return NextResponse.json(
|
|
{ message: 'Invalid tingkat_prestasi value. Must be one of: Kabupaten, Provinsi, Nasional, Internasional' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Insert new prestasi using id_mahasiswa
|
|
const { data, error } = await supabase
|
|
.from('prestasi_mahasiswa')
|
|
.insert({
|
|
id_mahasiswa: mahasiswaExists.id_mahasiswa,
|
|
jenis_prestasi,
|
|
nama_prestasi,
|
|
tingkat_prestasi,
|
|
peringkat,
|
|
tanggal_prestasi,
|
|
keterangan: keterangan || null
|
|
})
|
|
.select()
|
|
.single();
|
|
|
|
if (error) {
|
|
console.error('Error creating prestasi mahasiswa:', error);
|
|
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
|
|
return NextResponse.json(
|
|
{
|
|
message: `Prestasi berhasil ditambahkan`,
|
|
id: data.id_prestasi
|
|
},
|
|
{ status: 201 }
|
|
);
|
|
} catch (error) {
|
|
console.error('Error creating prestasi mahasiswa:', error);
|
|
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// PUT - Update an existing prestasi mahasiswa
|
|
export async function PUT(request: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const id = searchParams.get('id');
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ message: 'ID is required' }, { status: 400 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const {
|
|
nim,
|
|
jenis_prestasi,
|
|
nama_prestasi,
|
|
tingkat_prestasi,
|
|
peringkat,
|
|
tanggal_prestasi,
|
|
keterangan
|
|
} = body;
|
|
|
|
// Validate required fields
|
|
if (!nim || !jenis_prestasi || !nama_prestasi || !tingkat_prestasi || !peringkat || !tanggal_prestasi) {
|
|
return NextResponse.json(
|
|
{ message: 'Missing required fields: nim, jenis_prestasi, nama_prestasi, tingkat_prestasi, peringkat, tanggal_prestasi' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Check if prestasi exists
|
|
const { data: existing, error: checkError } = await supabase
|
|
.from('prestasi_mahasiswa')
|
|
.select('*')
|
|
.eq('id_prestasi', id)
|
|
.single();
|
|
|
|
if (checkError || !existing) {
|
|
return NextResponse.json({ message: 'Prestasi mahasiswa not found' }, { status: 404 });
|
|
}
|
|
|
|
// Check if mahasiswa exists by NIM and get id_mahasiswa
|
|
const { data: mahasiswaExists, error: mahasiswaCheckError } = await supabase
|
|
.from('mahasiswa')
|
|
.select('id_mahasiswa, nama')
|
|
.eq('nim', nim)
|
|
.single();
|
|
|
|
if (mahasiswaCheckError || !mahasiswaExists) {
|
|
return NextResponse.json(
|
|
{ message: `Mahasiswa dengan NIM ${nim} tidak terdaftar` },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Validate enum values
|
|
const validJenisPrestasi = ['Akademik', 'Non-Akademik'];
|
|
const validTingkat = ['Kabupaten', 'Provinsi', 'Nasional', 'Internasional'];
|
|
|
|
if (!validJenisPrestasi.includes(jenis_prestasi)) {
|
|
return NextResponse.json(
|
|
{ message: 'Invalid jenis_prestasi value. Must be one of: Akademik, Non-Akademik' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (!validTingkat.includes(tingkat_prestasi)) {
|
|
return NextResponse.json(
|
|
{ message: 'Invalid tingkat_prestasi value. Must be one of: Kabupaten, Provinsi, Nasional, Internasional' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Update prestasi using id_mahasiswa
|
|
const { error } = await supabase
|
|
.from('prestasi_mahasiswa')
|
|
.update({
|
|
id_mahasiswa: mahasiswaExists.id_mahasiswa,
|
|
jenis_prestasi,
|
|
nama_prestasi,
|
|
tingkat_prestasi,
|
|
peringkat,
|
|
tanggal_prestasi,
|
|
keterangan: keterangan || null
|
|
})
|
|
.eq('id_prestasi', id);
|
|
|
|
if (error) {
|
|
console.error('Error updating prestasi mahasiswa:', error);
|
|
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
|
|
return NextResponse.json({
|
|
message: `Prestasi berhasil diperbarui`
|
|
});
|
|
} catch (error) {
|
|
console.error('Error updating prestasi mahasiswa:', error);
|
|
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// DELETE - Delete a prestasi mahasiswa
|
|
export async function DELETE(request: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const id = searchParams.get('id');
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ message: 'ID is required' }, { status: 400 });
|
|
}
|
|
|
|
// Check if prestasi exists
|
|
const { data: existing, error: checkError } = await supabase
|
|
.from('prestasi_mahasiswa')
|
|
.select('id_prestasi')
|
|
.eq('id_prestasi', id)
|
|
.single();
|
|
|
|
if (checkError || !existing) {
|
|
return NextResponse.json({ message: 'Prestasi mahasiswa not found' }, { status: 404 });
|
|
}
|
|
|
|
// Delete prestasi
|
|
const { error } = await supabase
|
|
.from('prestasi_mahasiswa')
|
|
.delete()
|
|
.eq('id_prestasi', id);
|
|
|
|
if (error) {
|
|
console.error('Error deleting prestasi mahasiswa:', error);
|
|
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
|
|
return NextResponse.json({ message: 'Prestasi mahasiswa deleted successfully' });
|
|
} catch (error) {
|
|
console.error('Error deleting prestasi mahasiswa:', error);
|
|
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|