2026-05-26 15:48:01 +05:30
|
|
|
import prisma from '../prisma/client.js';
|
2026-03-25 17:59:36 +05:30
|
|
|
|
|
|
|
|
// GET ALL NEWS
|
|
|
|
|
|
|
|
|
|
export const getAllNews = async (req, res) => {
|
|
|
|
|
try {
|
2026-05-05 10:48:18 +05:30
|
|
|
const page = req.query.page ? parseInt(req.query.page) : null;
|
|
|
|
|
const limit = req.query.limit ? parseInt(req.query.limit) : null;
|
2026-05-26 15:48:01 +05:30
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
mode: 'insensitive',
|
2026-04-24 17:25:47 +05:30
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: {};
|
|
|
|
|
|
|
|
|
|
const whereCondition = {
|
|
|
|
|
...searchFilter,
|
|
|
|
|
};
|
2026-03-26 11:30:26 +05:30
|
|
|
|
2026-05-05 10:48:18 +05:30
|
|
|
const skip = page && limit ? (page - 1) * limit : undefined;
|
|
|
|
|
const take = limit ? limit : undefined;
|
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-05-26 15:48:01 +05:30
|
|
|
orderBy: { createdAt: 'desc' },
|
2026-03-26 11:20:03 +05:30
|
|
|
skip,
|
2026-05-05 10:48:18 +05:30
|
|
|
take,
|
2026-04-24 17:25:47 +05:30
|
|
|
}),
|
|
|
|
|
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-05-05 10:48:18 +05:30
|
|
|
page: page || 1,
|
|
|
|
|
limit: limit || total,
|
|
|
|
|
totalPages: limit ? Math.ceil(total / limit) : 1,
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to fetch news',
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// 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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'News not found',
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to fetch news',
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CREATE NEWS
|
|
|
|
|
|
|
|
|
|
export const createNews = async (req, res) => {
|
|
|
|
|
try {
|
2026-05-26 15:48:01 +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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Headline is required',
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'News created successfully',
|
2026-03-25 17:59:36 +05:30
|
|
|
data: news,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
success: false,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to create news',
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'News updated successfully',
|
2026-03-25 17:59:36 +05:30
|
|
|
data: news,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
success: false,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to update news',
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'News deleted successfully',
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
success: false,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to delete news',
|
2026-03-25 17:59:36 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|