API & UI Menu Landing Page, Submenu SDGs Desa & APBDes

This commit is contained in:
2025-07-24 11:53:58 +08:00
parent bdf751ec3d
commit b745bd4623
25 changed files with 2189 additions and 14 deletions

View File

@@ -0,0 +1,36 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type FormCreate = {
name: string;
jumlah: string;
imageId: string;
fileId: string;
};
export default async function apbdesCreate(context: Context) {
const body = context.body as FormCreate;
try {
const result = await prisma.aPBDes.create({
data: {
name: body.name,
jumlah: body.jumlah,
imageId: body.imageId,
fileId: body.fileId,
},
include: {
image: true,
file: true,
},
});
return {
success: true,
message: "Berhasil membuat APB Des",
data: result,
};
} catch (error) {
console.error("Error creating APB Des:", error);
throw new Error("Gagal membuat APB Des: " + (error as Error).message);
}
}

View File

@@ -0,0 +1,21 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
export default async function apbdesDelete(context: Context) {
const { params } = context;
const id = params?.id as string;
if (!id) {
throw new Error("ID tidak ditemukan dalam parameter");
}
const deleted = await prisma.aPBDes.delete({
where: { id },
});
return {
success: true,
message: "Berhasil menghapus APB Des",
data: deleted,
};
}

View File

@@ -0,0 +1,44 @@
// /api/berita/findManyPaginated.ts
import prisma from "@/lib/prisma";
import { Context } from "elysia";
async function apbdesFindMany(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.aPBDes.findMany({
where: { isActive: true },
include: {
image: true,
file: true,
},
skip,
take: limit,
orderBy: { createdAt: "desc" }, // opsional, kalau mau urut berdasarkan waktu
}),
prisma.aPBDes.count({
where: { isActive: true },
}),
]);
return {
success: true,
message: "Success fetch APB Des with pagination",
data,
page,
totalPages: Math.ceil(total / limit),
total,
};
} catch (e) {
console.error("Find many paginated error:", e);
return {
success: false,
message: "Failed fetch APB Des with pagination",
};
}
}
export default apbdesFindMany;

View File

@@ -0,0 +1,29 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
export default async function apbdesFindUnique(context: Context) {
const { params } = context;
const id = params?.id as string;
if (!id) {
throw new Error("ID tidak ditemukan dalam parameter");
}
const data = await prisma.aPBDes.findUnique({
where: { id },
include: {
image: true,
file: true,
},
});
if (!data) {
throw new Error("APB Des tidak ditemukan");
}
return {
success: true,
message: "Data APB Des ditemukan",
data,
};
}

View File

@@ -0,0 +1,41 @@
import Elysia, { t } from "elysia";
import apbdesCreate from "./create";
import apbdesDelete from "./del";
import apbdesFindMany from "./findMany";
import apbdesFindUnique from "./findUnique";
import apbdesUpdate from "./updt";
const APBDes = new Elysia({
prefix: "/apbdes",
tags: ["Landing Page/Profile/APB Des"],
})
// ✅ Find all
.get("/find-many", apbdesFindMany)
// ✅ Find by ID
.get("/:id", apbdesFindUnique)
// ✅ Create
.post("/create", apbdesCreate, {
body: t.Object({
name: t.String(),
imageId: t.String(),
fileId: t.String(),
jumlah: t.String(),
}),
})
// ✅ Update
.put("/:id", apbdesUpdate, {
body: t.Object({
name: t.String(),
imageId: t.Optional(t.String()),
fileId: t.Optional(t.String()),
jumlah: t.Optional(t.String()),
}),
})
// ✅ Delete
.delete("/del/:id", apbdesDelete);
export default APBDes;

View File

@@ -0,0 +1,51 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type FormUpdateAPBDes = {
name?: string;
imageId?: string;
fileId?: string;
jumlah?: string;
};
export default async function apbdesUpdate(context: Context) {
const body = context.body as FormUpdateAPBDes;
const id = context.params.id;
if (!id) {
return {
success: false,
message: "ID APB Des wajib diisi",
};
}
try {
const updated = await prisma.aPBDes.update({
where: { id },
data: {
name: body.name,
imageId: body.imageId,
fileId: body.fileId,
jumlah: body.jumlah,
},
include: {
image: true,
},
});
return {
success: true,
message: "APB Des berhasil diperbarui",
data: updated,
};
} catch (error: any) {
console.error("❌ Error update APB Des:", error);
return {
success: false,
message: "Gagal memperbarui data APB Des",
error: error.message,
};
}
}

View File

