2026-03-25 17:59:36 +05:30
|
|
|
import prisma from "../prisma/client.js";
|
|
|
|
|
|
|
|
|
|
// GET ALL NEWS
|
|
|
|
|
|
|
|
|
|
export const getAllNews = async (req, res) => {
|
|
|
|
|
try {
|
2026-04-24 17:25:47 +05:30
|
|
|
const page = parseInt(req.query.page) || 1;
|
|
|
|
|
const limit = parseInt(req.query.limit) || 10;
|
|
|
|
|
const search = req.query.search?.trim() || "";
|
2026-03-26 11:20:03 +05:30
|
|
|
|
2026-04-14 17:33:21 +05:30
|
|
|
const includeImages = {
|
|
|
|
|
images: true,
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-24 17:25:47 +05:30
|
|
|
const searchFilter = search
|
|
|
|
|
? {
|
|
|
|
|
headline: {
|
|
|
|
|
contains: search,
|
|
|
|
|
mode: "insensitive",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: {};
|
|
|
|
|
|
|
|
|
|
const whereCondition = {
|
|
|
|
|
...searchFilter,
|
|
|
|
|
};
|
2026-03-26 11:30:26 +05:30
|
|
|
|
2026-04-24 17:25:47 +05:30
|
|
|
const skip = (page - 1) * limit;
|
2026-03-26 11:20:03 +05:30
|
|
|
|
|
|
|
|
const [news, total] = await Promise.all([
|
|
|
|
|
prisma.newsMedia.findMany({
|
2026-04-24 17:25:47 +05:30
|
|
|
where: whereCondition,
|
2026-04-14 17:33:21 +05:30
|
|
|
include: includeImages,
|
2026-03-26 11:20:03 +05:30
|
|
|
orderBy: { createdAt: "desc" },
|
|
|
|
|
skip,
|
2026-04-24 17:25:47 +05:30
|
|
|
take: limit,
|
|
|
|
|
}),
|
|
|
|
|
prisma.newsMedia.count({
|
|
|
|
|
where: whereCondition,
|
2026-03-26 11:20:03 +05:30
|
|
|
}),
|
|
|
|
|
]);
|
2026-03-25 17:59:36 +05:30
|
|
|
|
|
|
|
|
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,
|
2026-04-14 17:33:21 +05:30
|
|
|
Images: n.images.map((img) => ({
|
|
|
|
|
id: img.id,
|
|
|
|
|
image: img.url,
|
|
|
|
|
})),
|
2026-03-25 17:59:36 +05:30
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: response,
|
2026-03-26 11:20:03 +05:30
|
|
|
meta: {
|
|
|
|
|
total,
|
2026-04-24 17:25:47 +05:30
|
|
|
page,
|
|
|
|
|
limit,
|
|
|
|
|
totalPages: Math.ceil(total / limit),
|
2026-03-26 11:20:03 +05:30
|
|
|
},
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch news",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// GET NEWS BY ID
|
|
|
|
|
|
|
|
|
|
export const getNewsById = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
|
|
const n = await prisma.newsMedia.findUnique({
|
|
|
|
|
where: { id: Number(id) },
|
2026-04-14 17:33:21 +05:30
|
|
|
include: { images: true },
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!n) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "News not found",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
Id: n.id.toString(),
|
|
|
|
|
Headline: n.headline,
|
|
|
|
|
Content: n.content,
|
|
|
|
|
FirstPara: n.firstPara,
|
|
|
|
|
SecondPara: n.secondPara,
|
|
|
|
|
Date: n.date,
|
|
|
|
|
Author: n.author,
|
2026-04-14 17:33:21 +05:30
|
|
|
Images: n.images.map((img) => ({
|
|
|
|
|
id: img.id,
|
|
|
|
|
image: img.url,
|
|
|
|
|
})),
|
2026-03-25 17:59:36 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: response,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch news",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CREATE NEWS
|
|
|
|
|
|
|
|
|
|
export const createNews = async (req, res) => {
|
|
|
|
|
try {
|
2026-04-14 17:33:21 +05:30
|
|
|
const {
|
|
|
|
|
headline,
|
|
|
|
|
content,
|
|
|
|
|
firstPara,
|
|
|
|
|
secondPara,
|
|
|
|
|
date,
|
|
|
|
|
author,
|
|
|
|
|
imageUrls,
|
|
|
|
|
} = req.body;
|
2026-03-25 17:59:36 +05:30
|
|
|
|
|
|
|
|
if (!headline) {
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Headline is required",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const news = await prisma.newsMedia.create({
|
|
|
|
|
data: {
|
|
|
|
|
headline,
|
|
|
|
|
content,
|
|
|
|
|
firstPara,
|
|
|
|
|
secondPara,
|
|
|
|
|
date: date ? new Date(date) : null,
|
|
|
|
|
author,
|
2026-04-14 17:33:21 +05:30
|
|
|
images: imageUrls
|
|
|
|
|
? {
|
|
|
|
|
create: imageUrls.map((url) => ({ url })),
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2026-03-25 17:59:36 +05:30
|
|
|
},
|
2026-04-14 17:33:21 +05:30
|
|
|
include: { images: true },
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return res.status(201).json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "News created successfully",
|
|
|
|
|
data: news,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to create news",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// UPDATE NEWS
|
|
|
|
|
|
|
|
|
|
export const updateNews = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
2026-04-14 17:33:21 +05:30
|
|
|
const { imageUrls, ...otherData } = req.body;
|
2026-03-25 17:59:36 +05:30
|
|
|
|
|
|
|
|
const news = await prisma.newsMedia.update({
|
|
|
|
|
where: { id: Number(id) },
|
|
|
|
|
data: {
|
2026-04-14 17:33:21 +05:30
|
|
|
...otherData,
|
2026-03-25 17:59:36 +05:30
|
|
|
date: req.body.date ? new Date(req.body.date) : undefined,
|
2026-04-14 17:33:21 +05:30
|
|
|
images: imageUrls
|
|
|
|
|
? {
|
|
|
|
|
deleteMany: {},
|
|
|
|
|
create: imageUrls.map((url) => ({ url })),
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2026-03-25 17:59:36 +05:30
|
|
|
},
|
2026-04-14 17:33:21 +05:30
|
|
|
include: { images: true },
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "News updated successfully",
|
|
|
|
|
data: news,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to update news",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// DELETE NEWS
|
|
|
|
|
|
|
|
|
|
export const deleteNews = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
|
|
await prisma.newsMedia.delete({
|
|
|
|
|
where: { id: Number(id) },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "News deleted successfully",
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to delete news",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|