Fix UI Admin Menu Pendidikam, Add Menu User & Role

This commit is contained in:
2025-09-02 18:08:53 +08:00
parent 7ae83788b4
commit fa9601e126
86 changed files with 5349 additions and 2649 deletions

View File

@@ -10,9 +10,6 @@ async function lembagaPendidikanFindMany(context: Context) {
const search = (context.query.search as string) || "";
const skip = (page - 1) * limit;
const jenjangPendidikanName = (context.query.jenjangPendidikanId as string) || "";
console.log('Lembaga API Query Params:', { page, limit, search, jenjangPendidikanName });
// Buat where clause
const where: any = { isActive: true };

View File

@@ -10,8 +10,6 @@ async function pengajarFindMany(context: Context) {
const search = (context.query.search as string) || "";
const jenjangPendidikanName = (context.query.jenjangPendidikanId as string) || "";
console.log('Pengajar API Query Params:', { page, limit, search, jenjangPendidikanId: jenjangPendidikanName });
const where: any = { isActive: true };
// Filter berdasarkan jenjang pendidikan (jika ada)

View File

@@ -10,8 +10,6 @@ async function siswaFindMany(context: Context) {
const search = (context.query.search as string) || "";
const jenjangPendidikanName = (context.query.jenjangPendidikanName as string) || "";
console.log('Siswa API Query Params:', { page, limit, search, jenjangPendidikanId: jenjangPendidikanName });
// Buat where clause
const where: any = { isActive: true };

View File

@@ -1,11 +1,53 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// /api/berita/findManyPaginated.ts
import prisma from "@/lib/prisma";
import { Context } from "elysia";
export default async function kategoriBukuFindMany() {
const data = await prisma.kategoriBuku.findMany();
async function kategoriBukuFindMany(context: Context) {
// Ambil parameter dari query
const page = Number(context.query.page) || 1;
const limit = Number(context.query.limit) || 10;
const search = (context.query.search as string) || '';
const skip = (page - 1) * limit;
return {
success: true,
message: "Success get all kategori buku",
data,
};
// Buat where clause
const where: any = { isActive: true };
// Tambahkan pencarian (jika ada)
if (search) {
where.OR = [
{ name: { contains: search, mode: 'insensitive' } },
];
}
try {
// Ambil data dan total count secara paralel
const [data, total] = await Promise.all([
prisma.kategoriBuku.findMany({
where,
skip,
take: limit,
orderBy: { createdAt: 'desc' },
}),
prisma.kategoriBuku.count({ where }),
]);
return {
success: true,
message: "Berhasil ambil kategori buku dengan pagination",
data,
page,
limit,
total,
totalPages: Math.ceil(total / limit),
};
} catch (e) {
console.error("Error di findMany paginated:", e);
return {
success: false,
message: "Gagal mengambil data kategori buku",
};
}
}
export default kategoriBukuFindMany;

View File

@@ -1,51 +0,0 @@
import { Context } from "elysia";
import prisma from "@/lib/prisma";
import bcrypt from "bcryptjs";
type FormCreateUser = {
nama: string;
email: string;
password: string;
roleId: string;
isActive?: boolean;
};
export default async function userCreate(context: Context) {
const body = (await context.body) as FormCreateUser;
if (!body.nama || !body.email || !body.password || !body.roleId) {
throw new Error("Semua field wajib diisi");
}
try {
// Cek apakah email sudah terdaftar
const existing = await prisma.user.findUnique({
where: { email: body.email },
});
if (existing) {
throw new Error("Email sudah terdaftar");
}
// Hash password sebelum simpan
const hashedPassword = await bcrypt.hash(body.password, 10);
const result = await prisma.user.create({
data: {
nama: body.nama,
email: body.email,
password: hashedPassword,
roleId: body.roleId,
isActive: body.isActive ?? true,
},
});
return {
success: true,
message: "User berhasil dibuat",
data: result,
};
} catch (error) {
console.error("Error creating user:", error);
throw new Error("Gagal membuat user: " + (error as Error).message);
}
}

View File

@@ -1,28 +1,52 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import prisma from "@/lib/prisma";
import { Context } from "elysia";
export default async function userFindMany(context: Context) {
const page = Number(context.query.page) || 1;
const limit = Number(context.query.limit) || 10;
const search = (context.query.search as string) || '';
const skip = (page - 1) * limit;
// Buat where clause
const where: any = { isActive: true };
// Tambahkan pencarian (jika ada)
if (search) {
where.OR = [
{ nama: { contains: search, mode: 'insensitive' } },
];
}
export default async function userFindMany() {
try {
const data = await prisma.user.findMany({
const [data, total] = await Promise.all([
prisma.user.findMany({
include: {
role: true,
},
orderBy: {
createdAt: "desc",
},
});
skip,
take: limit,
}),
prisma.user.count({ where }),
]);
return {
success: true,
message: "Success get all user",
message: "Berhasil ambil user dengan pagination",
data,
page,
limit,
total,
totalPages: Math.ceil(total / limit),
};
} catch (error) {
console.error("Find many error:", error);
} catch (e) {
console.error("Error di findMany paginated:", e);
return {
success: false,
message:
"Gagal mengambil data: " +
(error instanceof Error ? error.message : "Unknown error"),
message: "Gagal mengambil data user",
};
}
}

View File

@@ -1,53 +1,13 @@
import { Elysia, t } from "elysia";
// Import semua handler
import userCreate from "./create";
import userFindMany from "./findMany";
import userFindUnique from "./findUnique";
import userUpdate from "./updt";
import userDelete from "./del"; // `delete` nggak boleh jadi nama file JS langsung, jadi biasanya `del.ts`
import userLogin from "./login";
import userRegister from "./register";
const User = new Elysia({ prefix: "/api/user" })
.post("/register", userRegister, {
body: t.Object({
nama: t.String(),
email: t.String(),
password: t.String(),
}),
})
.post("/login", userLogin, {
body: t.Object({
email: t.String(),
password: t.String(),
}),
})
.post("/create", userCreate, {
body: t.Object({
nama: t.String(),
email: t.String(),
password: t.String(),
roleId: t.String(),
}),
})
.get("/findMany", userFindMany)
.get("/findUnique/:id", userFindUnique)
.put(
"/update/:id",
async (context) => {
const response = await userUpdate(context);
return response;
},
{
body: t.Object({
nama: t.String(),
email: t.String(),
password: t.String(),
roleId: t.String(),
}),
}
)
.put("/del/:id", userDelete, {
params: t.Object({
id: t.String(),

View File

@@ -3,6 +3,7 @@ import { Context } from "elysia";
type FormCreate = {
name: string;
permissions: string[];
}
export default async function roleCreate(context: Context) {
@@ -12,6 +13,7 @@ export default async function roleCreate(context: Context) {
const result = await prisma.role.create({
data: {
name: body.name,
permissions: body.permissions,
},
});
return {

View File

@@ -13,6 +13,7 @@ const Role = new Elysia({
.post("/create", roleCreate, {
body: t.Object({
name: t.String(),
permissions: t.Array(t.String()),
}),
})
@@ -26,6 +27,7 @@ const Role = new Elysia({
.put("/:id", roleUpdate, {
body: t.Object({
name: t.String(),
permissions: t.Array(t.String()),
}),
})
.delete("/del/:id", roleDelete);

View File

@@ -3,6 +3,7 @@ import { Context } from "elysia";
type FormUpdate = {
name: string;
permissions: string[];
}
export default async function roleUpdate(context: Context) {
@@ -14,6 +15,7 @@ export default async function roleUpdate(context: Context) {
where: { id },
data: {
name: body.name,
permissions: body.permissions,
},
});
return {

View File

@@ -1,35 +0,0 @@
// /api/user/update.ts
import prisma from '@/lib/prisma';
import { Context } from 'elysia';
export default async function userUpdate(context: Context) {
const { id } = context.params as { id: string };
const body = await context.body as {
nama?: string;
email?: string;
password?: string;
roleId?: string;
isActive?: boolean;
};
try {
const updated = await prisma.user.update({
where: { id },
data: {
...body,
},
});
return {
success: true,
message: 'User berhasil diupdate',
data: updated,
};
} catch (error) {
console.error(error);
return {
success: false,
message: 'Gagal mengupdate user',
};
}
}