@@ -4,6 +4,8 @@ import ProgramInovasi from "./profile/program-inovasi";
import PejabatDesa from "./profile/pejabat-desa";
import KategoriDesaAntiKorupsi from "./desa-anti-korupsi/kategori-dak";
import DesaAntiKorupsi from "./desa-anti-korupsi";
import SDGSDesa from "./sdgs-desa";
import APBDes from "./apbdes";
const LandingPage = new Elysia({
prefix: "/api/landingpage",
@@ -15,5 +17,7 @@ const LandingPage = new Elysia({
.use(PejabatDesa)
.use(KategoriDesaAntiKorupsi)
.use(DesaAntiKorupsi)
.use(SDGSDesa)
.use(APBDes)
export default LandingPage

View File

@@ -0,0 +1,33 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type FormCreate = {
name: string;
jumlah: string;
imageId: string;
};
export default async function sdgsDesaCreate(context: Context) {
const body = context.body as FormCreate;
try {
const result = await prisma.sDGSDesa.create({
data: {
name: body.name,
jumlah: body.jumlah,
imageId: body.imageId,
},
include: {
image: true,
},
});
return {
success: true,
message: "Berhasil membuat SDGS Desa",
data: result,
};
} catch (error) {
console.error("Error creating SDGS Desa:", error);
throw new Error("Gagal membuat SDGS Desa: " + (error as Error).message);
}
}

View File

@@ -0,0 +1,21 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
export default async function sdgsDesaDelete(context: Context) {
const { params } = context;
const id = params?.id as string;
if (!id) {
throw new Error("ID tidak ditemukan dalam parameter");
}
const deleted = await prisma.sDGSDesa.delete({
where: { id },
});
return {
success: true,
message: "Berhasil menghapus SDGS Desa",
data: deleted,
};
}

View File

@@ -0,0 +1,43 @@
// /api/berita/findManyPaginated.ts
import prisma from "@/lib/prisma";
import { Context } from "elysia";
async function sdgsDesaFindMany(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.sDGSDesa.findMany({
where: { isActive: true },
include: {
image: true,
},
skip,
take: limit,
orderBy: { createdAt: "desc" }, // opsional, kalau mau urut berdasarkan waktu
}),
prisma.sDGSDesa.count({
where: { isActive: true },
}),
]);
return {
success: true,
message: "Success fetch SDGS Desa with pagination",
data,
page,
totalPages: Math.ceil(total / limit),
total,
};
} catch (e) {
console.error("Find many paginated error:", e);
return {
success: false,
message: "Failed fetch SDGS Desa with pagination",
};
}
}
export default sdgsDesaFindMany;

View File

@@ -0,0 +1,28 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
export default async function sdgsDesaFindUnique(context: Context) {
const { params } = context;
const id = params?.id as string;
if (!id) {
throw new Error("ID tidak ditemukan dalam parameter");
}
const data = await prisma.sDGSDesa.findUnique({
where: { id },
include: {
image: true,
},
});
if (!data) {
throw new Error("SDGS Desa tidak ditemukan");
}
return {
success: true,
message: "Data SDGS Desa ditemukan",
data,
};
}

View File

@@ -0,0 +1,39 @@
import Elysia, { t } from "elysia";
import sdgsDesaCreate from "./create";
import sdgsDesaDelete from "./del";
import sdgsDesaFindMany from "./findMany";
import sdgsDesaFindUnique from "./findUnique";
import sdgsDesaUpdate from "./updt";
const SDGSDesa = new Elysia({
prefix: "/sdgsdesa",
tags: ["Landing Page/Profile/SDGS Desa"],
})
// ✅ Find all
.get("/find-many", sdgsDesaFindMany)
// ✅ Find by ID
.get("/:id", sdgsDesaFindUnique)
// ✅ Create
.post("/create", sdgsDesaCreate, {
body: t.Object({
name: t.String(),
imageId: t.String(),
jumlah: t.String(),
}),
})
// ✅ Update
.put("/:id", sdgsDesaUpdate, {
body: t.Object({
name: t.String(),
imageId: t.Optional(t.String()),
jumlah: t.Optional(t.String()),
}),
})
// ✅ Delete
.delete("/del/:id", sdgsDesaDelete);
export default SDGSDesa;

View File

@@ -0,0 +1,49 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type FormUpdateSDGSDesa = {
name?: string;
imageId?: string;
jumlah?: string;
};
export default async function sdgsDesaUpdate(context: Context) {
const body = context.body as FormUpdateSDGSDesa;
const id = context.params.id;
if (!id) {
return {
success: false,
message: "ID SDGS Desa wajib diisi",
};
}
try {
const updated = await prisma.sDGSDesa.update({
where: { id },
data: {
name: body.name,
imageId: body.imageId,
jumlah: body.jumlah,
},
include: {
image: true,
},
});
return {
success: true,
message: "SDGS Desa berhasil diperbarui",
data: updated,
};
} catch (error: any) {
console.error("❌ Error update SDGS Desa:", error);
return {
success: false,
message: "Gagal memperbarui data SDGS Desa",
error: error.message,
};
}
}