UI & API Menu Landing Page, Submenu Desa Anti Korupsi
This commit is contained in:
@@ -14,27 +14,15 @@ const fileStorageCreate = async (context: Context) => {
|
||||
const file = body.file;
|
||||
const name = body.name;
|
||||
|
||||
if (!file) {
|
||||
return {
|
||||
status: 400,
|
||||
body: "No file uploaded",
|
||||
};
|
||||
}
|
||||
if (!file) return { status: 400, body: "No file uploaded" };
|
||||
if (!name) return { status: 400, body: "No name provided" };
|
||||
if (!UPLOAD_DIR) return { status: 500, body: "UPLOAD_DIR is not defined" };
|
||||
|
||||
if (!name) {
|
||||
return {
|
||||
status: 400,
|
||||
body: "No name provided",
|
||||
};
|
||||
}
|
||||
// Tentukan kategori berdasarkan mimeType
|
||||
const isImage = file.type.startsWith("image/");
|
||||
const category = isImage ? "image" : "document";
|
||||
|
||||
if (!UPLOAD_DIR) {
|
||||
return {
|
||||
status: 500,
|
||||
body: "UPLOAD_DIR is not defined",
|
||||
};
|
||||
}
|
||||
const pathName = "allFile";
|
||||
const pathName = category === "image" ? "images" : "documents";
|
||||
const rootPath = path.join(UPLOAD_DIR, pathName);
|
||||
await fs.mkdir(rootPath, { recursive: true });
|
||||
|
||||
@@ -44,9 +32,10 @@ const fileStorageCreate = async (context: Context) => {
|
||||
const data = await prisma.fileStorage.create({
|
||||
data: {
|
||||
name: newName,
|
||||
realName: file.name, // Add the original file name as realName
|
||||
realName: file.name,
|
||||
path: rootPath,
|
||||
mimeType: file.type,
|
||||
category,
|
||||
link: `/api/fileStorage/findUnique/${newName}`,
|
||||
},
|
||||
});
|
||||
@@ -56,9 +45,7 @@ const fileStorageCreate = async (context: Context) => {
|
||||
Buffer.from(await file.arrayBuffer())
|
||||
);
|
||||
|
||||
return {
|
||||
data,
|
||||
};
|
||||
return { data };
|
||||
};
|
||||
|
||||
export default fileStorageCreate;
|
||||
export default fileStorageCreate;
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export const fileStorageFindMany = async () => {
|
||||
const data = await prisma.fileStorage.findMany();
|
||||
return data;
|
||||
export const fileStorageFindMany = async (context: Context) => {
|
||||
const category = context.query?.category as string | undefined;
|
||||
|
||||
const data = await prisma.fileStorage.findMany({
|
||||
where: category ? { category } : {},
|
||||
});
|
||||
|
||||
return { data };
|
||||
};
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = {
|
||||
name: string;
|
||||
deskripsi: string;
|
||||
fileId: string;
|
||||
kategoriId: string; // minimal satu kategori
|
||||
};
|
||||
|
||||
export default async function desaAntiKorupsiCreate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
if (!body.kategoriId) {
|
||||
throw new Error("kategoriId wajib diisi");
|
||||
}
|
||||
|
||||
try {
|
||||
// Create langsung data AdministrasiOnline
|
||||
const result = await prisma.desaAntiKorupsi.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
deskripsi: body.deskripsi,
|
||||
fileId: body.fileId,
|
||||
kategoriId: body.kategoriId, // relasi ke JenisLayanan
|
||||
},
|
||||
include: {
|
||||
kategori: true, // Include data relasi
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil membuat desa anti korupsi",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error creating desa anti korupsi:", error);
|
||||
throw new Error(
|
||||
"Gagal membuat desa anti korupsi: " + (error as Error).message
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function desaAntiKorupsiDelete(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.desaAntiKorupsi.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil menghapus desa anti korupsi",
|
||||
data: deleted,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// /api/berita/findManyPaginated.ts
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
async function desaAntiKorupsiFindMany(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.desaAntiKorupsi.findMany({
|
||||
where: { isActive: true },
|
||||
include: {
|
||||
kategori: true,
|
||||
file: true,
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' }, // opsional, kalau mau urut berdasarkan waktu
|
||||
}),
|
||||
prisma.desaAntiKorupsi.count({
|
||||
where: { isActive: true }
|
||||
})
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch desa anti korupsi 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 desa anti korupsi with pagination",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default desaAntiKorupsiFindMany;
|
||||
@@ -0,0 +1,29 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function desaAntiKorupsiFindUnique(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.desaAntiKorupsi.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
kategori: true,
|
||||
file: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
throw new Error("Desa anti korupsi tidak ditemukan");
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Data desa anti korupsi ditemukan",
|
||||
data,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import desaAntiKorupsiDelete from "./del";
|
||||
import desaAntiKorupsiFindMany from "./findMany";
|
||||
import desaAntiKorupsiFindUnique from "./findUnique";
|
||||
import desaAntiKorupsiUpdate from "./updt";
|
||||
import desaAntiKorupsiCreate from "./create";
|
||||
|
||||
const DesaAntiKorupsi = new Elysia({
|
||||
prefix: "/desaantikorupsi",
|
||||
tags: ["Landing Page/Desa Anti Korupsi"],
|
||||
})
|
||||
|
||||
// ✅ Find all
|
||||
.get("/find-many", desaAntiKorupsiFindMany)
|
||||
|
||||
// ✅ Find by ID
|
||||
.get("/:id", desaAntiKorupsiFindUnique)
|
||||
|
||||
// ✅ Create
|
||||
.post("/create", desaAntiKorupsiCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
fileId: t.String(),
|
||||
kategoriId: t.String(),
|
||||
}),
|
||||
})
|
||||
|
||||
// ✅ Update
|
||||
.put("/:id", desaAntiKorupsiUpdate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
fileId: t.String(),
|
||||
kategoriId: t.String(),
|
||||
}),
|
||||
})
|
||||
// ✅ Delete
|
||||
.delete("/del/:id", desaAntiKorupsiDelete);
|
||||
|
||||
export default DesaAntiKorupsi;
|
||||
@@ -0,0 +1,25 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
|
||||
export default async function kategoriDesaAntiKorupsiCreate(context: Context) {
|
||||
const body = context.body as {name: string};
|
||||
|
||||
if (!body.name) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Nama is required",
|
||||
};
|
||||
}
|
||||
|
||||
const kategoriDesaAntiKorupsi = await prisma.kategoriDesaAntiKorupsi.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create kategori desa anti korupsi",
|
||||
data: kategoriDesaAntiKorupsi
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function kategoriDesaAntiKorupsiDelete(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.kategoriDesaAntiKorupsi.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil menghapus kategori desa anti korupsi",
|
||||
data: deleted,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function kategoriDesaAntiKorupsiFindMany() {
|
||||
const data = await prisma.kategoriDesaAntiKorupsi.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 kategoriDesaAntiKorupsiFindUnique(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.kategoriDesaAntiKorupsi.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Kategori desa anti korupsi tidak ditemukan",
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success find kategori desa anti korupsi",
|
||||
data,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Find by ID error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil kategori desa anti korupsi: " + (error instanceof Error ? error.message : 'Unknown error'),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import kategoriDesaAntiKorupsiCreate from "./create";
|
||||
import kategoriDesaAntiKorupsiDelete from "./del";
|
||||
import kategoriDesaAntiKorupsiFindMany from "./findMany";
|
||||
import kategoriDesaAntiKorupsiFindUnique from "./findUnique";
|
||||
import kategoriDesaAntiKorupsiUpdate from "./updt";
|
||||
|
||||
const KategoriDesaAntiKorupsi = new Elysia({
|
||||
prefix: "/kategoridak",
|
||||
tags: ["Landing Page/Desa Anti Korupsi"],
|
||||
})
|
||||
.get("/find-many", kategoriDesaAntiKorupsiFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await kategoriDesaAntiKorupsiFindUnique(context);
|
||||
return response;
|
||||
})
|
||||
.delete("/del/:id", kategoriDesaAntiKorupsiDelete)
|
||||
.post("/create", kategoriDesaAntiKorupsiCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
}),
|
||||
})
|
||||
.put("/:id", kategoriDesaAntiKorupsiUpdate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
}),
|
||||
});
|
||||
|
||||
export default KategoriDesaAntiKorupsi;
|
||||
@@ -0,0 +1,44 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function kategoriDesaAntiKorupsiUpdate(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: "Name is required",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const kategoriDesaAntiKorupsi = await prisma.kategoriDesaAntiKorupsi.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: body.name,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success update kategori desa anti korupsi",
|
||||
data: kategoriDesaAntiKorupsi,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Update error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal update kategori desa anti korupsi",
|
||||
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 FormUpdateDesaAntiKorupsi = {
|
||||
name?: string;
|
||||
deskripsi?: string;
|
||||
fileId?: string;
|
||||
kategoriId?: string; // minimal satu kategori
|
||||
};
|
||||
|
||||
export default async function desaAntiKorupsiUpdate(context: Context) {
|
||||
const body = context.body as FormUpdateDesaAntiKorupsi;
|
||||
|
||||
const id = context.params.id;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID desa anti korupsi wajib diisi",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const updated = await prisma.desaAntiKorupsi.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: body.name,
|
||||
deskripsi: body.deskripsi,
|
||||
fileId: body.fileId,
|
||||
kategoriId: body.kategoriId,
|
||||
},
|
||||
include: {
|
||||
file: true,
|
||||
kategori: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Desa anti korupsi berhasil diperbarui",
|
||||
data: updated,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("❌ Error update desa anti korupsi:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal memperbarui data desa anti korupsi",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ import Elysia from "elysia";
|
||||
import MediaSosial from "./profile/media-sosial";
|
||||
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";
|
||||
|
||||
const LandingPage = new Elysia({
|
||||
prefix: "/api/landingpage",
|
||||
@@ -11,5 +13,7 @@ const LandingPage = new Elysia({
|
||||
.use(MediaSosial)
|
||||
.use(ProgramInovasi)
|
||||
.use(PejabatDesa)
|
||||
.use(KategoriDesaAntiKorupsi)
|
||||
.use(DesaAntiKorupsi)
|
||||
|
||||
export default LandingPage
|
||||
|
||||
Reference in New Issue
Block a user