feat: pagination in newMedia

This commit is contained in:
ARJUN S THAMPI
2026-03-26 11:30:26 +05:30
parent 2ed1bee149
commit 9d149e6abe
@@ -4,16 +4,41 @@ import prisma from "../prisma/client.js";
export const getAllNews = async (req, res) => { export const getAllNews = async (req, res) => {
try { try {
const page = parseInt(req.query.page) || 1; const page = parseInt(req.query.page);
const limit = parseInt(req.query.limit) || 10; const limit = parseInt(req.query.limit);
const skip = (page - 1) * limit; if (!page && !limit) {
const news = await prisma.newsMedia.findMany({
orderBy: { createdAt: "desc" },
});
const response = news.map((n) => ({
Id: n.id.toString(),
Headline: n.headline,
Content: n.content,
FirstPara: n.firstPara,
SecondPara: n.secondPara,
Date: n.date,
Author: n.author,
}));
return res.status(200).json({
success: true,
data: response,
meta: null,
});
}
const currentPage = page || 1;
const currentLimit = limit || 10;
const skip = (currentPage - 1) * currentLimit;
const [news, total] = await Promise.all([ const [news, total] = await Promise.all([
prisma.newsMedia.findMany({ prisma.newsMedia.findMany({
orderBy: { createdAt: "desc" }, orderBy: { createdAt: "desc" },
skip, skip,
take: limit, take: currentLimit,
}), }),
prisma.newsMedia.count(), prisma.newsMedia.count(),
]); ]);
@@ -33,9 +58,9 @@ export const getAllNews = async (req, res) => {
data: response, data: response,
meta: { meta: {
total, total,
page, page: currentPage,
limit, limit: currentLimit,
totalPages: Math.ceil(total / limit), totalPages: Math.ceil(total / currentLimit),
}, },
}); });
} catch (error) { } catch (error) {