trilium/src/routes/api/attributes.js

48 lines
1.7 KiB
JavaScript
Raw Normal View History

"use strict";
const express = require('express');
const router = express.Router();
const sql = require('../../services/sql');
const auth = require('../../services/auth');
2018-01-12 10:40:09 +08:00
const sync_table = require('../../services/sync_table');
const utils = require('../../services/utils');
const wrap = require('express-promise-wrap').wrap;
router.get('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteId = req.params.noteId;
2018-01-29 08:30:14 +08:00
res.send(await sql.getAll("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId]));
}));
2018-01-12 10:40:09 +08:00
router.put('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteId = req.params.noteId;
const attributes = req.body;
const now = utils.nowDate();
await sql.doInTransaction(async () => {
for (const attr of attributes) {
2018-01-29 08:30:14 +08:00
if (attr.attributeId) {
await sql.execute("UPDATE attributes SET name = ?, value = ?, dateModified = ? WHERE attributeId = ?",
[attr.name, attr.value, now, attr.attributeId]);
2018-01-12 10:40:09 +08:00
}
else {
2018-01-29 08:30:14 +08:00
attr.attributeId = utils.newAttributeId();
2018-01-12 10:40:09 +08:00
await sql.insert("attributes", {
2018-01-29 08:30:14 +08:00
attributeId: attr.attributeId,
noteId: noteId,
2018-01-12 10:40:09 +08:00
name: attr.name,
value: attr.value,
2018-01-29 08:30:14 +08:00
dateCreated: now,
dateModified: now
2018-01-12 10:40:09 +08:00
});
}
2018-01-29 08:30:14 +08:00
await sync_table.addAttributeSync(attr.attributeId);
2018-01-12 10:40:09 +08:00
}
});
2018-01-29 08:30:14 +08:00
res.send(await sql.getAll("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId]));
2018-01-12 10:40:09 +08:00
}));
module.exports = router;