API Struktur Admin PPID
This commit is contained in:
@@ -9,6 +9,7 @@ import PermohonanKeberatanInformasiPublik from "./permohonan_keberatan_informasi
|
||||
import ProfilePPID from "./profile_ppid";
|
||||
import VisiMisiPPID from "./visi_misi_ppid/visi_misi_ppid";
|
||||
import DasarHukumPPID from "./dasar_hukum";
|
||||
import StrukturPPID from "./struktur_ppid";
|
||||
|
||||
|
||||
|
||||
@@ -24,6 +25,7 @@ const PPID = new Elysia({ prefix: "/api/ppid", tags: ["PPID"] })
|
||||
.use(PermohonanKeberatanInformasiPublik)
|
||||
.use(VisiMisiPPID)
|
||||
.use(DasarHukumPPID)
|
||||
.use(StrukturPPID)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function strukturPPIDFindById(request: Request) {
|
||||
const url = new URL(request.url);
|
||||
const pathSegments = url.pathname.split('/');
|
||||
const id = pathSegments[pathSegments.length - 1];
|
||||
|
||||
if (!id) {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "ID tidak boleh kosong",
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof id !== 'string') {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "ID tidak valid",
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const data = await prisma.strukturPPID.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
}
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Data tidak ditemukan",
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
success: true,
|
||||
message: "Berhasil mengambil data berdasarkan ID",
|
||||
data,
|
||||
}, { status: 200 });
|
||||
} catch (e) {
|
||||
console.error("Find by ID error:", e);
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Gagal mengambil data: " + (e instanceof Error ? e.message : 'Unknown error'),
|
||||
}, {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
}
|
||||
23
src/app/api/[[...slugs]]/_lib/ppid/struktur_ppid/index.ts
Normal file
23
src/app/api/[[...slugs]]/_lib/ppid/struktur_ppid/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import strukturPPIDFindById from "./find-by-id";
|
||||
import strukturPPIDUpdate from "./update";
|
||||
|
||||
const StrukturPPID = new Elysia({
|
||||
prefix: "/strukturppid",
|
||||
tags: ["PPID/Struktur PPID"]
|
||||
})
|
||||
.get("/:id", async (context) => {
|
||||
const response = await strukturPPIDFindById(new Request(context.request))
|
||||
return response
|
||||
})
|
||||
.put("/:id", async (context) => {
|
||||
const response = await strukturPPIDUpdate(context)
|
||||
return response
|
||||
}, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
imageId: t.String(),
|
||||
})
|
||||
})
|
||||
|
||||
export default StrukturPPID
|
||||
116
src/app/api/[[...slugs]]/_lib/ppid/struktur_ppid/update.ts
Normal file
116
src/app/api/[[...slugs]]/_lib/ppid/struktur_ppid/update.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
type FormUpdate = Prisma.StrukturPPIDGetPayload<{
|
||||
select: {
|
||||
id: true;
|
||||
name: true;
|
||||
imageId: true;
|
||||
};
|
||||
}>;
|
||||
|
||||
export default async function strukturPPIDUpdate(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id as string;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
const { name, imageId } = 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.strukturPPID.findUnique({
|
||||
where: {
|
||||
id
|
||||
},
|
||||
include: {
|
||||
image: true,
|
||||
}
|
||||
})
|
||||
|
||||
if (!existing) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Data tidak ditemukan",
|
||||
}),
|
||||
{
|
||||
status: 404,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (existing.imageId !== imageId) {
|
||||
const oldImage = existing.image;
|
||||
if (oldImage) {
|
||||
try {
|
||||
const filePath = path.join(oldImage.path, oldImage.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: oldImage.id },
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Gagal hapus gambar lama:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prisma.strukturPPID.update({
|
||||
where: {
|
||||
id
|
||||
},
|
||||
data: {
|
||||
name,
|
||||
imageId,
|
||||
}
|
||||
})
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: "Struktur PPID Berhasil Dibuat",
|
||||
data: updated,
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error updating struktur PPID:", error);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat mengupdate struktur PPID",
|
||||
}),
|
||||
{
|
||||
status: 500,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user