-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTemplates.ts
109 lines (93 loc) · 3.43 KB
/
Templates.ts
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
import type { Response } from 'express';
import express from 'express';
import * as TemplatesHandler from '../handler/templates/TemplatesHandler';
import * as PagesHandler from '../handler/templates/PagesHandler';
import * as PageGroupsHandler from '../handler/templates/PageGroupsHandler';
import { getTokenFromCookies } from '../utils';
import type {
GetInitialTemplateIdResponse,
GetPageGroupsResponse,
GetPagesResponse,
GetTemplatesResponse,
PostPageGroupsRequestBody,
PostPagesRequestBody,
PostTemplatesRequestBody,
PutPagesRequestBody,
PutTemplatesRequestBody
} from './Templates.types';
const router = express.Router();
router.use(express.json());
router.get('/', (_req, res: Response<GetTemplatesResponse>, next) => {
TemplatesHandler.listTemplates()
.then(templates => res.send(templates))
.catch(next);
});
router.post<never, never, PostTemplatesRequestBody>('/', (req, res, next) => {
TemplatesHandler.validateAndCreateTemplate(req.user!.username, req.body)
.then(() => res.status(200).end())
.catch(next);
});
router.put<never, never, PutTemplatesRequestBody>('/', (req, res, next) => {
TemplatesHandler.updateTemplate(req.user!.username, req.body)
.then(() => res.status(200).end())
.catch(next);
});
router.delete('/:templateId', (req, res, next) => {
TemplatesHandler.deleteTemplate(req.params.templateId)
.then(() => res.status(200).end())
.catch(next);
});
router.get('/initial', (req, res: Response<GetInitialTemplateIdResponse>, next) => {
TemplatesHandler.getInitialTemplateId(
req.user!.role,
req.user!.group_system_roles,
req.user!.tenants,
req.headers.tenant as string,
getTokenFromCookies(req)
)
.then(template => res.send(template))
.catch(next);
});
router.get('/page-groups', (_req, res: Response<GetPageGroupsResponse>, next) => {
try {
res.send(PageGroupsHandler.listPageGroups());
} catch (err) {
next(err);
}
});
router.post<never, never, PostPageGroupsRequestBody>('/page-groups', (req, res, next) => {
PageGroupsHandler.validateAndCreatePageGroup(req.user!.username, req.body)
.then(() => res.status(200).end())
.catch(next);
});
router.put('/page-groups/:groupId', (req, res, next) => {
PageGroupsHandler.updatePageGroup(req.user!.username, req.params.groupId, req.body)
.then(() => res.status(200).end())
.catch(next);
});
router.delete('/page-groups/:groupId', (req, res, next) => {
PageGroupsHandler.deletePageGroup(req.params.groupId)
.then(() => res.status(200).end())
.catch(next);
});
router.get('/pages', (_req, res: Response<GetPagesResponse>, next) => {
PagesHandler.listPages()
.then(pages => res.send(pages))
.catch(next);
});
router.post<never, never, PostPagesRequestBody>('/pages', (req, res, next) => {
PagesHandler.validateAndCreatePage(req.user!.username, { ...req.body, layout: [] })
.then(() => res.status(200).end())
.catch(next);
});
router.put<never, never, PutPagesRequestBody>('/pages', (req, res, next) => {
PagesHandler.updatePage(req.user!.username, req.body)
.then(() => res.status(200).end())
.catch(next);
});
router.delete('/pages/:pageId', (req, res, next) => {
PagesHandler.deletePage(req.params.pageId)
.then(() => res.status(200).end())
.catch(next);
});
export default router;