2018-07-27 10:52:48 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const Entity = require('./entity');
|
|
|
|
|
const repository = require('../services/repository');
|
|
|
|
|
const dateUtils = require('../services/date_utils');
|
|
|
|
|
const sql = require('../services/sql');
|
|
|
|
|
|
|
|
|
|
class Relation extends Entity {
|
|
|
|
|
static get tableName() { return "relations"; }
|
|
|
|
|
static get primaryKeyName() { return "relationId"; }
|
2018-07-29 20:33:42 +02:00
|
|
|
static get hashedProperties() { return ["relationId", "sourceNoteId", "name", "targetNoteId", "isInheritable", "dateModified", "dateCreated"]; }
|
2018-07-27 10:52:48 +02:00
|
|
|
|
|
|
|
|
async getSourceNote() {
|
|
|
|
|
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.sourceNoteId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getTargetNote() {
|
|
|
|
|
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.targetNoteId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async beforeSaving() {
|
|
|
|
|
if (this.position === undefined) {
|
|
|
|
|
this.position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM relations WHERE sourceNoteId = ?`, [this.sourceNoteId]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-29 20:33:42 +02:00
|
|
|
if (!this.isInheritable) {
|
|
|
|
|
this.isInheritable = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 10:52:48 +02:00
|
|
|
if (!this.isDeleted) {
|
|
|
|
|
this.isDeleted = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.dateCreated) {
|
|
|
|
|
this.dateCreated = dateUtils.nowDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.dateModified = dateUtils.nowDate();
|
2018-08-06 08:59:26 +02:00
|
|
|
|
|
|
|
|
super.beforeSaving();
|
2018-07-27 10:52:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = Relation;
|