forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaceRoutes.js
More file actions
82 lines (71 loc) · 2 KB
/
Copy pathspaceRoutes.js
File metadata and controls
82 lines (71 loc) · 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
import express from "express";
import { protect } from "../middlewares/authMiddleware.js";
import upload from "../middlewares/upload.js";
import {
cacheMiddleware,
invalidateCacheMiddleware,
} from "../middlewares/cache.js";
import { CACHE_TTL, CACHE_KEYS } from "../utils/cache.js";
import {
getSpaces,
getSpaceById,
createSpace,
updateSpace,
joinWaitList,
deleteSpace,
getSpacesByHost,
} from "../controllers/spaceController.js";
const router = express.Router();
// Cache key generators
const spacesListCacheKey = () => `${CACHE_KEYS.SPACES}list`;
const spaceDetailCacheKey = (req) => `${CACHE_KEYS.SPACE}${req.params.id}`;
const spacesByHostCacheKey = (req) =>
`${CACHE_KEYS.SPACES}host:${req.params.hostId}`;
// Get all spaces - cached for 5 minutes (shorter TTL as spaces are time-sensitive)
router.get(
"/",
cacheMiddleware(CACHE_TTL.SPACES, spacesListCacheKey),
getSpaces
);
// Get all spaces by host (user) - cached for 5 minutes
router.get(
"/by-host/:hostId",
cacheMiddleware(CACHE_TTL.SPACES, spacesByHostCacheKey),
getSpacesByHost
);
// Get a single space by ID - cached for 5 minutes
router.get(
"/:id",
cacheMiddleware(CACHE_TTL.SPACES, spaceDetailCacheKey),
getSpaceById
);
// Create a new space - invalidates spaces cache
router.post(
"/",
protect,
upload.fields([{ name: "thumbnail", maxCount: 1 }]),
invalidateCacheMiddleware([`${CACHE_KEYS.SPACES}*`, `${CACHE_KEYS.EDUCATORS}*`]),
createSpace
);
// Join waitlist - invalidates space cache
router.post(
"/:id/waitlist",
protect,
invalidateCacheMiddleware([`${CACHE_KEYS.SPACE}*`]),
joinWaitList
);
// Update a space - invalidates space caches
router.put(
"/update/:id",
protect,
invalidateCacheMiddleware([`${CACHE_KEYS.SPACES}*`, `${CACHE_KEYS.SPACE}*`]),
updateSpace
);
// Delete a space - invalidates space caches
router.delete(
"/:id",
protect,
invalidateCacheMiddleware([`${CACHE_KEYS.SPACES}*`, `${CACHE_KEYS.SPACE}*`, `${CACHE_KEYS.EDUCATORS}*`]),
deleteSpace
);
export default router;