Renaming atlases to collections

This commit is contained in:
Jori Lallo
2016-08-05 18:09:14 +03:00
parent cf1563eb2f
commit c753382571
14 changed files with 119 additions and 119 deletions

View File

@@ -4,12 +4,12 @@ import _orderBy from 'lodash.orderby';
import auth from './authentication';
import pagination from './middlewares/pagination';
import { presentAtlas } from '../presenters';
import { presentCollection } from '../presenters';
import { Atlas } from '../models';
const router = new Router();
router.post('atlases.create', auth(), async (ctx) => {
router.post('collections.create', auth(), async (ctx) => {
const {
name,
description,
@@ -28,11 +28,11 @@ router.post('atlases.create', auth(), async (ctx) => {
});
ctx.body = {
data: await presentAtlas(atlas, true),
data: await presentCollection(atlas, true),
};
});
router.post('atlases.info', auth(), async (ctx) => {
router.post('collections.info', auth(), async (ctx) => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
@@ -47,14 +47,14 @@ router.post('atlases.info', auth(), async (ctx) => {
if (!atlas) throw httpErrors.NotFound();
ctx.body = {
data: await presentAtlas(atlas, true),
data: await presentCollection(atlas, true),
};
});
router.post('atlases.list', auth(), pagination(), async (ctx) => {
router.post('collections.list', auth(), pagination(), async (ctx) => {
const user = ctx.state.user;
const atlases = await Atlas.findAll({
const collections = await Atlas.findAll({
where: {
teamId: user.teamId,
},
@@ -67,8 +67,8 @@ router.post('atlases.list', auth(), pagination(), async (ctx) => {
// Atlases
let data = [];
await Promise.all(atlases.map(async (atlas) => {
return data.push(await presentAtlas(atlas, true));
await Promise.all(collections.map(async (atlas) => {
return data.push(await presentCollection(atlas, true));
}));
data = _orderBy(data, ['updatedAt'], ['desc']);
@@ -79,7 +79,7 @@ router.post('atlases.list', auth(), pagination(), async (ctx) => {
};
});
router.post('atlases.updateNavigationTree', auth(), async (ctx) => {
router.post('collections.updateNavigationTree', auth(), async (ctx) => {
const { id, tree } = ctx.body;
ctx.assertPresent(id, 'id is required');
@@ -96,7 +96,7 @@ router.post('atlases.updateNavigationTree', auth(), async (ctx) => {
await atlas.updateNavigationTree(tree);
ctx.body = {
data: await presentAtlas(atlas, true),
data: await presentCollection(atlas, true),
};
});

View File

@@ -82,19 +82,19 @@ router.post('documents.search', auth(), async (ctx) => {
router.post('documents.create', auth(), async (ctx) => {
const {
atlas,
collection,
title,
text,
parentDocument,
} = ctx.body;
ctx.assertPresent(atlas, 'atlas is required');
ctx.assertPresent(collection, 'collection is required');
ctx.assertPresent(title, 'title is required');
ctx.assertPresent(text, 'text is required');
const user = ctx.state.user;
const ownerAtlas = await Atlas.findOne({
where: {
id: atlas,
id: collection,
teamId: user.teamId,
},
});
@@ -161,9 +161,9 @@ router.post('documents.update', auth(), async (ctx) => {
await document.createRevision();
// Update
const atlas = await Atlas.findById(document.atlasId);
if (atlas.type === 'atlas') {
await atlas.updateNavigationTree();
const collection = await Atlas.findById(document.atlasId);
if (collection.type === 'atlas') {
await collection.updateNavigationTree();
}
ctx.body = {
@@ -184,11 +184,11 @@ router.post('documents.delete', auth(), async (ctx) => {
teamId: user.teamId,
},
});
const atlas = await Atlas.findById(document.atlasId);
const collection = await Atlas.findById(document.atlasId);
if (!document) throw httpErrors.BadRequest();
if (atlas.type === 'atlas') {
if (collection.type === 'atlas') {
// Don't allow deletion of root docs
if (!document.parentDocumentId) {
throw httpErrors.BadRequest('Unable to delete atlas\'s root document');
@@ -196,8 +196,8 @@ router.post('documents.delete', auth(), async (ctx) => {
// Delete all chilren
try {
await atlas.deleteDocument(document);
await atlas.save();
await collection.deleteDocument(document);
await collection.save();
} catch (e) {
throw httpErrors.BadRequest('Error while deleting');
}

View File

@@ -2,7 +2,7 @@ import _orderBy from 'lodash.orderby';
import { Document, Atlas } from './models';
export function presentUser(user) {
return new Promise(async (resolve, reject) => {
return new Promise(async (resolve, _reject) => {
const data = {
id: user.id,
name: user.name,
@@ -14,7 +14,7 @@ export function presentUser(user) {
}
export function presentTeam(team) {
return new Promise(async (resolve, reject) => {
return new Promise(async (resolve, _reject) => {
resolve({
id: team.id,
name: team.name,
@@ -22,42 +22,7 @@ export function presentTeam(team) {
});
}
export function presentAtlas(atlas, includeRecentDocuments=false) {
return new Promise(async (resolve, reject) => {
const data = {
id: atlas.id,
name: atlas.name,
description: atlas.description,
type: atlas.type,
}
if (atlas.type === 'atlas') {
data.navigationTree = await atlas.getStructure();
}
if (includeRecentDocuments) {
const documents = await Document.findAll({
where: {
atlasId: atlas.id,
},
limit: 10,
order: [
['updatedAt', 'DESC'],
],
});
let recentDocuments = [];
await Promise.all(documents.map(async (document) => {
recentDocuments.push(await presentDocument(document, true));
}))
data.recentDocuments = _orderBy(recentDocuments, ['updatedAt'], ['desc']);
}
resolve(data);
});
}
export async function presentDocument(document, includeAtlas=false) {
export async function presentDocument(document, includeCollection = false) {
const data = {
id: document.id,
url: document.buildUrl(),
@@ -66,18 +31,17 @@ export async function presentDocument(document, includeAtlas=false) {
text: document.text,
html: document.html,
preview: document.preview,
private: document.private,
createdAt: document.createdAt,
updatedAt: document.updatedAt,
atlas: document.atlasId,
collection: document.atlasId,
team: document.teamId,
}
};
if (includeAtlas) {
const atlas = await Atlas.findOne({ where: {
if (includeCollection) {
const collection = await Atlas.findOne({ where: {
id: document.atlasId,
}});
data.atlas = await presentAtlas(atlas, false);
} });
data.collection = await presentCollection(collection, false);
}
const user = await document.getUser();
@@ -85,3 +49,38 @@ export async function presentDocument(document, includeAtlas=false) {
return data;
}
export function presentCollection(collection, includeRecentDocuments=false) {
return new Promise(async (resolve, _reject) => {
const data = {
id: collection.id,
name: collection.name,
description: collection.description,
type: collection.type,
};
if (collection.type === 'atlas') {
data.navigationTree = await collection.getStructure();
}
if (includeRecentDocuments) {
const documents = await Document.findAll({
where: {
atlasId: collection.id,
},
limit: 10,
order: [
['updatedAt', 'DESC'],
],
});
const recentDocuments = [];
await Promise.all(documents.map(async (document) => {
recentDocuments.push(await presentDocument(document, true));
}));
data.recentDocuments = _orderBy(recentDocuments, ['updatedAt'], ['desc']);
}
resolve(data);
});
}