API & UI Menu Lingkungan, Submenu Program Penghijauan
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import Elysia from "elysia";
|
||||
import PengelolaanSampah from "./pengelolaan-sampah";
|
||||
import ProgramPenghijauan from "./program-penghijauan";
|
||||
|
||||
const Lingkungan = new Elysia({
|
||||
prefix: "/api/lingkungan",
|
||||
@@ -7,5 +8,6 @@ const Lingkungan = new Elysia({
|
||||
})
|
||||
|
||||
.use(PengelolaanSampah)
|
||||
.use(ProgramPenghijauan)
|
||||
|
||||
export default Lingkungan;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreateProgramPenghijauan = {
|
||||
name: string;
|
||||
judul: string;
|
||||
deskripsi: string;
|
||||
icon: string;
|
||||
}
|
||||
|
||||
export default async function programPenghijauanCreate(context: Context){
|
||||
const body = context.body as FormCreateProgramPenghijauan;
|
||||
|
||||
await prisma.programPenghijauan.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
judul: body.judul,
|
||||
deskripsi: body.deskripsi,
|
||||
icon: body.icon,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create program kreatif",
|
||||
data: {
|
||||
...body,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function programPenghijauanDelete(context: Context) {
|
||||
const { id } = context.params as { id: string };
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID program penghijauan tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const deleted = await prisma.programPenghijauan.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Program penghijauan berhasil dihapus",
|
||||
data: deleted,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error delete program penghijauan:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat menghapus program penghijauan",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
// Di findMany.ts
|
||||
export default async function programPenghijauanFindMany(context: Context) {
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
try {
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.programPenghijauan.findMany({
|
||||
where: { isActive: true },
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.programPenghijauan.count({
|
||||
where: { isActive: true }
|
||||
})
|
||||
]);
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch program penghijauan with pagination",
|
||||
data,
|
||||
page,
|
||||
totalPages,
|
||||
total,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Find many paginated error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch program penghijauan with pagination",
|
||||
data: [],
|
||||
page: 1,
|
||||
totalPages: 1,
|
||||
total: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function programPenghijauanFindUnique(context: Context) {
|
||||
const { id } = context.params as { id: string };
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID program penghijauan diperlukan",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const programPenghijauan = await prisma.programPenghijauan.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!programPenghijauan) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Program penghijauan tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: programPenghijauan,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error findUnique program penghijauan:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data program penghijauan",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import programPenghijauanFindMany from "./findMany";
|
||||
import programPenghijauanFindUnique from "./findUnique";
|
||||
import programPenghijauanCreate from "./create";
|
||||
import programPenghijauanUpdate from "./updt";
|
||||
import programPenghijauanDelete from "./del";
|
||||
|
||||
const ProgramPenghijauan = new Elysia({
|
||||
prefix: "/programpenghijauan",
|
||||
tags: ["Lingkungan/Program Penghijauan"],
|
||||
})
|
||||
.get("/find-many", programPenghijauanFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await programPenghijauanFindUnique(context);
|
||||
return response;
|
||||
})
|
||||
.post("/create", programPenghijauanCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
judul: t.String(),
|
||||
deskripsi: t.String(),
|
||||
icon: t.String(),
|
||||
}),
|
||||
})
|
||||
.put(
|
||||
"/:id",
|
||||
async (context) => {
|
||||
const response = await programPenghijauanUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
judul: t.String(),
|
||||
deskripsi: t.String(),
|
||||
icon: t.String(),
|
||||
}),
|
||||
}
|
||||
)
|
||||
.delete("/del/:id", programPenghijauanDelete);
|
||||
export default ProgramPenghijauan;
|
||||
@@ -0,0 +1,48 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormUpdateProgramPenghijauan = {
|
||||
id: string;
|
||||
name?: string;
|
||||
judul?: string;
|
||||
deskripsi?: string;
|
||||
icon?: string;
|
||||
};
|
||||
export default async function programPenghijauanUpdate(context: Context) {
|
||||
const body = context.body as FormUpdateProgramPenghijauan;
|
||||
const id = context.params?.id; // ambil dari URL param
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID program penghijauan wajib diisi",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const updated = await prisma.programPenghijauan.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: body.name,
|
||||
judul: body.judul,
|
||||
deskripsi: body.deskripsi,
|
||||
icon: body.icon,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Program penghijauan berhasil diupdate",
|
||||
data: updated,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error update program penghijauan:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengupdate program penghijauan",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user