Change Database

This commit is contained in:
Randa Firman Putra
2025-06-20 00:45:19 +07:00
parent e028039ee2
commit 2f7ab6c0a9
45 changed files with 1896 additions and 953 deletions

View File

@@ -1,9 +1,8 @@
import { NextResponse } from 'next/server';
import pool from '@/lib/db';
import supabase from '@/lib/db';
import bcrypt from 'bcryptjs';
export async function POST(request: Request) {
let connection;
try {
const { username, nim, password } = await request.json();
@@ -23,31 +22,28 @@ export async function POST(request: Request) {
);
}
// Get connection from pool
connection = await pool.getConnection();
// Check if NIM exists in mahasiswa table
const [mahasiswa]: any = await connection.execute(
'SELECT * FROM mahasiswa WHERE nim = ?',
[nim]
);
const { data: mahasiswa, error: mahasiswaError } = await supabase
.from('mahasiswa')
.select('nim')
.eq('nim', nim)
.single();
if (mahasiswa.length === 0) {
connection.release();
if (mahasiswaError || !mahasiswa) {
return NextResponse.json(
{ error: 'NIM tidak terdaftar sebagai mahasiswa' },
{ status: 400 }
);
}
// Check if NIM already exists in user table
const [existingUsers]: any = await connection.execute(
'SELECT * FROM user WHERE nim = ?',
[nim]
);
// Check if NIM already exists in user_app table
const { data: existingUsers, error: userError } = await supabase
.from('user_app')
.select('nim')
.eq('nim', nim)
.single();
if (existingUsers.length > 0) {
connection.release();
if (!userError && existingUsers) {
return NextResponse.json(
{ error: 'NIM sudah terdaftar sebagai pengguna' },
{ status: 400 }
@@ -58,22 +54,30 @@ export async function POST(request: Request) {
const hashedPassword = await bcrypt.hash(password, 10);
// Insert new user
await connection.execute(
'INSERT INTO user (nim, username, password, role, created_at, updated_at) VALUES (?, ?, ?, ?, NOW(), NOW())',
[nim, username, hashedPassword, 'mahasiswa']
);
const { data: newUser, error: insertError } = await supabase
.from('user_app')
.insert({
nim: nim,
username: username,
password: hashedPassword,
role: 'mahasiswa'
})
.select()
.single();
connection.release();
if (insertError) {
console.error('Insert error:', insertError);
return NextResponse.json(
{ error: 'Terjadi kesalahan saat registrasi' },
{ status: 500 }
);
}
return NextResponse.json(
{ message: 'Registrasi berhasil' },
{ status: 201 }
);
} catch (error) {
if (connection) {
connection.release();
}
console.error('Registration error:', error);
return NextResponse.json(
{ error: 'Terjadi kesalahan saat registrasi' },