forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecommendedBooksController.js
More file actions
57 lines (50 loc) · 1.79 KB
/
Copy pathrecommendedBooksController.js
File metadata and controls
57 lines (50 loc) · 1.79 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
import logger from "../../config/logger.js";
import Book from "../../models/Book.js";
import { getCacheOrSet, CACHE_TTL, CACHE_KEYS } from "../../utils/cache.js";
/**
* Get recommended books based on user interests
* Matches books by category with user's interests
*/
export const getRecommendedBooks = async (req, res) => {
try {
const { interests } = req.body;
const hasInterests = Array.isArray(interests) && interests.length > 0;
// This endpoint is POST (interests come in the body), so the shared
// cacheMiddleware (GET-only) can't key off req.query - cache explicitly here instead.
const cacheKey = hasInterests
? `${CACHE_KEYS.BOOKS}recommended:${[...interests].sort().join(",")}`
: `${CACHE_KEYS.BOOKS}recommended:popular`;
const payload = await getCacheOrSet(
cacheKey,
async () => {
if (!hasInterests) {
// If no interests provided, return recent/popular books
const books = await Book.find()
.populate("author", "name email avatar")
.sort({ readCount: -1 }) // Sort by popularity
.limit(10);
return { books, message: "Popular books" };
}
// Find books that match user interests
const recommended = await Book.find({
category: { $in: interests },
})
.populate("author", "name email avatar")
.sort({ readCount: -1 })
.limit(10);
return {
recommended,
message: "Books recommended based on your interests",
};
},
CACHE_TTL.BOOKS
);
res.status(200).json({ success: true, ...payload });
} catch (error) {
logger.error("Error fetching recommended books:", error);
res.status(500).json({
success: false,
message: error.message,
});
}
};