API All Kesehatan
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = Prisma.ProgramKesehatanGetPayload<{
|
||||
select: {
|
||||
name: true;
|
||||
deskripsiSingkat: true;
|
||||
deskripsi: true;
|
||||
imageId: true;
|
||||
}
|
||||
}>
|
||||
export default async function programKesehatanCreate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
await prisma.programKesehatan.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
deskripsiSingkat: body.deskripsiSingkat,
|
||||
deskripsi: body.deskripsi,
|
||||
imageId: body.imageId,
|
||||
}
|
||||
})
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create program kesehatan",
|
||||
data: {
|
||||
...body,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import fs from "fs/promises";
|
||||
import { Context } from "elysia";
|
||||
import path from "path";
|
||||
|
||||
const programKesehatanDelete = async (context: Context) => {
|
||||
const id = context.params?.id as string;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
status: 400,
|
||||
body: "ID tidak diberikan",
|
||||
};
|
||||
}
|
||||
|
||||
const programKesehatan = await prisma.programKesehatan.findUnique({
|
||||
where: { id },
|
||||
include: { image: true },
|
||||
});
|
||||
|
||||
if (!programKesehatan) {
|
||||
return {
|
||||
status: 404,
|
||||
body: "Program kesehatan tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
// Hapus file gambar dari filesystem jika ada
|
||||
if (programKesehatan.image) {
|
||||
try {
|
||||
const filePath = path.join(programKesehatan.image.path, programKesehatan.image.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: programKesehatan.image.id },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Gagal hapus file image:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Hapus berita dari DB
|
||||
await prisma.programKesehatan.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Program kesehatan dan file terkait berhasil dihapus",
|
||||
};
|
||||
};
|
||||
|
||||
export default programKesehatanDelete;
|
||||
@@ -0,0 +1,25 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function programKesehatanFindMany() {
|
||||
try {
|
||||
const data = await prisma.programKesehatan.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
},
|
||||
include: {
|
||||
image: true,
|
||||
}
|
||||
})
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch program kesehatan",
|
||||
data,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Find many error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch program kesehatan",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function programKesehatanFindUnique(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.programKesehatan.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: "Success fetch program kesehatan by ID",
|
||||
data,
|
||||
}, { status: 200 })
|
||||
} catch (error) {
|
||||
console.error("Find by ID error:", error);
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Gagal mengambil program kesehatan: " + (error instanceof Error ? error.message : 'Unknown error'),
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import programKesehatanCreate from "./create";
|
||||
import programKesehatanFindMany from "./find-many";
|
||||
import programKesehatanDelete from "./del";
|
||||
import programKesehatanFindUnique from "./findUnique";
|
||||
import programKesehatanUpdate from "./updt";
|
||||
|
||||
const ProgramKesehatan = new Elysia({
|
||||
prefix: "/programkesehatan",
|
||||
tags: ["Kesehatan/Program Kesehatan"],
|
||||
})
|
||||
.post("/create", programKesehatanCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsiSingkat: t.String(),
|
||||
deskripsi: t.String(),
|
||||
imageId: t.String(),
|
||||
})
|
||||
})
|
||||
.get("/find-many", programKesehatanFindMany)
|
||||
.delete("/del/:id", programKesehatanDelete)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await programKesehatanFindUnique(new Request(context.request));
|
||||
return response;
|
||||
})
|
||||
.put(
|
||||
"/:id",
|
||||
async (context) => {
|
||||
const response = await programKesehatanUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsiSingkat: t.String(),
|
||||
deskripsi: t.String(),
|
||||
imageId: t.String(),
|
||||
})
|
||||
}
|
||||
)
|
||||
export default ProgramKesehatan;
|
||||
@@ -0,0 +1,115 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
|
||||
type FormUpdate = Prisma.ProgramKesehatanGetPayload<{
|
||||
select: {
|
||||
id: true;
|
||||
name: true;
|
||||
deskripsiSingkat: true;
|
||||
deskripsi: true;
|
||||
imageId: true;
|
||||
};
|
||||
}>;
|
||||
export default async function programKesehatanUpdate(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id as string;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
const { name, deskripsiSingkat, deskripsi, 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.programKesehatan.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Program kesehatan tidak ditemukan",
|
||||
}),
|
||||
{
|
||||
status: 404,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (existing.imageId && 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 (err) {
|
||||
console.error("Gagal hapus gambar lama:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prisma.programKesehatan.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name,
|
||||
deskripsiSingkat,
|
||||
deskripsi,
|
||||
imageId,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: "Success update program kesehatan",
|
||||
data: updated,
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Update error:", error);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message:
|
||||
"Gagal mengupdate program kesehatan: " +
|
||||
(error instanceof Error ? error.message : "Unknown error"),
|
||||
}),
|
||||
{
|
||||
status: 500,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user