UI admin bagian pengumuman dan backendnya

This commit is contained in:
2025-06-04 17:24:32 +08:00
parent f56c5b3532
commit 7d58513e33
11 changed files with 513 additions and 153 deletions

View File

@@ -0,0 +1,39 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
const pengumumanDelete = async (context: Context) => {
const id = context.params?.id as string;
if (!id) {
return {
status: 400,
body: "ID tidak ditemukan",
}
}
const pengumuman = await prisma.pengumuman.findUnique({
where: {id},
include: {
CategoryPengumuman: true,
}
})
if (!pengumuman) {
return {
status: 404,
body: "Pengumuman tidak ditemukan",
}
}
await prisma.pengumuman.delete({
where: {id},
})
return {
status: 200,
success: true,
message: "Pengumuman berhasil dihapus",
}
}
export default pengumumanDelete

View File

@@ -0,0 +1,37 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
const pengumumanFindById = async (context: Context) => {
const id = context.params?.id as string;
if (!id) {
return {
status: 400,
body: "ID tidak diberikan",
};
}
const pengumuman = await prisma.pengumuman.findUnique({
where: { id },
include: {
CategoryPengumuman: true,
},
});
if (!pengumuman) {
return {
status: 404,
body: "Pengumuman tidak ditemukan",
};
}
return {
status: 200,
success: true,
message: "Success fetch pengumuman by ID",
data: pengumuman,
};
};
export default pengumumanFindById;

View File

@@ -1,8 +1,23 @@
import prisma from "@/lib/prisma";
export default async function pengumumanFindMany() {
const res = await prisma.pengumuman.findMany();
try {
const data = await prisma.pengumuman.findMany({
where: { isActive: true },
include: {
CategoryPengumuman: true,
},
});
return {
data: res,
success: true,
message: "Success fetch pengumuman",
data,
};
}
} catch (e) {
console.error("Find many error:", e);
return {
success: false,
message: "Failed fetch pengumuman",
};
}
}

View File

@@ -3,10 +3,15 @@ import { pengumumanCreate } from "./create";
import pengumumanFindMany from "./find-many";
import { t } from "elysia";
import pengumumanCategoryFindMany from "./category";
import pengumumanDelete from "./del";
import pengumumanFindById from "./find-by-id";
import pengumumanUpdate from "./updt";
const Pengumuman = new Elysia({ prefix: "/pengumuman", tags: ["Desa/Pengumuman"] })
.get("/category/find-many", pengumumanCategoryFindMany)
.get("/find-many", pengumumanFindMany)
.get("/:id", pengumumanFindById)
.delete("/delete/:id", pengumumanDelete)
.post("/create", pengumumanCreate, {
body: t.Object({
judul: t.String(),
@@ -14,6 +19,15 @@ const Pengumuman = new Elysia({ prefix: "/pengumuman", tags: ["Desa/Pengumuman"]
content: t.String(),
categoryPengumumanId: t.Union([t.String(), t.Null()]),
}),
})
.put("/:id", pengumumanUpdate, {
body: t.Object({
id: t.String(),
judul: t.String(),
deskripsi: t.String(),
content: t.String(),
categoryPengumumanId: t.Union([t.String(), t.Null()]),
}),
});
export default Pengumuman;

View File

@@ -0,0 +1,39 @@
import prisma from "@/lib/prisma";
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;
};
}>;
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,
},
});
return {
success: true,
message: "Success update pengumuman",
data: {
...body,
},
};
}
export default pengumumanUpdate;