forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarkController.js
More file actions
129 lines (106 loc) · 3.2 KB
/
Copy pathbookmarkController.js
File metadata and controls
129 lines (106 loc) · 3.2 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import User from "../../models/User.js";
import Course from "../../models/Course.js";
import { catchAsync, APIError } from "../../middlewares/errorHandler.js";
import logger from "../../config/logger.js";
/**
* Toggle bookmark for a course
* If bookmarked, remove it. If not bookmarked, add it.
*/
export const toggleCourseBookmark = catchAsync(async (req, res, next) => {
const { courseId } = req.params;
const userId = req.user._id;
logger.info(`Toggle bookmark for course ${courseId} by user ${userId}`);
// Check if course exists
const course = await Course.findById(courseId);
if (!course) {
return next(new APIError("Course not found", 404));
}
// Get user
const user = await User.findById(userId);
if (!user) {
return next(new APIError("User not found", 404));
}
// Check if already bookmarked
const bookmarkIndex = user.bookmarkedCourses.indexOf(courseId);
let isBookmarked = false;
if (bookmarkIndex > -1) {
// Remove bookmark
user.bookmarkedCourses.splice(bookmarkIndex, 1);
isBookmarked = false;
logger.info(`Removed bookmark for course ${courseId}`);
} else {
// Add bookmark
user.bookmarkedCourses.push(courseId);
isBookmarked = true;
logger.info(`Added bookmark for course ${courseId}`);
}
await user.save();
res.status(200).json({
success: true,
message: isBookmarked
? "Course bookmarked successfully"
: "Bookmark removed successfully",
isBookmarked,
bookmarkedCourses: user.bookmarkedCourses,
});
});
/**
* Get all bookmarked courses for authenticated user
*/
export const getBookmarkedCourses = catchAsync(async (req, res, next) => {
const userId = req.user._id;
logger.info(`Fetching bookmarked courses for user ${userId}`);
const user = await User.findById(userId).populate({
path: "bookmarkedCourses",
populate: {
path: "createdBy",
select: "name email avatar",
},
});
if (!user) {
return next(new APIError("User not found", 404));
}
res.status(200).json({
success: true,
bookmarks: user.bookmarkedCourses,
count: user.bookmarkedCourses.length,
});
});
/**
* Check if a course is bookmarked by the user
*/
export const checkIfBookmarked = catchAsync(async (req, res, next) => {
const { courseId } = req.params;
const userId = req.user._id;
const user = await User.findById(userId);
if (!user) {
return next(new APIError("User not found", 404));
}
const isBookmarked = user.bookmarkedCourses.includes(courseId);
res.status(200).json({
success: true,
isBookmarked,
});
});
/**
* Remove a bookmark
*/
export const removeBookmark = catchAsync(async (req, res, next) => {
const { courseId } = req.params;
const userId = req.user._id;
logger.info(`Removing bookmark for course ${courseId} by user ${userId}`);
const user = await User.findById(userId);
if (!user) {
return next(new APIError("User not found", 404));
}
// Remove from bookmarks
user.bookmarkedCourses = user.bookmarkedCourses.filter(
(id) => id.toString() !== courseId.toString()
);
await user.save();
res.status(200).json({
success: true,
message: "Bookmark removed successfully",
bookmarkedCourses: user.bookmarkedCourses,
});
});