forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchRoutes.js
More file actions
25 lines (20 loc) · 1.06 KB
/
Copy pathsearchRoutes.js
File metadata and controls
25 lines (20 loc) · 1.06 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
import express from "express";
import { searchAll, searchEducatorsHandler } from "../controllers/searchController.js";
import { cacheMiddleware } from "../middlewares/cache.js";
import { CACHE_TTL, CACHE_KEYS } from "../utils/cache.js";
const router = express.Router();
// Cache key generator for search queries
const searchCacheKey = (req) => {
const query = req.query.q || req.query.query || "";
const type = req.query.type || "all";
const page = req.query.page || 1;
const limit = req.query.limit || 10;
const filterKeys = ['minPrice', 'maxPrice', 'free', 'category', 'minRating', 'interest', 'sort'];
const filtersStr = filterKeys.map(k => `${k}=${req.query[k] || ''}`).join('&');
return `${CACHE_KEYS.SEARCH}${req.path}:${type}:${query.toLowerCase().trim()}:page=${page}:limit=${limit}:${filtersStr}`;
};
// Main search endpoint
router.get("/", cacheMiddleware(CACHE_TTL.SEARCH, searchCacheKey), searchAll);
// Dedicated educators endpoint
router.get("/educators", cacheMiddleware(CACHE_TTL.SEARCH, searchCacheKey), searchEducatorsHandler);
export default router;