Fix UI Admin PPID
Profile PPID clear
This commit is contained in:
@@ -1,82 +0,0 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
import { writeFile, unlink } from "fs/promises";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { mkdir } from "fs/promises";
|
||||
|
||||
interface ProfilePPIDBody {
|
||||
id: string;
|
||||
file: Blob & { name: string; type: string };
|
||||
}
|
||||
|
||||
export default async function editImageProfilePPID(context: Context<{ body: ProfilePPIDBody }>) {
|
||||
const { id, file } = context.body;
|
||||
|
||||
if (!file || !id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "File atau ID harus disertakan",
|
||||
};
|
||||
}
|
||||
|
||||
// Validasi ekstensi file
|
||||
const allowedTypes = ["image/jpeg", "image/png", "image/webp"];
|
||||
if (!allowedTypes.includes(file.type)) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Tipe file tidak diizinkan. Hanya JPG, PNG, atau WEBP",
|
||||
};
|
||||
}
|
||||
|
||||
const bytes = await file.arrayBuffer();
|
||||
const buffer = Buffer.from(bytes);
|
||||
const filename = `${id}_${Date.now()}_${file.name}`;
|
||||
const folderPath = path.resolve("public", "assets", "images", "ppid", "profile-ppid");
|
||||
|
||||
try {
|
||||
await mkdir(folderPath, { recursive: true });
|
||||
|
||||
console.log('Folder path:', folderPath);
|
||||
const filePath = path.join(folderPath, filename);
|
||||
console.log('File path:', filePath);
|
||||
|
||||
await writeFile(filePath, buffer);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed to write file to disk",
|
||||
};
|
||||
}
|
||||
|
||||
const imageUrl = `/assets/images/ppid/profile-ppid/${filename}`;
|
||||
|
||||
// Hapus file lama (opsional, kalau mau bersih)
|
||||
const oldData = await prisma.profilePPID.findUnique({ where: { id } });
|
||||
if (oldData?.imageUrl) {
|
||||
const oldPath = path.resolve("public", oldData.imageUrl.replace(/^\//, ""));
|
||||
if (fs.existsSync(oldPath)) {
|
||||
await unlink(oldPath).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
// Update DB
|
||||
await prisma.profilePPID.update({
|
||||
where: { id },
|
||||
data: { imageUrl },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Gambar berhasil diunggah",
|
||||
url: imageUrl,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error uploading file:', error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengunggah gambar",
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,51 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function profilePPIDFindById(context: Context) {
|
||||
export default async function handler(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 {
|
||||
const id = context?.params?.slugs?.[0];
|
||||
|
||||
// If no ID provided, get the first profile
|
||||
if (!id) {
|
||||
const data = await prisma.profilePPID.findFirst();
|
||||
return {
|
||||
success: true,
|
||||
data,
|
||||
};
|
||||
if (typeof id !== 'string') {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "ID tidak valid",
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const data = await prisma.profilePPID.findUniqueOrThrow({
|
||||
const data = await prisma.profilePPID.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching profilePPID:", error);
|
||||
if (!data) {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Data tidak ditemukan",
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
return {
|
||||
return Response.json({
|
||||
success: true,
|
||||
message: "Berhasil mengambil data berdasarkan ID",
|
||||
data,
|
||||
}, { status: 200 });
|
||||
} catch (e) {
|
||||
console.error("Find by ID error:", e);
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Unknown error",
|
||||
};
|
||||
message: "Gagal mengambil data: " + (e instanceof Error ? e.message : 'Unknown error'),
|
||||
}, {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,30 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import profilePPIDFindById from "./find-by-id";
|
||||
import profilePPIDUpdate from "./update";
|
||||
import editImageProfilePPID from "./edit-img";
|
||||
|
||||
const ProfilePPID = new Elysia({
|
||||
prefix: "/profileppid",
|
||||
tags: ["PPID/Profile PPID"]
|
||||
})
|
||||
.get("/find-by-id", profilePPIDFindById)
|
||||
.post("/update", profilePPIDUpdate, {
|
||||
.get("/:id", async (context) => {
|
||||
const response = await profilePPIDFindById(new Request(context.request))
|
||||
return response
|
||||
})
|
||||
.put("/:id", async (context) => {
|
||||
const response = await profilePPIDUpdate(context)
|
||||
return response
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
id: t.String(),
|
||||
name: t.String(),
|
||||
biodata: t.String(),
|
||||
riwayat: t.String(),
|
||||
pengalaman: t.String(),
|
||||
unggulan: t.String(),
|
||||
imageId: t.String(),
|
||||
})
|
||||
})
|
||||
.post("/edit-img", editImageProfilePPID)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
export default ProfilePPID;
|
||||
|
||||
@@ -1,38 +1,121 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
type FormCreate = Prisma.ProfilePPIDGetPayload<{
|
||||
select: {
|
||||
id: true;
|
||||
name: true;
|
||||
biodata: true;
|
||||
riwayat: true;
|
||||
pengalaman: true;
|
||||
unggulan: true;
|
||||
}
|
||||
}>
|
||||
type FormUpdate = Prisma.ProfilePPIDGetPayload<{
|
||||
select: {
|
||||
id: true;
|
||||
name: true;
|
||||
biodata: true;
|
||||
riwayat: true;
|
||||
pengalaman: true;
|
||||
unggulan: true;
|
||||
imageId: true;
|
||||
};
|
||||
}>;
|
||||
export default async function profilePPIDUpdate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
try {
|
||||
const id = context.params?.id as string;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
await prisma.profilePPID.update({
|
||||
where: {
|
||||
id: body.id
|
||||
},
|
||||
data: {
|
||||
name: body.name,
|
||||
biodata: body.biodata,
|
||||
riwayat: body.riwayat,
|
||||
pengalaman: body.pengalaman,
|
||||
unggulan: body.unggulan,
|
||||
const { name, biodata, riwayat, pengalaman, unggulan, imageId } = body;
|
||||
|
||||
if (!id) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "ID tidak boleh kosong",
|
||||
}),
|
||||
{
|
||||
status: 400,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
const existing = await prisma.profilePPID.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Data tidak ditemukan",
|
||||
}),
|
||||
{
|
||||
status: 404,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (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 (error) {
|
||||
console.error("Gagal hapus gambar lama:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prisma.profilePPID.update({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
name,
|
||||
biodata,
|
||||
riwayat,
|
||||
pengalaman,
|
||||
imageId,
|
||||
unggulan,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: "Profile PPID Berhasil Dibuat",
|
||||
data: {
|
||||
...body,
|
||||
}
|
||||
}
|
||||
}
|
||||
data: updated,
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error updating profile PPID:", error);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat mengupdate profile PPID",
|
||||
}),
|
||||
{
|
||||
status: 500,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user