- Update schema: add images relation list and linkVideo field - API: support multiple image upload and YouTube link in create/update - Admin create page: add gallery upload (max 10) and YouTube embed preview - Admin edit page: manage existing/new gallery images and YouTube link - Admin detail page: display gallery grid and YouTube video embed - Public detail page: show gallery images and YouTube video with responsive layout - State: add imageIds[] and linkVideo fields with proper type handling - Music player: fix seek functionality and ESLint warnings Breaking changes: - Prisma schema updated - requires migration - API create/update endpoints now expect imageIds array and linkVideo string Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
import prisma from "@/lib/prisma";
|
|
import { Context } from "elysia";
|
|
import { Prisma } from "@prisma/client";
|
|
import fs from "fs/promises";
|
|
import path from "path";
|
|
|
|
type FormUpdate = {
|
|
id: string;
|
|
judul: string;
|
|
deskripsi: string;
|
|
content: string;
|
|
kategoriBeritaId: string;
|
|
imageId: string; // Featured image
|
|
imageIds?: string[]; // Multiple images for gallery
|
|
linkVideo?: string; // YouTube link
|
|
};
|
|
|
|
async function beritaUpdate(context: Context) {
|
|
try {
|
|
const id = context.params?.id as string;
|
|
const body = (await context.body) as Omit<FormUpdate, "id">;
|
|
|
|
const { judul, deskripsi, content, kategoriBeritaId, imageId, imageIds, linkVideo } = body;
|
|
|
|
if (!id) {
|
|
return new Response(
|
|
JSON.stringify({ success: false, message: "ID tidak boleh kosong" }),
|
|
{ status: 400, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
const existing = await prisma.berita.findUnique({
|
|
where: { id },
|
|
include: {
|
|
image: true,
|
|
images: true, // Include gallery images
|
|
kategoriBerita: true,
|
|
},
|
|
});
|
|
|
|
if (!existing) {
|
|
return new Response(
|
|
JSON.stringify({ success: false, message: "Berita tidak ditemukan" }),
|
|
{ status: 404, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
// Delete old featured image if changed
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Build update data
|
|
const updateData: Prisma.BeritaUpdateInput = {
|
|
judul,
|
|
deskripsi,
|
|
content,
|
|
kategoriBerita: kategoriBeritaId ? { connect: { id: kategoriBeritaId } } : { disconnect: true },
|
|
image: imageId ? { connect: { id: imageId } } : { disconnect: true },
|
|
linkVideo,
|
|
};
|
|
|
|
// Handle multiple images update
|
|
if (imageIds !== undefined) {
|
|
// Disconnect all existing images first
|
|
updateData.images = {
|
|
set: [],
|
|
};
|
|
|
|
// Connect new images if provided
|
|
if (imageIds.length > 0) {
|
|
updateData.images = {
|
|
...updateData.images,
|
|
connect: imageIds.map((id) => ({ id })),
|
|
};
|
|
}
|
|
}
|
|
|
|
const updated = await prisma.berita.update({
|
|
where: { id },
|
|
data: updateData,
|
|
include: {
|
|
image: true,
|
|
images: true,
|
|
kategoriBerita: true,
|
|
},
|
|
});
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
message: "Berita berhasil diupdate",
|
|
data: updated,
|
|
}),
|
|
{ status: 200, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
} catch (error) {
|
|
console.error("Error updating berita:", error);
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: false,
|
|
message: "Terjadi kesalahan saat mengupdate berita",
|
|
}),
|
|
{ status: 500, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
}
|
|
export default beritaUpdate;
|