Present with related objects

This commit is contained in:
Jori Lallo
2016-05-19 23:53:07 -07:00
parent 99a59ceb94
commit 270f556570
3 changed files with 51 additions and 30 deletions

View File

@ -23,7 +23,7 @@ router.post('atlases.info', auth(), async (ctx) => {
if (!atlas) throw httpErrors.NotFound();
ctx.body = {
data: presentAtlas(atlas),
data: await presentAtlas(atlas, true),
};
});
@ -41,11 +41,15 @@ router.post('atlases.list', auth(), pagination(), async (ctx) => {
limit: ctx.state.pagination.limit,
});
// Atlases
let data = [];
await Promise.all(atlases.map(async (atlas) => {
data.push(await presentAtlas(atlas));
}))
ctx.body = {
pagination: ctx.state.pagination,
data: atlases.map((atlas) => {
return presentAtlas(atlas);
})
data: data,
};
});

View File

@ -11,12 +11,6 @@ const Atlas = sequelize.define('atlas', {
name: DataTypes.STRING,
description: DataTypes.STRING,
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes }},
}, {
instanceMethods: {
getRecentDocuments() {
return [];
},
},
});
Atlas.belongsTo(Team);

View File

@ -1,30 +1,53 @@
var marked = require('marked');
import Document from './models/Document';
export function presentUser(user) {
return {
return new Promise(async (resolve, reject) => {
resolve({
id: user.id,
name: user.name,
username: user.username,
email: user.email,
avatarUrl: user.slackData.profile.image_192,
};
});
});
}
export function presentTeam(team) {
return {
return new Promise(async (resolve, reject) => {
resolve({
id: team.id,
name: team.name,
};
});
});
}
export function presentAtlas(atlas, includeRecentDocuments=true) {
return {
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,
recentDocuments: atlas.getRecentDocuments(),
}
if (includeRecentDocuments) {
const documents = await Document.findAll({
where: {
atlasId: atlas.id,
},
limit: 10,
});
let recentDocuments = [];
await Promise.all(documents.map(async (document) => {
recentDocuments.push(await presentDocument(document));
}))
data.recentDocuments = recentDocuments;
}
resolve(data);
});
}
export async function presentDocument(document, includeAtlas=false) {
@ -41,7 +64,7 @@ export async function presentDocument(document, includeAtlas=false) {
if (includeAtlas) {
const atlas = await document.getAtlas();
data.atlas = presentAtlas(atlas, false);
data.atlas = await presentAtlas(atlas, false);
}
return data;