Senin, 26 May 2025 :

Yang Sudah Di Kerjakan
* Tampilan UI Admin di menu ekonomi
* API Create, edit dan delete berita

Yang Akan Dikerjakan:
* API ProfilePPID
* Tampilan UI Admin Di Menu Inovasi
This commit is contained in:
2025-05-26 17:15:07 +08:00
parent 02738104b5
commit 3654629bde
9 changed files with 563 additions and 65 deletions

View File

@@ -0,0 +1,65 @@
import prisma from "@/lib/prisma";
export default async function handler(
request: Request
) {
// Extract the ID from the URL path
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 {
// Validate that the ID is a valid UUID or whatever format you're using
if (typeof id !== 'string') {
return Response.json({
success: false,
message: "ID tidak valid",
}, { status: 400 });
}
const data = await prisma.berita.findUnique({
where: { id },
include: {
image: true,
kategoriBerita: true,
},
});
if (!data) {
return Response.json({
success: false,
message: "Berita tidak ditemukan",
}, { status: 404 });
}
// Ensure we're returning a proper Response object
return new Response(JSON.stringify({
success: true,
message: "Success fetch berita by ID",
data,
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
});
} catch (e) {
console.error("Find by ID error:", e);
return new Response(JSON.stringify({
success: false,
message: "Gagal mengambil berita: " + (e instanceof Error ? e.message : 'Unknown error'),
}), {
status: 500,
headers: {
'Content-Type': 'application/json',
},
});
}
}

View File

@@ -4,10 +4,15 @@ import beritaFindMany from "./find-many";
import beritaCreate from "./create";
import beritaDelete from "./del";
import beritaUpdate from "./updt";
import findBeritaById from "./find-by-id";
const Berita = new Elysia({ prefix: "/berita", tags: ["Desa/Berita"] })
.get("/category/find-many", kategoriBeritaFindMany)
.get("/find-many", beritaFindMany)
.get("/:id", async (context) => {
const response = await findBeritaById(new Request(context.request));
return response;
})
.post("/create", beritaCreate, {
body: t.Object({
judul: t.String(),
@@ -18,14 +23,21 @@ const Berita = new Elysia({ prefix: "/berita", tags: ["Desa/Berita"] })
}),
})
.delete("/delete/:id", beritaDelete)
.put("/update/:id", beritaUpdate, {
body: t.Object({
judul: t.String(),
deskripsi: t.String(),
imageId: t.String(),
content: t.String(),
kategoriBeritaId: t.Union([t.String(), t.Null()]),
}),
});
.put(
"/:id",
async (context) => {
const response = await beritaUpdate(context);
return response;
},
{
body: t.Object({
judul: t.String(),
deskripsi: t.String(),
imageId: t.String(),
content: t.String(),
kategoriBeritaId: t.Union([t.String(), t.Null()]),
}),
}
);
export default Berita;

View File

@@ -86,7 +86,8 @@ type FormUpdate = Prisma.BeritaGetPayload<{
async function beritaUpdate(context: Context) {
const id = context.params.id as string; // ambil dari URL
try {
const id = context.params?.id as string; // ambil dari URL
const body = (await context.body) as Omit<FormUpdate, "id">;
const {
@@ -98,24 +99,25 @@ async function beritaUpdate(context: Context) {
} = body;
if (!id) {
return {
status: 400,
body: "ID tidak boleh kosong",
};
return new Response(
JSON.stringify({ success: false, message: "ID tidak boleh kosong" }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
const existing = await prisma.berita.findUnique({
where: { id },
include: {
image: true,
kategoriBerita: true,
},
});
if (!existing) {
return {
status: 404,
body: "Berita tidak ditemukan",
};
return new Response(
JSON.stringify({ success: false, message: "Berita tidak ditemukan" }),
{ status: 404, headers: { 'Content-Type': 'application/json' } }
);
}
if (existing.imageId && existing.imageId !== imageId) {
@@ -139,15 +141,28 @@ async function beritaUpdate(context: Context) {
judul,
deskripsi,
content,
kategoriBeritaId,
kategoriBeritaId: kategoriBeritaId || null,
imageId,
},
});
return {
success: true,
message: "Berita berhasil diupdate",
data: updated,
};
return new Response(
JSON.stringify({
success: true,
message: "Berita berhasil diupdate",
data: updated,
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
} catch (error) {
console.error("Error updating berita:", error);
return new Response(
JSON.stringify({
success: false,
message: "Terjadi kesalahan saat mengupdate berita",
}),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
export default beritaUpdate;