feat: add page newMedia
This commit is contained in:
@@ -4,9 +4,19 @@ import prisma from "../prisma/client.js";
|
||||
|
||||
export const getAllNews = async (req, res) => {
|
||||
try {
|
||||
const news = await prisma.newsMedia.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const limit = parseInt(req.query.limit) || 10;
|
||||
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [news, total] = await Promise.all([
|
||||
prisma.newsMedia.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
skip,
|
||||
take: limit,
|
||||
}),
|
||||
prisma.newsMedia.count(),
|
||||
]);
|
||||
|
||||
const response = news.map((n) => ({
|
||||
Id: n.id.toString(),
|
||||
@@ -21,6 +31,12 @@ export const getAllNews = async (req, res) => {
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
data: response,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
Reference in New Issue
Block a user