Menerapkan pagination di submenu pegawai & berita
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -1,26 +1,48 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
// Di findMany.ts
|
||||
export default async function pegawaiFindMany(context: Context) {
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
export default async function pegawaiFindMany() {
|
||||
try {
|
||||
const pegawaiList = await prisma.pegawai.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: {
|
||||
posisi: true,
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.pegawai.findMany({
|
||||
where: { isActive: true },
|
||||
include: {
|
||||
posisi: true,
|
||||
image: true,
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.pegawai.count({
|
||||
where: { isActive: true }
|
||||
})
|
||||
]);
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: pegawaiList,
|
||||
message: "Success fetch pegawai with pagination",
|
||||
data,
|
||||
page,
|
||||
totalPages,
|
||||
total,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error findMany pegawai:", error);
|
||||
} catch (e) {
|
||||
console.error("Find many paginated error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data pegawai",
|
||||
error: error.message,
|
||||
message: "Failed fetch pegawai with pagination",
|
||||
data: [],
|
||||
page: 1,
|
||||
totalPages: 1,
|
||||
total: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user