UI & API Admin Menu Kesehatan Done

This commit is contained in:
2025-06-27 23:52:00 +08:00
parent 924be5b11b
commit 6d5b8dcf64
19 changed files with 1546 additions and 752 deletions

View File

@@ -0,0 +1,55 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type ArtikelKesehatanInput = {
title: string;
content: string;
introduction: {content: string};
symptom: {title: string; content: string};
prevention: {title: string; content: string};
firstAid: {title: string; content: string};
mythVsFact: {title: string; mitos: string; fakta: string};
doctorSign: {content: string};
}
const artikelKesehatanCreate = async (context: Context) => {
const body = await context.body as ArtikelKesehatanInput;
const { title, content, introduction, symptom, prevention, firstAid, mythVsFact, doctorSign } = body;
const [createdIntroduction, createdSymptom, createdPrevention, createdFirstAid, createdMythVsFact, createdDoctorSign] = await Promise.all([
prisma.introduction.create({ data: introduction }),
prisma.symptom.create({ data: symptom }),
prisma.prevention.create({ data: prevention }),
prisma.firstAid.create({ data: firstAid }),
prisma.mythVsFact.create({ data: mythVsFact }),
prisma.doctorSign.create({ data: doctorSign }),
])
const artikelKesehatan = await prisma.artikelKesehatan.create({
data: {
title,
content,
introductionId: createdIntroduction.id,
symptomId: createdSymptom.id,
preventionId: createdPrevention.id,
firstAidId: createdFirstAid.id,
mythVsFactId: createdMythVsFact.id,
doctorSignId: createdDoctorSign.id,
},
include: {
introduction: true,
symptom: true,
prevention: true,
firstaid: true,
mythvsfact: true,
doctorsign: true,
}
})
return {
success: true,
message: "Success create artikel kesehatan",
data: artikelKesehatan,
}
}
export default artikelKesehatanCreate; // export the function

View File

@@ -0,0 +1,41 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
const artikelKesehatanDelete = async (context: Context) => {
const id = context.params?.id as string;
if (!id) {
return {
status: 400,
message: "ID tidak ditemukan",
}
}
const artikelKesehatan = await prisma.artikelKesehatan.findUnique({
where: { id },
include: {
introduction: true,
symptom: true,
prevention: true,
firstaid: true,
mythvsfact: true,
doctorsign: true,
}
})
if (!artikelKesehatan) {
return {
status: 404,
message: "Artikel kesehatan tidak ditemukan",
}
}
await prisma.artikelKesehatan.delete({ where: { id } })
return {
status: 200,
success: true,
message: "Artikel kesehatan berhasil dihapus",
}
}
export default artikelKesehatanDelete;

View File

@@ -0,0 +1,30 @@
import prisma from "@/lib/prisma";
export default async function artikelKesehatanFindMany() {
try {
const data = await prisma.artikelKesehatan.findMany({
where: {
isActive: true,
},
include: {
introduction: true,
symptom: true,
prevention: true,
firstaid: true,
mythvsfact: true,
doctorsign: true,
}
})
return {
success: true,
message: "Success fetch artikel kesehatan",
data,
}
} catch (error) {
console.error("Find many error:", error);
return {
success: false,
message: "Failed fetch artikel kesehatan",
}
}
}

View File

@@ -0,0 +1,52 @@
import prisma from "@/lib/prisma";
export async function artikelKesehatanFindUnique(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.artikelKesehatan.findUnique({
where: {id},
include: {
introduction: true,
symptom: true,
prevention: true,
firstaid: true,
mythvsfact: true,
doctorsign: true,
}
})
if (!data) {
return Response.json({
success: false,
message: "Data tidak ditemukan",
}, { status: 404 })
}
return Response.json({
success: true,
data,
}, { status: 200 })
} catch (error) {
console.error("Find unique error:", error);
return Response.json({
success: false,
message: "Failed fetch artikel kesehatan",
}, { status: 500 })
}
}

View File

