Edit collection (#173)
* Collection edit modal * Add icon * 💚 * Oh look, some specs * Delete collection * Remove from collection * Handle error responses Protect against deleting last collection * Fix key * 💚 * Keyboard navigate documents list * Add missing database constraints
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
// @flow
|
||||
import Router from 'koa-router';
|
||||
import httpErrors from 'http-errors';
|
||||
import _ from 'lodash';
|
||||
@@ -15,7 +16,7 @@ router.post('collections.create', auth(), async ctx => {
|
||||
|
||||
const user = ctx.state.user;
|
||||
|
||||
const atlas = await Collection.create({
|
||||
const collection = await Collection.create({
|
||||
name,
|
||||
description,
|
||||
type: type || 'atlas',
|
||||
@@ -24,7 +25,20 @@ router.post('collections.create', auth(), async ctx => {
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
data: await presentCollection(ctx, atlas),
|
||||
data: await presentCollection(ctx, collection),
|
||||
};
|
||||
});
|
||||
|
||||
router.post('collections.update', auth(), async ctx => {
|
||||
const { id, name } = ctx.body;
|
||||
ctx.assertPresent(name, 'name is required');
|
||||
|
||||
const collection = await Collection.findById(id);
|
||||
collection.name = name;
|
||||
await collection.save();
|
||||
|
||||
ctx.body = {
|
||||
data: await presentCollection(ctx, collection),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -33,17 +47,17 @@ router.post('collections.info', auth(), async ctx => {
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
|
||||
const user = ctx.state.user;
|
||||
const atlas = await Collection.scope('withRecentDocuments').findOne({
|
||||
const collection = await Collection.scope('withRecentDocuments').findOne({
|
||||
where: {
|
||||
id,
|
||||
teamId: user.teamId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!atlas) throw httpErrors.NotFound();
|
||||
if (!collection) throw httpErrors.NotFound();
|
||||
|
||||
ctx.body = {
|
||||
data: await presentCollection(ctx, atlas),
|
||||
data: await presentCollection(ctx, collection),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -59,7 +73,9 @@ router.post('collections.list', auth(), pagination(), async ctx => {
|
||||
});
|
||||
|
||||
const data = await Promise.all(
|
||||
collections.map(async atlas => await presentCollection(ctx, atlas))
|
||||
collections.map(
|
||||
async collection => await presentCollection(ctx, collection)
|
||||
)
|
||||
);
|
||||
|
||||
ctx.body = {
|
||||
@@ -68,4 +84,28 @@ router.post('collections.list', auth(), pagination(), async ctx => {
|
||||
};
|
||||
});
|
||||
|
||||
router.post('collections.delete', auth(), async ctx => {
|
||||
const { id } = ctx.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
|
||||
const user = ctx.state.user;
|
||||
const collection = await Collection.findById(id);
|
||||
const total = await Collection.count();
|
||||
|
||||
if (total === 1) throw httpErrors.BadRequest('Cannot delete last collection');
|
||||
|
||||
if (!collection || collection.teamId !== user.teamId)
|
||||
throw httpErrors.BadRequest();
|
||||
|
||||
try {
|
||||
await collection.destroy();
|
||||
} catch (e) {
|
||||
throw httpErrors.BadRequest('Error while deleting collection');
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
};
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user