2016-05-07 18:52:08 +00:00
|
|
|
import Router from 'koa-router';
|
|
|
|
import httpErrors from 'http-errors';
|
2016-08-22 06:40:57 +00:00
|
|
|
import _ from 'lodash';
|
2016-05-07 18:52:08 +00:00
|
|
|
|
2016-08-27 17:48:56 +00:00
|
|
|
import auth from './middlewares/authentication';
|
2016-05-07 18:52:08 +00:00
|
|
|
import pagination from './middlewares/pagination';
|
2016-08-05 15:09:14 +00:00
|
|
|
import { presentCollection } from '../presenters';
|
2016-07-26 06:01:14 +00:00
|
|
|
import { Atlas } from '../models';
|
2016-05-07 18:52:08 +00:00
|
|
|
|
|
|
|
const router = new Router();
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
router.post('collections.create', auth(), async (ctx) => {
|
2016-07-26 06:01:14 +00:00
|
|
|
const {
|
2016-07-22 05:06:30 +00:00
|
|
|
name,
|
|
|
|
description,
|
|
|
|
type,
|
2016-07-22 07:11:54 +00:00
|
|
|
} = ctx.body;
|
2016-07-22 05:06:30 +00:00
|
|
|
ctx.assertPresent(name, 'name is required');
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
|
|
|
|
const atlas = await Atlas.create({
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
type: type || 'atlas',
|
|
|
|
teamId: user.teamId,
|
2016-07-26 07:05:10 +00:00
|
|
|
creatorId: user.id,
|
2016-07-22 05:06:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.body = {
|
2016-08-15 10:51:26 +00:00
|
|
|
data: await presentCollection(ctx, atlas, true),
|
2016-07-22 05:06:30 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
router.post('collections.info', auth(), async (ctx) => {
|
2016-07-26 06:01:14 +00:00
|
|
|
const { id } = ctx.body;
|
2016-05-07 18:52:08 +00:00
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
2016-06-20 07:18:03 +00:00
|
|
|
const user = ctx.state.user;
|
2016-05-07 18:52:08 +00:00
|
|
|
const atlas = await Atlas.findOne({
|
|
|
|
where: {
|
2016-07-26 06:01:14 +00:00
|
|
|
id,
|
2016-06-20 07:18:03 +00:00
|
|
|
teamId: user.teamId,
|
2016-05-07 18:52:08 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!atlas) throw httpErrors.NotFound();
|
|
|
|
|
|
|
|
ctx.body = {
|
2016-08-15 10:51:26 +00:00
|
|
|
data: await presentCollection(ctx, atlas, true),
|
2016-05-07 18:52:08 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
router.post('collections.list', auth(), pagination(), async (ctx) => {
|
2016-06-20 07:18:03 +00:00
|
|
|
const user = ctx.state.user;
|
2016-08-05 15:09:14 +00:00
|
|
|
const collections = await Atlas.findAll({
|
2016-05-07 18:52:08 +00:00
|
|
|
where: {
|
2016-06-20 07:18:03 +00:00
|
|
|
teamId: user.teamId,
|
2016-05-07 18:52:08 +00:00
|
|
|
},
|
2016-05-20 07:33:52 +00:00
|
|
|
order: [
|
|
|
|
['updatedAt', 'DESC'],
|
|
|
|
],
|
2016-05-07 18:52:08 +00:00
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
2016-05-20 06:53:07 +00:00
|
|
|
// Atlases
|
|
|
|
let data = [];
|
2016-08-05 15:09:14 +00:00
|
|
|
await Promise.all(collections.map(async (atlas) => {
|
2016-08-15 10:51:26 +00:00
|
|
|
return data.push(await presentCollection(ctx, atlas, true));
|
2016-05-20 07:33:52 +00:00
|
|
|
}));
|
|
|
|
|
2016-08-22 06:40:57 +00:00
|
|
|
data = _.orderBy(data, ['updatedAt'], ['desc']);
|
2016-05-20 06:53:07 +00:00
|
|
|
|
2016-05-07 18:52:08 +00:00
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
2016-07-26 06:01:14 +00:00
|
|
|
data,
|
2016-05-07 18:52:08 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
router.post('collections.updateNavigationTree', auth(), async (ctx) => {
|
2016-07-26 06:01:14 +00:00
|
|
|
const { id, tree } = ctx.body;
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
2016-06-26 05:27:44 +00:00
|
|
|
|
2016-07-26 06:01:14 +00:00
|
|
|
const user = ctx.state.user;
|
|
|
|
const atlas = await Atlas.findOne({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
teamId: user.teamId,
|
|
|
|
},
|
|
|
|
});
|
2016-06-26 05:27:44 +00:00
|
|
|
|
2016-07-26 06:01:14 +00:00
|
|
|
if (!atlas) throw httpErrors.NotFound();
|
2016-06-26 05:27:44 +00:00
|
|
|
|
2016-07-26 06:01:14 +00:00
|
|
|
await atlas.updateNavigationTree(tree);
|
2016-06-26 05:27:44 +00:00
|
|
|
|
2016-07-26 06:01:14 +00:00
|
|
|
ctx.body = {
|
2016-08-15 10:51:26 +00:00
|
|
|
data: await presentCollection(ctx, atlas, true),
|
2016-07-26 06:01:14 +00:00
|
|
|
};
|
2016-06-26 05:27:44 +00:00
|
|
|
});
|
|
|
|
|
2016-06-22 07:01:57 +00:00
|
|
|
export default router;
|