API & UI Menu Landing Page, Submenu Prestasi Desa
This commit is contained in:
@@ -6,6 +6,8 @@ import KategoriDesaAntiKorupsi from "./desa-anti-korupsi/kategori-dak";
|
||||
import DesaAntiKorupsi from "./desa-anti-korupsi";
|
||||
import SDGSDesa from "./sdgs-desa";
|
||||
import APBDes from "./apbdes";
|
||||
import PrestasiDesa from "./prestasi-desa";
|
||||
import KategoriPrestasi from "./prestasi-desa/kategori-prestasi";
|
||||
|
||||
const LandingPage = new Elysia({
|
||||
prefix: "/api/landingpage",
|
||||
@@ -19,5 +21,7 @@ const LandingPage = new Elysia({
|
||||
.use(DesaAntiKorupsi)
|
||||
.use(SDGSDesa)
|
||||
.use(APBDes)
|
||||
.use(PrestasiDesa)
|
||||
.use(KategoriPrestasi)
|
||||
|
||||
export default LandingPage
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = {
|
||||
name: string;
|
||||
deskripsi: string;
|
||||
imageId: string;
|
||||
kategoriId: string;
|
||||
};
|
||||
|
||||
export default async function prestasiDesaCreate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
try {
|
||||
const result = await prisma.prestasiDesa.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
deskripsi: body.deskripsi,
|
||||
imageId: body.imageId,
|
||||
kategoriId: body.kategoriId,
|
||||
},
|
||||
include: {
|
||||
image: true,
|
||||
kategori: true,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil membuat Prestasi Desa",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error creating Prestasi Desa:", error);
|
||||
throw new Error(
|
||||
"Gagal membuat Prestasi Desa: " + (error as Error).message
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function prestasiDesaDelete(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.prestasiDesa.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil menghapus Prestasi Desa",
|
||||
data: deleted,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// /api/berita/findManyPaginated.ts
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
async function prestasiDesaFindMany(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.prestasiDesa.findMany({
|
||||
where: { isActive: true },
|
||||
include: {
|
||||
image: true,
|
||||
kategori: true,
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" }, // opsional, kalau mau urut berdasarkan waktu
|
||||
}),
|
||||
prisma.prestasiDesa.count({
|
||||
where: { isActive: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch Prestasi 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 Prestasi Desa with pagination",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default prestasiDesaFindMany;
|
||||
@@ -0,0 +1,29 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function prestasiDesaFindUnique(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.prestasiDesa.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
kategori: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
throw new Error("Prestasi Desa tidak ditemukan");
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Data Prestasi Desa ditemukan",
|
||||
data,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import prestasiDesaCreate from "./create";
|
||||
import prestasiDesaDelete from "./del";
|
||||
import prestasiDesaFindMany from "./findMany";
|
||||
import prestasiDesaFindUnique from "./findUnique";
|
||||
import prestasiDesaUpdate from "./updt";
|
||||
|
||||
const PrestasiDesa = new Elysia({
|
||||
prefix: "/prestasidesa",
|
||||
tags: ["Landing Page/Profile/Prestasi Desa"],
|
||||
})
|
||||
|
||||
// ✅ Find all
|
||||
.get("/find-many", prestasiDesaFindMany)
|
||||
|
||||
// ✅ Find by ID
|
||||
.get("/:id", prestasiDesaFindUnique)
|
||||
|
||||
// ✅ Create
|
||||
.post("/create", prestasiDesaCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
imageId: t.String(),
|
||||
deskripsi: t.String(),
|
||||
kategoriId: t.String(),
|
||||
}),
|
||||
})
|
||||
|
||||
// ✅ Update
|
||||
.put("/:id", prestasiDesaUpdate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
imageId: t.Optional(t.String()),
|
||||
deskripsi: t.Optional(t.String()),
|
||||
kategoriId: t.Optional(t.String()),
|
||||
}),
|
||||
})
|
||||
// ✅ Delete
|
||||
.delete("/del/:id", prestasiDesaDelete);
|
||||
|
||||
export default PrestasiDesa;
|
||||
@@ -0,0 +1,25 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
|
||||
export default async function kategoriPrestasiCreate(context: Context) {
|
||||
const body = context.body as {name: string};
|
||||
|
||||
if (!body.name) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Nama is required",
|
||||
};
|
||||
}
|
||||
|
||||
const kategoriPrestasi = await prisma.kategoriPrestasiDesa.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create kategori prestasi",
|
||||
data: kategoriPrestasi
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
const kategoriPrestasiDelete = async (context: Context) => {
|
||||
const id = context.params.id;
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID is required",
|
||||
}
|
||||
}
|
||||
|
||||
const kategoriPrestasi = await prisma.kategoriPrestasiDesa.delete({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
})
|
||||
|
||||
if(!kategoriPrestasi) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Kategori Prestasi tidak ditemukan",
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success delete kategori prestasi",
|
||||
data: kategoriPrestasi,
|
||||
}
|
||||
}
|
||||
|
||||
export default kategoriPrestasiDelete
|
||||
@@ -0,0 +1,15 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function kategoriPrestasiFindMany() {
|
||||
const data = await prisma.kategoriPrestasiDesa.findMany();
|
||||
return {
|
||||
success: true,
|
||||
data: data.map((item: any) => {
|
||||
return {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
}
|
||||
}),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Context } from "elysia";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function kategoriPrestasiFindUnique(context: Context) {
|
||||
const url = new URL(context.request.url);
|
||||
const pathSegments = url.pathname.split('/');
|
||||
const id = pathSegments[pathSegments.length - 1];
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID is required",
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof id !== 'string') {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID is required",
|
||||
}
|
||||
}
|
||||
|
||||
const data = await prisma.kategoriPrestasiDesa.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Kategori prestasi tidak ditemukan",
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success find kategori prestasi",
|
||||
data,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Find by ID error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil kategori prestasi: " + (error instanceof Error ? error.message : 'Unknown error'),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import kategoriPrestasiCreate from "./create";
|
||||
import kategoriPrestasiDelete from "./del";
|
||||
import kategoriPrestasiFindMany from "./findMany";
|
||||
import kategoriPrestasiFindUnique from "./findUnique";
|
||||
import kategoriPrestasiUpdate from "./updt";
|
||||
|
||||
const KategoriPrestasi = new Elysia({
|
||||
prefix: "/kategoriprestasi",
|
||||
tags: ["Lingkungan/Kategori Kegiatan"],
|
||||
})
|
||||
.get("/find-many", kategoriPrestasiFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await kategoriPrestasiFindUnique(context);
|
||||
return response;
|
||||
})
|
||||
.delete("/del/:id", kategoriPrestasiDelete)
|
||||
.post("/create", kategoriPrestasiCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
}),
|
||||
})
|
||||
.put("/:id", kategoriPrestasiUpdate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
}),
|
||||
});
|
||||
|
||||
export default KategoriPrestasi;
|
||||
@@ -0,0 +1,44 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function kategoriPrestasiUpdate(context: Context) {
|
||||
const body = context.body as { name: string };
|
||||
const id = context.params?.id as string;
|
||||
|
||||
// Validasi ID dan nama
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID is required",
|
||||
};
|
||||
}
|
||||
|
||||
if (!body.name) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Nama is required",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const kategoriPrestasi = await prisma.kategoriPrestasiDesa.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: body.name,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success update kategori prestasi",
|
||||
data: kategoriPrestasi,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Update error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal update kategori prestasi",
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormUpdatePrestasiDesa = {
|
||||
name?: string;
|
||||
imageId?: string;
|
||||
deskripsi?: string;
|
||||
kategoriId?: string;
|
||||
};
|
||||
|
||||
export default async function prestasiDesaUpdate(context: Context) {
|
||||
const body = context.body as FormUpdatePrestasiDesa;
|
||||
|
||||
const id = context.params.id;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID Prestasi Desa wajib diisi",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const updated = await prisma.prestasiDesa.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: body.name,
|
||||
imageId: body.imageId,
|
||||
deskripsi: body.deskripsi,
|
||||
kategoriId: body.kategoriId,
|
||||
},
|
||||
include: {
|
||||
image: true,
|
||||
kategori: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Prestasi Desa berhasil diperbarui",
|
||||
data: updated,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("❌ Error update Prestasi Desa:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal memperbarui data Prestasi Desa",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user