Added revisions

This commit is contained in:
Jori Lallo
2016-06-26 11:23:03 -07:00
parent 2bcf37fd83
commit 0026b092d2
5 changed files with 149 additions and 11 deletions

View File

@ -11,6 +11,7 @@ import {
truncateMarkdown,
} from '../utils/truncate';
import User from './User';
import Revision from './Revision';
slug.defaults.mode ='rfc3986';
@ -19,6 +20,13 @@ const generateSlug = (title, urlId) => {
return `${slugifiedTitle}-${urlId}`;
};
const documentBeforeSave = (doc) => {
doc.html = convertToMarkdown(doc.text);
doc.preview = truncateMarkdown(doc.text, 160);
doc.revisionCount = doc.revisionCount + 1;
return doc;
};
const Document = sequelize.define('document', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
urlId: { type: DataTypes.STRING, primaryKey: true },
@ -27,21 +35,23 @@ const Document = sequelize.define('document', {
text: DataTypes.TEXT,
html: DataTypes.TEXT,
preview: DataTypes.TEXT,
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0, },
parentDocumentId: DataTypes.UUID,
lastModifiedById: {
type: 'UUID',
allowNull: false,
references: {
model: 'users',
}
},
}, {
hooks: {
beforeValidate: (doc) => {
doc.urlId = randomstring.generate(15);
},
beforeCreate: (doc) => {
doc.html = convertToMarkdown(doc.text);
doc.preview = truncateMarkdown(doc.text, 160);
},
beforeUpdate: (doc) => {
doc.html = convertToMarkdown(doc.text);
doc.preview = truncateMarkdown(doc.text, 160);
},
beforeCreate: documentBeforeSave,
beforeUpdate: documentBeforeSave,
},
instanceMethods: {
buildUrl() {
@ -51,6 +61,17 @@ const Document = sequelize.define('document', {
getUrl() {
return `/documents/${ this.id }`;
},
async createRevision() {
// Create revision of the current (latest)
await Revision.create({
title: this.title,
text: this.text,
html: this.html,
preview: this.preview,
userId: this.lastModifiedById,
documentId: this.id,
});
},
}
});