forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarkBookController.js
More file actions
154 lines (132 loc) · 3.92 KB
/
Copy pathbookmarkBookController.js
File metadata and controls
154 lines (132 loc) · 3.92 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import mongoose from "mongoose";
import User from "../../models/User.js";
import Book from "../../models/Book.js";
import logger from "../../config/logger.js";
export const toggleBookBookmark = async (req, res) => {
try {
const { bookId } = req.params;
const userId = req.user._id;
if (!mongoose.Types.ObjectId.isValid(bookId)) {
return res
.status(400)
.json({ success: false, message: "Invalid book ID format" });
}
const book = await Book.findById(bookId);
if (!book) {
return res
.status(404)
.json({ success: false, message: "Book not found" });
}
const user = await User.findById(userId);
if (!user) {
return res
.status(404)
.json({ success: false, message: "User not found" });
}
if (!Array.isArray(user.bookmarkedBooks)) {
user.bookmarkedBooks = [];
}
const existingIndex = user.bookmarkedBooks.findIndex(
(id) => id.toString() === bookId
);
let message = "";
let isBookmarked = false;
if (existingIndex > -1) {
user.bookmarkedBooks.splice(existingIndex, 1);
message = "Book removed from bookmarks";
logger.info(`Removed bookmark for book ${bookId} by user ${userId}`);
} else {
user.bookmarkedBooks.push(bookId);
message = "Book bookmarked successfully";
isBookmarked = true;
logger.info(`Book ${bookId} bookmarked by user ${userId}`);
}
await user.save();
return res.status(200).json({
success: true,
isBookmarked,
message,
bookmarks: user.bookmarkedBooks,
});
} catch (error) {
logger.error("Error toggling book bookmark:", error);
return res
.status(500)
.json({ success: false, message: "Failed to toggle bookmark" });
}
};
export const getBookmarkedBooks = async (req, res) => {
try {
const user = await User.findById(req.user._id).populate({
path: "bookmarkedBooks",
populate: {
path: "author",
select: "name avatar role",
},
});
if (!user) {
return res
.status(404)
.json({ success: false, message: "User not found" });
}
return res.status(200).json({
success: true,
bookmarks: user.bookmarkedBooks || [],
count: user.bookmarkedBooks?.length || 0,
});
} catch (error) {
logger.error("Error fetching bookmarked books:", error);
return res
.status(500)
.json({ success: false, message: "Failed to fetch bookmarked books" });
}
};
export const checkIfBookBookmarked = async (req, res) => {
try {
const { bookId } = req.params;
const user = await User.findById(req.user._id).select("bookmarkedBooks");
if (!user) {
return res
.status(404)
.json({ success: false, message: "User not found" });
}
const isBookmarked = user.bookmarkedBooks?.some(
(id) => id.toString() === bookId
);
return res.status(200).json({
success: true,
isBookmarked: Boolean(isBookmarked),
});
} catch (error) {
logger.error("Error checking book bookmark status:", error);
return res.status(500).json({
success: false,
message: "Failed to check bookmark status",
});
}
};
export const removeBookBookmark = async (req, res) => {
try {
const { bookId } = req.params;
const user = await User.findById(req.user._id);
if (!user) {
return res
.status(404)
.json({ success: false, message: "User not found" });
}
user.bookmarkedBooks = (user.bookmarkedBooks || []).filter(
(id) => id.toString() !== bookId
);
await user.save();
return res.status(200).json({
success: true,
message: "Bookmark removed successfully",
bookmarks: user.bookmarkedBooks,
});
} catch (error) {
logger.error("Error removing book bookmark:", error);
return res
.status(500)
.json({ success: false, message: "Failed to remove bookmark" });
}
};