forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchController.js
More file actions
42 lines (33 loc) · 1.45 KB
/
Copy pathsearchController.js
File metadata and controls
42 lines (33 loc) · 1.45 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
import { searchCollections, searchEducators } from "../services/search/searchService.js";
import logger from "../config/logger.js";
export const searchAll = async (req, res) => {
try {
const { q, type = "all", page = 1, limit = 10, sort, minPrice, maxPrice, free, category, minRating, interest } = req.query;
if (q && q.trim().length > 100) {
return res.status(400).json({ success: false, message: "Query string is too long.", data: null });
}
const filters = { minPrice, maxPrice, free, category, minRating, interest };
const result = await searchCollections({ q: q ? q.trim() : "", type, page, limit, sort, filters });
res.json({ success: true, ...result });
} catch (err) {
logger.error("Search error:", err);
res.status(err.statusCode || 500).json({
success: false,
message: err.statusCode ? err.message : "Server error",
data: null,
});
}
};
export const searchEducatorsHandler = async (req, res) => {
try {
const { q, interest, page = 1, limit = 10 } = req.query;
if (q && q.trim().length > 100) {
return res.status(400).json({ success: false, error: "Query string is too long." });
}
const result = await searchEducators({ q: q ? q.trim() : "", interest, page, limit });
res.json({ success: true, ...result });
} catch (err) {
logger.error("Search educators error:", err);
res.status(500).json({ success: false, error: "Server error" });
}
};