API & UI Admin Menu Desa, Submenu Pengumuman

This commit is contained in:
2025-08-11 10:39:06 +08:00
parent b3bf6b0327
commit 5cbf7810bc
8 changed files with 398 additions and 213 deletions

View File

@@ -1,23 +1,70 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// /api/berita/findManyPaginated.ts
import prisma from "@/lib/prisma";
import { Context } from "elysia";
async function pengumumanFindMany(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 kategori = (context.query.kategori as string) || ''; // 🔥 Parameter kategori baru
const skip = (page - 1) * limit;
// Buat where clause
const where: any = { isActive: true };
// Filter berdasarkan kategori (jika ada)
if (kategori) {
where.CategoryPengumuman = {
name: {
equals: kategori,
mode: 'insensitive' // Tidak case-sensitive
}
};
}
// Tambahkan pencarian (jika ada)
if (search) {
where.OR = [
{ judul: { contains: search, mode: 'insensitive' } },
{ deskripsi: { contains: search, mode: 'insensitive' } },
{ content: { contains: search, mode: 'insensitive' } },
{ CategoryPengumuman: { name: { contains: search, mode: 'insensitive' } } }
];
}
export default async function pengumumanFindMany() {
try {
const data = await prisma.pengumuman.findMany({
where: { isActive: true },
include: {
CategoryPengumuman: true,
},
});
// Ambil data dan total count secara paralel
const [data, total] = await Promise.all([
prisma.pengumuman.findMany({
where,
include: {
CategoryPengumuman: true,
},
skip,
take: limit,
orderBy: { createdAt: 'desc' },
}),
prisma.pengumuman.count({ where }),
]);
return {
success: true,
message: "Success fetch pengumuman",
message: "Berhasil ambil pengumuman dengan pagination",
data,
page,
limit,
total,
totalPages: Math.ceil(total / limit),
};
} catch (e) {
console.error("Find many error:", e);
console.error("Error di findMany paginated:", e);
return {
success: false,
message: "Failed fetch pengumuman",
message: "Gagal mengambil data pengumuman",
};
}
}
export default pengumumanFindMany;

View File

@@ -10,7 +10,7 @@ import pengumumanUpdate from "./updt";
const Pengumuman = new Elysia({ prefix: "/pengumuman", tags: ["Desa/Pengumuman"] })
.get("/find-many", pengumumanFindMany)
.get("/:id", pengumumanFindById)
.delete("/delete/:id", pengumumanDelete)
.delete("/del/:id", pengumumanDelete)
.post("/create", pengumumanCreate, {
body: t.Object({
judul: t.String(),
@@ -23,7 +23,6 @@ const Pengumuman = new Elysia({ prefix: "/pengumuman", tags: ["Desa/Pengumuman"]
.get("/find-recent", pengumumanFindRecent)
.put("/:id", pengumumanUpdate, {
body: t.Object({
id: t.String(),
judul: t.String(),
deskripsi: t.String(),
content: t.String(),

View File

@@ -3,37 +3,75 @@ import { Prisma } from "@prisma/client";
import { Context } from "elysia";
type FormUpdate = Prisma.PengumumanGetPayload<{
select: {
id: true;
judul: true;
deskripsi: true;
content: true;
categoryPengumumanId: true;
imageId: true;
};
select: {
id: true;
judul: true;
deskripsi: true;
content: true;
categoryPengumumanId: true;
};
}>;
async function pengumumanUpdate(context: Context) {
const body = context.body as FormUpdate;
await prisma.pengumuman.update({
where: { id: body.id },
data: {
judul: body.judul,
deskripsi: body.deskripsi,
content: body.content,
categoryPengumumanId: body.categoryPengumumanId,
},
try {
const id = context.params?.id as string; // ambil dari URL
const body = (await context.body) as Omit<FormUpdate, "id">;
const {
judul,
deskripsi,
content,
categoryPengumumanId,
} = body;
if (!id) {
return new Response(
JSON.stringify({ success: false, message: "ID tidak boleh kosong" }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
const existing = await prisma.pengumuman.findUnique({
where: { id },
include: {
CategoryPengumuman: true,
},
});
return {
if (!existing) {
return new Response(
JSON.stringify({ success: false, message: "pengumuman tidak ditemukan" }),
{ status: 404, headers: { 'Content-Type': 'application/json' } }
);
}
const updated = await prisma.pengumuman.update({
where: { id },
data: {
judul,
deskripsi,
content,
categoryPengumumanId: categoryPengumumanId || null,
},
});
return new Response(
JSON.stringify({
success: true,
message: "Success update pengumuman",
data: {
...body,
},
};
message: "pengumuman berhasil diupdate",
data: updated,
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
} catch (error) {
console.error("Error updating pengumuman:", error);
return new Response(
JSON.stringify({
success: false,
message: "Terjadi kesalahan saat mengupdate pengumuman",
}),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
export default pengumumanUpdate;
export default pengumumanUpdate;