Change Alur Aplikasi
This commit is contained in:
63
app/api/keloladata/setting-jenis-pendaftaran/route.ts
Normal file
63
app/api/keloladata/setting-jenis-pendaftaran/route.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import supabase from '@/lib/db';
|
||||
|
||||
// GET - Fetch all unique jenis_pendaftaran values
|
||||
export async function GET() {
|
||||
try {
|
||||
// Get all unique jenis_pendaftaran values
|
||||
const { data, error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.select('jenis_pendaftaran')
|
||||
.not('jenis_pendaftaran', 'is', null)
|
||||
.order('jenis_pendaftaran');
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching jenis pendaftaran data:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
|
||||
// Get unique values
|
||||
const uniqueValues = [...new Set(data.map(item => item.jenis_pendaftaran))];
|
||||
|
||||
return NextResponse.json(uniqueValues.map(value => ({ jenis_pendaftaran: value })));
|
||||
} catch (error) {
|
||||
console.error('Error fetching jenis pendaftaran data:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - Update jenis_pendaftaran value
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { oldValue, newValue } = body;
|
||||
|
||||
// Validate required fields
|
||||
if (!oldValue || !newValue) {
|
||||
return NextResponse.json(
|
||||
{ message: 'Missing required fields: oldValue, newValue' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Update jenis_pendaftaran
|
||||
const { data, error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.update({ jenis_pendaftaran: newValue })
|
||||
.eq('jenis_pendaftaran', oldValue)
|
||||
.select();
|
||||
|
||||
if (error) {
|
||||
console.error('Error updating jenis pendaftaran:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
message: 'Jenis pendaftaran updated successfully',
|
||||
affectedRows: data?.length || 0
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error updating jenis pendaftaran:', error);
|
||||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user