2017-06-05 15:53:49 -04:00
|
|
|
import * as express from 'express'
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-10-19 03:43:01 -04:00
|
|
|
import { logger, getFormattedObjects } from '../../helpers'
|
2017-05-15 16:22:03 -04:00
|
|
|
import {
|
|
|
|
makeFriends,
|
2017-08-02 15:50:42 -04:00
|
|
|
quitFriends,
|
|
|
|
removeFriend
|
2017-05-15 16:22:03 -04:00
|
|
|
} from '../../lib'
|
|
|
|
import {
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
makeFriendsValidator,
|
2017-08-02 15:50:42 -04:00
|
|
|
setBodyHostsPort,
|
2017-10-19 03:43:01 -04:00
|
|
|
podRemoveValidator,
|
|
|
|
paginationValidator,
|
|
|
|
setPagination,
|
|
|
|
setPodsSort,
|
|
|
|
podsSortValidator
|
2017-05-15 16:22:03 -04:00
|
|
|
} from '../../middlewares'
|
2017-10-19 03:43:01 -04:00
|
|
|
import { PodInstance } from '../../models'
|
2017-05-15 16:22:03 -04:00
|
|
|
|
|
|
|
const podsRouter = express.Router()
|
|
|
|
|
2017-10-19 03:43:01 -04:00
|
|
|
podsRouter.get('/',
|
|
|
|
paginationValidator,
|
|
|
|
podsSortValidator,
|
|
|
|
setPodsSort,
|
|
|
|
setPagination,
|
|
|
|
listPods
|
2017-05-15 16:22:03 -04:00
|
|
|
)
|
2017-09-07 15:22:17 -04:00
|
|
|
podsRouter.post('/make-friends',
|
2017-05-15 16:22:03 -04:00
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
makeFriendsValidator,
|
|
|
|
setBodyHostsPort,
|
2017-05-22 14:58:25 -04:00
|
|
|
makeFriendsController
|
2017-05-15 16:22:03 -04:00
|
|
|
)
|
2017-09-07 15:22:17 -04:00
|
|
|
podsRouter.get('/quit-friends',
|
2017-05-15 16:22:03 -04:00
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
2017-05-22 14:58:25 -04:00
|
|
|
quitFriendsController
|
2017-05-15 16:22:03 -04:00
|
|
|
)
|
2017-08-02 15:50:42 -04:00
|
|
|
podsRouter.delete('/:id',
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
podRemoveValidator,
|
|
|
|
removeFriendController
|
|
|
|
)
|
2017-05-15 16:22:03 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
podsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-10-19 03:43:01 -04:00
|
|
|
db.Pod.listForApi(req.query.start, req.query.count, req.query.sort)
|
|
|
|
.then(resultList => res.json(getFormattedObjects(resultList.data, resultList.total)))
|
2017-07-05 07:26:25 -04:00
|
|
|
.catch(err => next(err))
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const hosts = req.body.hosts as string[]
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
makeFriends(hosts)
|
|
|
|
.then(() => logger.info('Made friends!'))
|
2017-07-07 12:26:12 -04:00
|
|
|
.catch(err => logger.error('Could not make friends.', err))
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
// Don't wait the process that could be long
|
2017-05-15 16:22:03 -04:00
|
|
|
res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-05 07:26:25 -04:00
|
|
|
quitFriends()
|
|
|
|
.then(() => res.type('json').status(204).end())
|
|
|
|
.catch(err => next(err))
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
2017-08-02 15:50:42 -04:00
|
|
|
|
|
|
|
function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const pod = res.locals.pod as PodInstance
|
|
|
|
|
|
|
|
removeFriend(pod)
|
2017-10-19 04:43:54 -04:00
|
|
|
.then(() => res.type('json').status(204).end())
|
2017-08-02 15:50:42 -04:00
|
|
|
.catch(err => next(err))
|
|
|
|
}
|