2018-01-29 18:34:59 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const Entity = require('./entity');
|
2018-03-31 10:51:37 -04:00
|
|
|
const repository = require('../services/repository');
|
2018-03-31 22:15:06 -04:00
|
|
|
const utils = require('../services/utils');
|
2018-04-01 12:45:35 -04:00
|
|
|
const sql = require('../services/sql');
|
2018-01-29 18:34:59 -05:00
|
|
|
|
2018-03-24 22:02:26 -04:00
|
|
|
class Label extends Entity {
|
|
|
|
|
static get tableName() { return "labels"; }
|
|
|
|
|
static get primaryKeyName() { return "labelId"; }
|
2018-01-29 23:35:36 -05:00
|
|
|
|
2018-01-29 18:34:59 -05:00
|
|
|
async getNote() {
|
2018-03-31 10:51:37 -04:00
|
|
|
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
|
2018-01-29 18:34:59 -05:00
|
|
|
}
|
2018-03-31 22:15:06 -04:00
|
|
|
|
2018-04-01 12:45:35 -04:00
|
|
|
async beforeSaving() {
|
2018-04-02 20:30:00 -04:00
|
|
|
super.beforeSaving();
|
2018-04-01 12:45:35 -04:00
|
|
|
|
2018-04-01 20:50:58 -04:00
|
|
|
if (!this.value) {
|
2018-04-01 12:45:35 -04:00
|
|
|
// null value isn't allowed
|
|
|
|
|
this.value = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.position === undefined) {
|
2018-04-01 20:50:58 -04:00
|
|
|
this.position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [this.noteId]);
|
2018-04-01 12:45:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.isDeleted) {
|
|
|
|
|
this.isDeleted = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
if (!this.dateCreated) {
|
|
|
|
|
this.dateCreated = utils.nowDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.dateModified = utils.nowDate();
|
|
|
|
|
}
|
2018-01-29 18:34:59 -05:00
|
|
|
}
|
|
|
|
|
|
2018-03-24 22:02:26 -04:00
|
|
|
module.exports = Label;
|