2018-08-03 04:48:21 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const attributeService = require('../../services/attributes');
|
|
|
|
const repository = require('../../services/repository');
|
|
|
|
const Attribute = require('../../entities/attribute');
|
|
|
|
|
|
|
|
async function getNoteAttributes(req) {
|
|
|
|
const noteId = req.params.noteId;
|
|
|
|
|
|
|
|
return await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNoteAttributes(req) {
|
|
|
|
const noteId = req.params.noteId;
|
|
|
|
const attributes = req.body;
|
|
|
|
|
|
|
|
for (const attribute of attributes) {
|
|
|
|
let attributeEntity;
|
|
|
|
|
|
|
|
if (attribute.attributeId) {
|
|
|
|
attributeEntity = await repository.getAttribute(attribute.attributeId);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// if it was "created" and then immediatelly deleted, we just don't create it at all
|
|
|
|
if (attribute.isDeleted) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
attributeEntity = new Attribute();
|
|
|
|
attributeEntity.noteId = noteId;
|
|
|
|
}
|
|
|
|
|
2018-08-06 02:08:56 +08:00
|
|
|
attributeEntity.type = attribute.type;
|
2018-08-03 04:48:21 +08:00
|
|
|
attributeEntity.name = attribute.name;
|
|
|
|
attributeEntity.value = attribute.value;
|
|
|
|
attributeEntity.position = attribute.position;
|
2018-08-06 02:08:56 +08:00
|
|
|
attributeEntity.isInheritable = attribute.isInheritable;
|
2018-08-03 04:48:21 +08:00
|
|
|
attributeEntity.isDeleted = attribute.isDeleted;
|
|
|
|
|
|
|
|
await attributeEntity.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
|
|
|
|
}
|
|
|
|
|
2018-08-03 17:11:57 +08:00
|
|
|
async function getAttributeNames(req) {
|
|
|
|
const type = req.query.type;
|
|
|
|
const query = req.query.query;
|
2018-08-03 04:48:21 +08:00
|
|
|
|
2018-08-03 17:11:57 +08:00
|
|
|
return attributeService.getAttributeNames(type, query);
|
2018-08-03 04:48:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getValuesForAttribute(req) {
|
|
|
|
const attributeName = req.params.attributeName;
|
|
|
|
|
|
|
|
return await sql.getColumn("SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [attributeName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getNoteAttributes,
|
|
|
|
updateNoteAttributes,
|
2018-08-03 17:11:57 +08:00
|
|
|
getAttributeNames,
|
2018-08-03 04:48:21 +08:00
|
|
|
getValuesForAttribute
|
|
|
|
};
|