forked from codeaashu/DevDisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.controllers.js
More file actions
97 lines (89 loc) · 3.4 KB
/
dev.controllers.js
File metadata and controls
97 lines (89 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import * as Dev_models from "../models/post.models.js";
import * as responseClass from "../utils/response.utils.js";
import { pickRandomPeriod } from "../utils/randomPeriod.utils.js"
// Helper function to fetch posts based on time period
const fetchPostsForPeriod = async (timePeriod) => {
let model;
switch (timePeriod) {
case "daily":
model = Dev_models.Post_daily;
break;
case "weekly":
model = Dev_models.Post_weekly;
break;
case "monthly":
model = Dev_models.Post_monthly;
break;
default:
throw new responseClass.ApiError(400, "Invalid time period specified.");
}
// Fetching posts for the chosen period
const response = await model.find();
if (!response || response.length === 0) {
throw new responseClass.ApiError(404, `No trending posts found for time period: '${timePeriod}'`);
}
return response;
};
const fetchTrendingPosts = async (req, res) => {
// Extract the "since" parameter from the URL, defaulting to "daily" if not provided
const { since = 'daily' } = req.params;
try {
// Fetch posts based on the time period
const posts = await fetchPostsForPeriod(since);
// Return a consistent response with the fetched posts
return res.status(200).json(new responseClass.ApiResponse(
200,
`Fetched trending posts for time period: '${since}'`,
posts
));
} catch (error) {
// Generic error handling for unexpected errors
console.error(error); // Optional: Add logging to track errors
return res.status(500).json(new responseClass.ApiResponse(
500,
`Something went wrong while fetching trending posts for time period: '${since}' --> \n${error.message || error}`
));
}
}
const fetchRandomPost = async (req, res) => {
const since = pickRandomPeriod();
try {
let posts;
switch (since) {
case "daily":
posts = await Dev_models.Post_daily.aggregate([
{ $sample: { size: 1 } }
])
break;
case "weekly":
posts = await Dev_models.Post_weekly.aggregate([
{ $sample: { size: 1 } }
])
break;
case "monthly":
posts = await Dev_models.Post_monthly.aggregate([
{ $sample: { size: 1 } }
])
break;
default:
posts = await Dev_models.Post_daily.aggregate([
{ $sample: { size: 1 } }
])
break;
}
if (posts.length > 0) {
console.log('Random Document:', posts[0]);
return res.status(200).json(new responseClass.ApiResponse(200, `Fetched random post for time period: '${since}'`, posts[0]));
} else {
console.log('No documents found.');
return res.status(404).json(new responseClass.ApiResponse(404, `No random posts found for time period: '${since}'`));
}
} catch (error) {
console.error(error);
return res.status(500).json(new responseClass.ApiResponse(500, `Something went wrong while fetching random post for time period: '${since}' --> \n${error.message || error}`));
}
}
export {
fetchTrendingPosts,
fetchRandomPost
};