@@ -0,0 +1,85 @@
import Elysia, { t } from "elysia";
import artikelKesehatanCreate from "./create";
import artikelKesehatanFindMany from "./findMany";
import artikelKesehatanDelete from "./del";
import { artikelKesehatanFindUnique } from "./findUnique";
import artikelKesehatanUpdate from "./updt";
const ArtikelKesehatan = new Elysia({
prefix: "artikel-kesehatan",
tags: ["Kesehatan/Artikel Kesehatan"],
})
.post("/create", artikelKesehatanCreate, {
body: t.Object({
title: t.String(),
content: t.String(),
introduction: t.Object({
content: t.String(),
}),
symptom: t.Object({
title: t.String(),
content: t.String(),
}),
prevention: t.Object({
title: t.String(),
content: t.String(),
}),
firstAid: t.Object({
title: t.String(),
content: t.String(),
}),
mythVsFact: t.Object({
title: t.String(),
mitos: t.String(),
fakta: t.String(),
}),
doctorSign: t.Object({
content: t.String(),
}),
}),
})
.get("/find-many", artikelKesehatanFindMany)
.delete("/del/:id", artikelKesehatanDelete)
.get("/:id", async (context) => {
const response = await artikelKesehatanFindUnique(
new Request(context.request)
);
return response;
})
.put(
"/:id",
async (context) => {
const response = await artikelKesehatanUpdate(context);
return response;
},
{
body: t.Object({
title: t.String(),
content: t.String(),
introduction: t.Object({
content: t.String(),
}),
symptom: t.Object({
title: t.String(),
content: t.String(),
}),
prevention: t.Object({
title: t.String(),
content: t.String(),
}),
firstAid: t.Object({
title: t.String(),
content: t.String(),
}),
mythVsFact: t.Object({
title: t.String(),
mitos: t.String(),
fakta: t.String(),
}),
doctorSign: t.Object({
content: t.String(),
}),
}),
}
);
export default ArtikelKesehatan;

View File

@@ -0,0 +1,106 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type ArtikelKesehatanInput = {
title: string;
content: string;
introduction: { content: string };
symptom: { title: string; content: string };
prevention: { title: string; content: string };
firstAid: { title: string; content: string };
mythVsFact: { title: string; mitos: string; fakta: string };
doctorSign: { content: string };
};
const artikelKesehatanUpdate = async (context: Context) => {
const id = context.params?.id as string;
const body = (await context.body) as ArtikelKesehatanInput;
if (!id) {
return new Response(
JSON.stringify({
success: false,
message: "ID is required",
}),
{
status: 400,
headers: {
"Content-Type": "application/json",
},
}
);
}
const existing = await prisma.artikelKesehatan.findUnique({
where: { id },
});
if (!existing) {
return new Response(
JSON.stringify({ success: false, message: "Data not found" }),
{ status: 404, headers: { "Content-Type": "application/json" } }
);
}
const {
title,
content,
introduction,
symptom,
prevention,
firstAid,
mythVsFact,
doctorSign,
} = body;
await Promise.all([
prisma.introduction.update({
where: { id: existing.introductionId },
data: introduction,
}),
prisma.symptom.update({
where: { id: existing.symptomId },
data: symptom,
}),
prisma.prevention.update({
where: { id: existing.preventionId },
data: prevention,
}),
prisma.firstAid.update({
where: { id: existing.firstAidId },
data: firstAid,
}),
prisma.mythVsFact.update({
where: { id: existing.mythVsFactId },
data: mythVsFact,
}),
prisma.doctorSign.update({
where: { id: existing.doctorSignId },
data: doctorSign,
}),
]);
const updated = await prisma.artikelKesehatan.update({
where: { id },
data: {
title,
content
},
include: {
introduction: true,
symptom: true,
prevention: true,
firstaid: true,
mythvsfact: true,
doctorsign: true,
},
});
return {
status: 200,
success: true,
message: "Artikel kesehatan berhasil dihapus",
data: updated,
};
};
export default artikelKesehatanUpdate;

View File

@@ -15,6 +15,7 @@ import ProgramKesehatan from "./program-kesehatan";
import Puskesmas from "./puskesmas";
import FasilitasKesehatan from "./data_kesehatan_warga/fasilitas_kesehatan";
import JadwalKegiatan from "./data_kesehatan_warga/jadwal_kegiatan";
import ArtikelKesehatan from "./data_kesehatan_warga/artikel_kesehatan";
const Kesehatan = new Elysia({
@@ -37,4 +38,5 @@ const Kesehatan = new Elysia({
.use(InfoWabahPenyakit)
.use(FasilitasKesehatan)
.use(JadwalKegiatan)
.use(ArtikelKesehatan);
export default Kesehatan;