UI admin bagian pengumuman dan backendnya
This commit is contained in:
39
src/app/api/[[...slugs]]/_lib/desa/pengumuman/del.ts
Normal file
39
src/app/api/[[...slugs]]/_lib/desa/pengumuman/del.ts
Normal 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
|
||||
37
src/app/api/[[...slugs]]/_lib/desa/pengumuman/find-by-id.ts
Normal file
37
src/app/api/[[...slugs]]/_lib/desa/pengumuman/find-by-id.ts
Normal 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;
|
||||
|
||||
@@ -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",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
39
src/app/api/[[...slugs]]/_lib/desa/pengumuman/updt.ts
Normal file
39
src/app/api/[[...slugs]]/_lib/desa/pengumuman/updt.ts
Normal 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;
|
||||
|
||||
Reference in New Issue
Block a user