PMULabs_Zhimol/server/report.js
2023-12-26 16:16:13 +04:00

45 lines
1.3 KiB
JavaScript

module.exports = (req, res, next) => {
const isReportRequest = req.url.startsWith("/report") && req.method === "GET";
if (!isReportRequest) {
next();
return;
}
try {
const { startDate, endDate } = req.query;
delete require.cache[require.resolve("./data.json")];
const data = require("./data.json");
const filteredArticles = data.articles.filter((article) => {
const articleDate = new Date(article.publishDate);
return (
articleDate >= startDate && articleDate <= endDate
);
});
console.log(filteredArticles);
const articleComm = filteredArticles.map((article) => {
const commentsCount = data.comments.filter(
(comment) => comment.articleId === article.id
).length;
const tag = data.tags.filter(
(tag) => tag.id === article.tagId
)[0].title;
const user = data.users.filter(
(user) => user.id === article.userId
)[0].nickname;
return { title: article.title, publishDate: article.publishDate,tagName:tag, userName:user, comments: commentsCount };
});
articleComm.sort((a, b) => b.publishDate - a.publishDate)
console.log(articleComm);
res.json(articleComm);
} catch (error) {
console.error("Error processing report: ", error);
res.status(500).json({ message: "Internal Server Error" });
}
};