From 9d149e6abed98f9acabd498cf6e555011d19901e Mon Sep 17 00:00:00 2001 From: ARJUN S THAMPI <61703062+arjun-thampi@users.noreply.github.com> Date: Thu, 26 Mar 2026 11:30:26 +0530 Subject: [PATCH] feat: pagination in newMedia --- .../src/controllers/newsMedia.controller.js | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/backend/src/controllers/newsMedia.controller.js b/backend/src/controllers/newsMedia.controller.js index 1c743dc..32f8d20 100644 --- a/backend/src/controllers/newsMedia.controller.js +++ b/backend/src/controllers/newsMedia.controller.js @@ -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) {