This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/api/collections.js

78 lines
1.7 KiB
JavaScript
Raw Normal View History

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';
2017-05-27 18:08:52 +00:00
import { Collection } from '../models';
2016-05-07 18:52:08 +00:00
const router = new Router();
2017-04-27 04:47:03 +00:00
router.post('collections.create', auth(), async ctx => {
const { name, description, type } = ctx.body;
2016-07-22 05:06:30 +00:00
ctx.assertPresent(name, 'name is required');
const user = ctx.state.user;
2017-05-27 18:08:52 +00:00
const atlas = await Collection.create({
2016-07-22 05:06:30 +00:00
name,
description,
type: type || 'atlas',
teamId: user.teamId,
creatorId: user.id,
2016-07-22 05:06:30 +00:00
});
ctx.body = {
data: await presentCollection(ctx, atlas, true),
2016-07-22 05:06:30 +00:00
};
});
2017-04-27 04:47:03 +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;
2017-05-27 18:08:52 +00:00
const atlas = await Collection.findOne({
2016-05-07 18:52:08 +00:00
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 = {
data: await presentCollection(ctx, atlas, true),
2016-05-07 18:52:08 +00:00
};
});
2017-04-27 04:47:03 +00:00
router.post('collections.list', auth(), pagination(), async ctx => {
2016-06-20 07:18:03 +00:00
const user = ctx.state.user;
2017-05-27 18:08:52 +00:00
const collections = await Collection.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
},
2017-04-27 04:47:03 +00:00
order: [['updatedAt', 'DESC']],
2016-05-07 18:52:08 +00:00
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
2017-05-27 18:08:52 +00:00
// Collectiones
2016-05-20 06:53:07 +00:00
let data = [];
2017-04-27 04:47:03 +00:00
await Promise.all(
collections.map(async atlas => {
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-06-22 07:01:57 +00:00
export default router;