mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
server-ts: Convert routes/api/attributes
This commit is contained in:
parent
9330241045
commit
c0349b3f84
@ -137,7 +137,10 @@ export default class Becca {
|
|||||||
return branch;
|
return branch;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAttribute(attributeId: string): BAttribute | null {
|
getAttribute(attributeId: string | null): BAttribute | null {
|
||||||
|
if (!attributeId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return this.attributes[attributeId];
|
return this.attributes[attributeId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,19 +1,20 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const sql = require('../../services/sql');
|
import sql = require('../../services/sql');
|
||||||
const log = require('../../services/log');
|
import log = require('../../services/log');
|
||||||
const attributeService = require('../../services/attributes');
|
import attributeService = require('../../services/attributes');
|
||||||
const BAttribute = require('../../becca/entities/battribute');
|
import BAttribute = require('../../becca/entities/battribute');
|
||||||
const becca = require('../../becca/becca');
|
import becca = require('../../becca/becca');
|
||||||
const ValidationError = require('../../errors/validation_error');
|
import ValidationError = require('../../errors/validation_error');
|
||||||
|
import { Request } from 'express';
|
||||||
|
|
||||||
function getEffectiveNoteAttributes(req) {
|
function getEffectiveNoteAttributes(req: Request) {
|
||||||
const note = becca.getNote(req.params.noteId);
|
const note = becca.getNote(req.params.noteId);
|
||||||
|
|
||||||
return note.getAttributes();
|
return note?.getAttributes();
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateNoteAttribute(req) {
|
function updateNoteAttribute(req: Request) {
|
||||||
const noteId = req.params.noteId;
|
const noteId = req.params.noteId;
|
||||||
const body = req.body;
|
const body = req.body;
|
||||||
|
|
||||||
@ -70,14 +71,17 @@ function updateNoteAttribute(req) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function setNoteAttribute(req) {
|
function setNoteAttribute(req: Request) {
|
||||||
const noteId = req.params.noteId;
|
const noteId = req.params.noteId;
|
||||||
const body = req.body;
|
const body = req.body;
|
||||||
|
|
||||||
const attributeId = sql.getValue(`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = ? AND name = ?`, [noteId, body.type, body.name]);
|
const attributeId = sql.getValue<string | null>(`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = ? AND name = ?`, [noteId, body.type, body.name]);
|
||||||
|
|
||||||
if (attributeId) {
|
if (attributeId) {
|
||||||
const attr = becca.getAttribute(attributeId);
|
const attr = becca.getAttribute(attributeId);
|
||||||
|
if (!attr) {
|
||||||
|
throw new ValidationError(`Missing attribute with ID ${attributeId}.`);
|
||||||
|
}
|
||||||
attr.value = body.value;
|
attr.value = body.value;
|
||||||
attr.save();
|
attr.save();
|
||||||
} else {
|
} else {
|
||||||
@ -88,14 +92,14 @@ function setNoteAttribute(req) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addNoteAttribute(req) {
|
function addNoteAttribute(req: Request) {
|
||||||
const noteId = req.params.noteId;
|
const noteId = req.params.noteId;
|
||||||
const body = req.body;
|
const body = req.body;
|
||||||
|
|
||||||
new BAttribute({...body, noteId}).save();
|
new BAttribute({...body, noteId}).save();
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteNoteAttribute(req) {
|
function deleteNoteAttribute(req: Request) {
|
||||||
const noteId = req.params.noteId;
|
const noteId = req.params.noteId;
|
||||||
const attributeId = req.params.attributeId;
|
const attributeId = req.params.attributeId;
|
||||||
|
|
||||||
@ -110,11 +114,14 @@ function deleteNoteAttribute(req) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateNoteAttributes(req) {
|
function updateNoteAttributes(req: Request) {
|
||||||
const noteId = req.params.noteId;
|
const noteId = req.params.noteId;
|
||||||
const incomingAttributes = req.body;
|
const incomingAttributes = req.body;
|
||||||
|
|
||||||
const note = becca.getNote(noteId);
|
const note = becca.getNote(noteId);
|
||||||
|
if (!note) {
|
||||||
|
throw new ValidationError(`Cannot find note with ID ${noteId}.`);
|
||||||
|
}
|
||||||
|
|
||||||
let existingAttrs = note.getOwnedAttributes().slice();
|
let existingAttrs = note.getOwnedAttributes().slice();
|
||||||
|
|
||||||
@ -179,25 +186,29 @@ function updateNoteAttributes(req) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAttributeNames(req) {
|
function getAttributeNames(req: Request) {
|
||||||
const type = req.query.type;
|
const type = req.query.type;
|
||||||
const query = req.query.query;
|
const query = req.query.query;
|
||||||
|
|
||||||
|
if (typeof type !== "string" || typeof query !== "string") {
|
||||||
|
throw new ValidationError("Invalid data type.");
|
||||||
|
}
|
||||||
|
|
||||||
return attributeService.getAttributeNames(type, query);
|
return attributeService.getAttributeNames(type, query);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getValuesForAttribute(req) {
|
function getValuesForAttribute(req: Request) {
|
||||||
const attributeName = req.params.attributeName;
|
const attributeName = req.params.attributeName;
|
||||||
|
|
||||||
return sql.getColumn("SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND type = 'label' AND value != '' ORDER BY value", [attributeName]);
|
return sql.getColumn("SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND type = 'label' AND value != '' ORDER BY value", [attributeName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createRelation(req) {
|
function createRelation(req: Request) {
|
||||||
const sourceNoteId = req.params.noteId;
|
const sourceNoteId = req.params.noteId;
|
||||||
const targetNoteId = req.params.targetNoteId;
|
const targetNoteId = req.params.targetNoteId;
|
||||||
const name = req.params.name;
|
const name = req.params.name;
|
||||||
|
|
||||||
const attributeId = sql.getValue(`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]);
|
const attributeId = sql.getValue<string | null>(`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]);
|
||||||
let attribute = becca.getAttribute(attributeId);
|
let attribute = becca.getAttribute(attributeId);
|
||||||
|
|
||||||
if (!attribute) {
|
if (!attribute) {
|
||||||
@ -212,20 +223,22 @@ function createRelation(req) {
|
|||||||
return attribute;
|
return attribute;
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteRelation(req) {
|
function deleteRelation(req: Request) {
|
||||||
const sourceNoteId = req.params.noteId;
|
const sourceNoteId = req.params.noteId;
|
||||||
const targetNoteId = req.params.targetNoteId;
|
const targetNoteId = req.params.targetNoteId;
|
||||||
const name = req.params.name;
|
const name = req.params.name;
|
||||||
|
|
||||||
const attributeId = sql.getValue(`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]);
|
const attributeId = sql.getValue<string | null>(`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]);
|
||||||
|
|
||||||
if (attributeId) {
|
if (attributeId) {
|
||||||
const attribute = becca.getAttribute(attributeId);
|
const attribute = becca.getAttribute(attributeId);
|
||||||
attribute.markAsDeleted();
|
if (attribute) {
|
||||||
|
attribute.markAsDeleted();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export = {
|
||||||
updateNoteAttributes,
|
updateNoteAttributes,
|
||||||
updateNoteAttribute,
|
updateNoteAttribute,
|
||||||
setNoteAttribute,
|
setNoteAttribute,
|
Loading…
x
Reference in New Issue
Block a user