More tree

This commit is contained in:
Jori Lallo
2016-06-20 23:12:56 -07:00
parent 24e02bfdc4
commit 2c07d43f46
7 changed files with 46 additions and 37 deletions

View File

@@ -122,7 +122,7 @@ module.exports = {
defaultValue: null,
special: [],
primaryKey: false },
rootDocumentForId:
parentDocumentForId:
{ type: 'UUID',
allowNull: true,
defaultValue: null,

View File

@@ -34,10 +34,24 @@ const Atlas = sequelize.define('atlas', {
// const slugifiedTitle = slug(this.title);
// return `${slugifiedTitle}-${this.urlId}`;
// }
async buildStructure() {
// TODO
const rootDocument = await Document.findOne({ where: {
parentDocumentForId: null,
atlasId: this.id,
}});
return {
name: rootDocument.title,
id: rootDocument.id,
url: rootDocument.getUrl(),
children: null,
}
}
}
});
Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' });
Atlas.hasOne(Document, { as: 'rootDocument', foreignKey: 'rootDocumentForId', constraints: false });
Atlas.hasOne(Document, { as: 'parentDocument', foreignKey: 'parentDocumentForId', constraints: false });
export default Atlas;

View File

@@ -45,7 +45,10 @@ const Document = sequelize.define('document', {
buildUrl() {
const slugifiedTitle = slug(this.title);
return `${slugifiedTitle}-${this.urlId}`;
}
},
getUrl() {
return `/documents/${ this.id }`;
},
}
});

View File

@@ -31,6 +31,11 @@ export function presentAtlas(atlas, includeRecentDocuments=false) {
type: atlas.type,
}
if (atlas.type === 'atlas') {
// Todo replace with `.atlasStructure`
data.structure = await atlas.buildStructure();
}
if (includeRecentDocuments) {
const documents = await Document.findAll({
where: {