feat: pagination in newMedia
This commit is contained in:
@@ -4,16 +4,41 @@ import prisma from "../prisma/client.js";
|
||||
|
||||
export const getAllNews = async (req, res) => {
|
||||
try {
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const limit = parseInt(req.query.limit) || 10;
|
||||
const page = parseInt(req.query.page);
|
||||
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([
|
||||
prisma.newsMedia.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
skip,
|
||||
take: limit,
|
||||
take: currentLimit,
|
||||
}),
|
||||
prisma.newsMedia.count(),
|
||||
]);
|
||||
@@ -33,9 +58,9 @@ export const getAllNews = async (req, res) => {
|
||||
data: response,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
page: currentPage,
|
||||
limit: currentLimit,
|
||||
totalPages: Math.ceil(total / currentLimit),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user