Menerapkan pagination di submenu pegawai & berita

This commit is contained in:
2025-07-10 10:46:58 +08:00
parent 7b2b306849
commit 2bc9b2f3c6
6 changed files with 566 additions and 400 deletions

View File

@@ -1,27 +1,44 @@
// /api/berita/findManyPaginated.ts
import prisma from "@/lib/prisma";
import { Context } from "elysia";
async function beritaFindManyPaginated(context: Context) {
const page = Number(context.query.page) || 1;
const limit = Number(context.query.limit) || 10;
const skip = (page - 1) * limit;
async function beritaFindMany() {
try {
const data = await prisma.berita.findMany({
where: { isActive: true },
include: {
image: true,
kategoriBerita: true,
},
});
const [data, total] = await Promise.all([
prisma.berita.findMany({
where: { isActive: true },
include: {
image: true,
kategoriBerita: true,
},
skip,
take: limit,
orderBy: { createdAt: 'desc' }, // opsional, kalau mau urut berdasarkan waktu
}),
prisma.berita.count({
where: { isActive: true }
})
]);
return {
success: true,
message: "Success fetch berita",
message: "Success fetch berita with pagination",
data,
page,
totalPages: Math.ceil(total / limit),
total,
};
} catch (e) {
console.error("Find many error:", e);
console.error("Find many paginated error:", e);
return {
success: false,
message: "Failed fetch berita",
message: "Failed fetch berita with pagination",
};
}
}
export default beritaFindMany;
export default beritaFindManyPaginated;