Files
portaldata/app/api/keloladata/kelompok-keahlian/route.ts
Randa Firman Putra 60211ae829 testing yuk
2025-09-14 16:59:31 +07:00

215 lines
6.1 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import supabase from '@/lib/db';
// GET - Ambil semua data kelompok keahlian
export async function GET() {
try {
const { data, error } = await supabase
.from('kelompok_keahlian')
.select('id_kk, nama_kelompok')
.order('id_kk', { ascending: true });
if (error) {
console.error('Error fetching kelompok keahlian:', error);
return NextResponse.json(
{ error: 'Failed to fetch kelompok keahlian' },
{ status: 500 }
);
}
return NextResponse.json(data);
} catch (error) {
console.error('Error fetching kelompok keahlian:', error);
return NextResponse.json(
{ error: 'Failed to fetch kelompok keahlian' },
{ status: 500 }
);
}
}
// POST - Buat kelompok keahlian baru
export async function POST(request: NextRequest) {
try {
const { nama_kelompok } = await request.json();
if (!nama_kelompok || nama_kelompok.trim() === '') {
return NextResponse.json(
{ error: 'Nama kelompok keahlian is required' },
{ status: 400 }
);
}
// Cek apakah nama_kelompok sudah ada
const { data: existingData, error: existingError } = await supabase
.from('kelompok_keahlian')
.select('id_kk')
.ilike('nama_kelompok', nama_kelompok.trim());
if (existingError) {
console.error('Error checking existing kelompok keahlian:', existingError);
return NextResponse.json(
{ error: 'Failed to check existing kelompok keahlian' },
{ status: 500 }
);
}
if (existingData && existingData.length > 0) {
return NextResponse.json(
{ error: 'Kelompok keahlian dengan nama tersebut sudah ada' },
{ status: 409 }
);
}
// Insert tanpa menentukan id_kk untuk membiarkan database auto-generate
const { data, error } = await supabase
.from('kelompok_keahlian')
.insert([{ nama_kelompok: nama_kelompok.trim() }])
.select('id_kk, nama_kelompok')
.single();
if (error) {
console.error('Error creating kelompok keahlian:', error);
return NextResponse.json(
{ error: 'Failed to create kelompok keahlian' },
{ status: 500 }
);
}
return NextResponse.json(data, { status: 201 });
} catch (error) {
console.error('Error creating kelompok keahlian:', error);
return NextResponse.json(
{ error: 'Failed to create kelompok keahlian' },
{ status: 500 }
);
}
}
// PUT - Update data kelompok keahlian
export async function PUT(request: NextRequest) {
try {
const { id_kk, nama_kelompok } = await request.json();
if (!id_kk || !nama_kelompok || nama_kelompok.trim() === '') {
return NextResponse.json(
{ error: 'ID dan nama kelompok keahlian are required' },
{ status: 400 }
);
}
// Check if kelompok keahlian exists
const { data: existingData, error: existingError } = await supabase
.from('kelompok_keahlian')
.select('id_kk')
.eq('id_kk', id_kk)
.single();
if (existingError || !existingData) {
return NextResponse.json(
{ error: 'Kelompok keahlian tidak ditemukan' },
{ status: 404 }
);
}
// Cek apakah nama_kelompok sudah ada untuk record lain
const { data: duplicateData, error: duplicateError } = await supabase
.from('kelompok_keahlian')
.select('id_kk')
.ilike('nama_kelompok', nama_kelompok.trim())
.neq('id_kk', id_kk);
if (duplicateError) {
console.error('Error checking duplicate kelompok keahlian:', duplicateError);
return NextResponse.json(
{ error: 'Failed to check duplicate kelompok keahlian' },
{ status: 500 }
);
}
if (duplicateData && duplicateData.length > 0) {
return NextResponse.json(
{ error: 'Kelompok keahlian dengan nama tersebut sudah ada' },
{ status: 409 }
);
}
const { data, error } = await supabase
.from('kelompok_keahlian')
.update({ nama_kelompok: nama_kelompok.trim() })
.eq('id_kk', id_kk)
.select('id_kk, nama_kelompok')
.single();
if (error) {
console.error('Error updating kelompok keahlian:', error);
return NextResponse.json(
{ error: 'Failed to update kelompok keahlian' },
{ status: 500 }
);
}
return NextResponse.json(data);
} catch (error) {
console.error('Error updating kelompok keahlian:', error);
return NextResponse.json(
{ error: 'Failed to update kelompok keahlian' },
{ status: 500 }
);
}
}
// DELETE - Hapus kelompok keahlian
export async function DELETE(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const id_kk = searchParams.get('id_kk');
if (!id_kk) {
return NextResponse.json(
{ error: 'ID kelompok keahlian is required' },
{ status: 400 }
);
}
// Check if kelompok keahlian exists
const { data: existingData, error: existingError } = await supabase
.from('kelompok_keahlian')
.select('id_kk')
.eq('id_kk', id_kk)
.single();
if (existingError || !existingData) {
return NextResponse.json(
{ error: 'Kelompok keahlian tidak ditemukan' },
{ status: 404 }
);
}
// Cek apakah kelompok keahlian sedang digunakan di tabel lain
// Anda mungkin ingin menambahkan cek foreign key di sini tergantung struktur database
const { error } = await supabase
.from('kelompok_keahlian')
.delete()
.eq('id_kk', id_kk);
if (error) {
console.error('Error deleting kelompok keahlian:', error);
return NextResponse.json(
{ error: 'Failed to delete kelompok keahlian' },
{ status: 500 }
);
}
return NextResponse.json(
{ message: 'Kelompok keahlian berhasil dihapus' },
{ status: 200 }
);
} catch (error) {
console.error('Error deleting kelompok keahlian:', error);
return NextResponse.json(
{ error: 'Failed to delete kelompok keahlian' },
{ status: 500 }
);
}
}