Sinkronisasi UI & API Admin - User Submenu Profile, Menu Desa
This commit is contained in:
@@ -10,6 +10,7 @@ import Penghargaan from "./penghargaan";
|
||||
import KategoriPotensi from "./potensi/kategori-potensi";
|
||||
import KategoriBerita from "./berita/kategori-berita";
|
||||
import KategoriPengumuman from "./pengumuman/kategori-pengumuman";
|
||||
import MantanPerbekel from "./profile/profile-mantan-perbekel";
|
||||
|
||||
|
||||
const Desa = new Elysia({ prefix: "/api/desa", tags: ["Desa"] })
|
||||
@@ -19,10 +20,12 @@ const Desa = new Elysia({ prefix: "/api/desa", tags: ["Desa"] })
|
||||
.use(PotensiDesa)
|
||||
.use(GalleryFoto)
|
||||
.use(GalleryVideo)
|
||||
.use(MantanPerbekel)
|
||||
.use(LayananDesa)
|
||||
.use(Penghargaan)
|
||||
.use(KategoriPotensi)
|
||||
.use(KategoriBerita)
|
||||
.use(KategoriPengumuman)
|
||||
|
||||
|
||||
export default Desa;
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = {
|
||||
nama: string;
|
||||
periode: string;
|
||||
imageId: string;
|
||||
daerah: string;
|
||||
}
|
||||
|
||||
export default async function profileMantanPerbekelCreate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
await prisma.perbekelDariMasaKeMasa.create({
|
||||
data: {
|
||||
nama: body.nama,
|
||||
periode: body.periode,
|
||||
imageId: body.imageId,
|
||||
daerah: body.daerah,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create profile mantan perbekel",
|
||||
data: {
|
||||
...body,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
const profileMantanPerbekelDelete = async (context: Context) => {
|
||||
const id = context.params?.id as string;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
status: 400,
|
||||
body: "ID tidak diberikan",
|
||||
};
|
||||
}
|
||||
|
||||
const foto = await prisma.perbekelDariMasaKeMasa.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!foto) {
|
||||
return {
|
||||
status: 404,
|
||||
body: "Foto tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
// Hapus file gambar dari filesystem jika ada
|
||||
if (foto.image) {
|
||||
try {
|
||||
const filePath = path.join(foto.image.path, foto.image.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: foto.image.id },
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Gagal hapus gambar lama:", err);
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.perbekelDariMasaKeMasa.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
body: "Foto berhasil dihapus",
|
||||
};
|
||||
}
|
||||
|
||||
export default profileMantanPerbekelDelete
|
||||
@@ -0,0 +1,58 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
// /api/berita/findManyPaginated.ts
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
async function profileMantanPerbekelFindMany(context: Context) {
|
||||
// Ambil parameter dari query
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const search = (context.query.search as string) || '';
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// Buat where clause
|
||||
const where: any = { isActive: true };
|
||||
|
||||
// Tambahkan pencarian (jika ada)
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ nama: { contains: search, mode: 'insensitive' } },
|
||||
{ periode: { contains: search, mode: 'insensitive' } },
|
||||
{ daerah: { contains: search, mode: 'insensitive' } },
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
// Ambil data dan total count secara paralel
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.perbekelDariMasaKeMasa.findMany({
|
||||
where,
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { periode: 'asc' },
|
||||
}),
|
||||
prisma.perbekelDariMasaKeMasa.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil ambil foto dengan pagination",
|
||||
data,
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Error di findMany paginated:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data foto",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default profileMantanPerbekelFindMany;
|
||||
@@ -0,0 +1,49 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function profileMantanPerbekelFindUnique(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.perbekelDariMasaKeMasa.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Profile mantan perbekel tidak ditemukan",
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
success: true,
|
||||
message: "Success fetch profile mantan perbekel by ID",
|
||||
data,
|
||||
}, { status: 200 });
|
||||
} catch (e) {
|
||||
console.error("Find by ID error:", e);
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Gagal mengambil profile mantan perbekel: " + (e instanceof Error ? e.message : 'Unknown error'),
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import profileMantanPerbekelCreate from "./create";
|
||||
import profileMantanPerbekelDelete from "./del";
|
||||
import profileMantanPerbekelFindMany from "./findMany";
|
||||
import profileMantanPerbekelUpdate from "./updt";
|
||||
import profileMantanPerbekelFindUnique from "./findUnique";
|
||||
|
||||
const MantanPerbekel = new Elysia({
|
||||
prefix: "mantanperbekel",
|
||||
tags: ["Desa/Profile/MantanPerbekel"],
|
||||
})
|
||||
.get("/findMany", profileMantanPerbekelFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await profileMantanPerbekelFindUnique(
|
||||
new Request(context.request)
|
||||
);
|
||||
return response;
|
||||
})
|
||||
.post("/create", profileMantanPerbekelCreate, {
|
||||
body: t.Object({
|
||||
nama: t.String(),
|
||||
daerah: t.String(),
|
||||
periode: t.String(),
|
||||
imageId: t.String(),
|
||||
}),
|
||||
})
|
||||
.delete("/del/:id", profileMantanPerbekelDelete)
|
||||
.put(
|
||||
"/:id",
|
||||
async (context) => {
|
||||
const response = await profileMantanPerbekelUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
nama: t.String(),
|
||||
daerah: t.String(),
|
||||
periode: t.String(),
|
||||
imageId: t.String(),
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
export default MantanPerbekel;
|
||||
@@ -0,0 +1,82 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
type FormUpdate = Prisma.PerbekelDariMasaKeMasaGetPayload<{
|
||||
select: {
|
||||
id: true;
|
||||
nama: true;
|
||||
periode: true;
|
||||
imageId: true;
|
||||
daerah: true;
|
||||
};
|
||||
}>;
|
||||
|
||||
export default async function profileMantanPerbekelUpdate(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
const { nama, periode, imageId, daerah } = body;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID tidak diberikan",
|
||||
};
|
||||
}
|
||||
|
||||
const existing = await prisma.perbekelDariMasaKeMasa.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Profile mantan perbekel tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
if (existing.imageId && existing.imageId !== imageId) {
|
||||
const oldImage = existing.image;
|
||||
if (oldImage) {
|
||||
try {
|
||||
const filePath = path.join(oldImage.path, oldImage.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: oldImage.id },
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Gagal hapus gambar lama:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prisma.perbekelDariMasaKeMasa.update({
|
||||
where: { id },
|
||||
data: {
|
||||
nama,
|
||||
periode,
|
||||
imageId,
|
||||
daerah,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success update profile mantan perbekel",
|
||||
data: updated,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error updating profile mantan perbekel:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat mengupdate profile mantan perbekel",
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user