]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
65fcc311 | 2 | |
e02643f3 | 3 | import { database as db } from '../../initializers/database' |
8a02bd04 | 4 | import { logger, getFormattedObjects } from '../../helpers' |
65fcc311 | 5 | import { |
65fcc311 | 6 | makeFriends, |
d5f5a670 GS |
7 | quitFriends, |
8 | removeFriend | |
65fcc311 C |
9 | } from '../../lib' |
10 | import { | |
65fcc311 C |
11 | authenticate, |
12 | ensureIsAdmin, | |
13 | makeFriendsValidator, | |
d5f5a670 | 14 | setBodyHostsPort, |
8a02bd04 C |
15 | podRemoveValidator, |
16 | paginationValidator, | |
17 | setPagination, | |
18 | setPodsSort, | |
19 | podsSortValidator | |
65fcc311 | 20 | } from '../../middlewares' |
8a02bd04 | 21 | import { PodInstance } from '../../models' |
65fcc311 C |
22 | |
23 | const podsRouter = express.Router() | |
24 | ||
8a02bd04 C |
25 | podsRouter.get('/', |
26 | paginationValidator, | |
27 | podsSortValidator, | |
28 | setPodsSort, | |
29 | setPagination, | |
30 | listPods | |
65fcc311 | 31 | ) |
aa2e7f15 | 32 | podsRouter.post('/make-friends', |
65fcc311 C |
33 | authenticate, |
34 | ensureIsAdmin, | |
35 | makeFriendsValidator, | |
36 | setBodyHostsPort, | |
e02643f3 | 37 | makeFriendsController |
65fcc311 | 38 | ) |
aa2e7f15 | 39 | podsRouter.get('/quit-friends', |
65fcc311 C |
40 | authenticate, |
41 | ensureIsAdmin, | |
e02643f3 | 42 | quitFriendsController |
65fcc311 | 43 | ) |
d5f5a670 GS |
44 | podsRouter.delete('/:id', |
45 | authenticate, | |
46 | ensureIsAdmin, | |
47 | podRemoveValidator, | |
48 | removeFriendController | |
49 | ) | |
65fcc311 C |
50 | |
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | export { | |
54 | podsRouter | |
55 | } | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | ||
69818c93 | 59 | function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { |
8a02bd04 C |
60 | db.Pod.listForApi(req.query.start, req.query.count, req.query.sort) |
61 | .then(resultList => res.json(getFormattedObjects(resultList.data, resultList.total))) | |
6fcd19ba | 62 | .catch(err => next(err)) |
65fcc311 C |
63 | } |
64 | ||
69818c93 C |
65 | function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { |
66 | const hosts = req.body.hosts as string[] | |
65fcc311 | 67 | |
6fcd19ba C |
68 | makeFriends(hosts) |
69 | .then(() => logger.info('Made friends!')) | |
ad0997ad | 70 | .catch(err => logger.error('Could not make friends.', err)) |
65fcc311 | 71 | |
6fcd19ba | 72 | // Don't wait the process that could be long |
65fcc311 C |
73 | res.type('json').status(204).end() |
74 | } | |
75 | ||
69818c93 | 76 | function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { |
6fcd19ba C |
77 | quitFriends() |
78 | .then(() => res.type('json').status(204).end()) | |
79 | .catch(err => next(err)) | |
65fcc311 | 80 | } |
d5f5a670 GS |
81 | |
82 | function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
83 | const pod = res.locals.pod as PodInstance | |
84 | ||
85 | removeFriend(pod) | |
bda65bdc | 86 | .then(() => res.type('json').status(204).end()) |
d5f5a670 GS |
87 | .catch(err => next(err)) |
88 | } |