Fix undefined ke detail berita terbaru
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
// app/api/check-update/route.ts
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Ambil berita terbaru
|
||||
const latestBerita = await prisma.berita.findFirst({
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, createdAt: true },
|
||||
});
|
||||
|
||||
// Ambil pengumuman terbaru
|
||||
const latestPengumuman = await prisma.pengumuman.findFirst({
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, createdAt: true },
|
||||
});
|
||||
|
||||
return Response.json({
|
||||
success: true,
|
||||
data: {
|
||||
berita: latestBerita
|
||||
? { id: latestBerita.id, createdAt: latestBerita.createdAt.toISOString() }
|
||||
: null,
|
||||
pengumuman: latestPengumuman
|
||||
? { id: latestPengumuman.id, createdAt: latestPengumuman.createdAt.toISOString() }
|
||||
: null,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error in /api/check-update:", error);
|
||||
return Response.json(
|
||||
{ success: false, message: "Gagal cek update" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
43
src/app/api/news/latest/route.ts
Normal file
43
src/app/api/news/latest/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
// app/api/news/latest/route.ts
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const berita = await prisma.berita.findMany({
|
||||
take: 3,
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: { kategoriBerita: true },
|
||||
});
|
||||
|
||||
const pengumuman = await prisma.pengumuman.findMany({
|
||||
take: 3,
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: { CategoryPengumuman: true },
|
||||
});
|
||||
|
||||
const news = [
|
||||
...berita.map((b) => ({
|
||||
id: b.id,
|
||||
type: "berita" as const,
|
||||
title: b.judul,
|
||||
content: b.content,
|
||||
timestamp: b.createdAt,
|
||||
kategoriBerita: b.kategoriBerita || undefined,
|
||||
})),
|
||||
...pengumuman.map((p) => ({
|
||||
id: p.id,
|
||||
type: "pengumuman" as const,
|
||||
title: p.judul,
|
||||
content: p.content,
|
||||
timestamp: p.createdAt,
|
||||
kategoriPengumuman: p.CategoryPengumuman || undefined,
|
||||
})),
|
||||
];
|
||||
|
||||
return NextResponse.json({ success: true, news }); // ✅ ganti 'data' jadi 'news'
|
||||
} catch (error) {
|
||||
console.error("API Error:", error);
|
||||
return NextResponse.json({ success: false, error: "Gagal memuat data" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user