From 8c7d159012b032fde302272f06935fecc397c90b Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 3 Mar 2018 09:30:18 -0500 Subject: [PATCH] fix export/import for multi-valued attributes --- src/routes/api/export.js | 18 ++++++++++++------ src/routes/api/import.js | 6 +++++- src/services/attributes.js | 2 ++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/routes/api/export.js b/src/routes/api/export.js index 49bb74fc5..06a850f6b 100644 --- a/src/routes/api/export.js +++ b/src/routes/api/export.js @@ -3,21 +3,22 @@ const express = require('express'); const router = express.Router(); const sql = require('../../services/sql'); -const attributes = require('../../services/attributes'); const html = require('html'); const auth = require('../../services/auth'); const wrap = require('express-promise-wrap').wrap; const tar = require('tar-stream'); const sanitize = require("sanitize-filename"); +const Repository = require("../../services/repository"); router.get('/:noteId/', auth.checkApiAuthOrElectron, wrap(async (req, res, next) => { const noteId = req.params.noteId; + const repo = new Repository(req); const noteTreeId = await sql.getValue('SELECT noteTreeId FROM note_tree WHERE noteId = ?', [noteId]); const pack = tar.pack(); - const name = await exportNote(noteTreeId, '', pack); + const name = await exportNote(noteTreeId, '', pack, repo); pack.finalize(); @@ -27,9 +28,9 @@ router.get('/:noteId/', auth.checkApiAuthOrElectron, wrap(async (req, res, next) pack.pipe(res); })); -async function exportNote(noteTreeId, directory, pack) { +async function exportNote(noteTreeId, directory, pack, repo) { const noteTree = await sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); - const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteTree.noteId]); + const note = await repo.getEntity("SELECT notes.* FROM notes WHERE noteId = ?", [noteTree.noteId]); if (note.isProtected) { return; @@ -54,7 +55,7 @@ async function exportNote(noteTreeId, directory, pack) { if (children.length > 0) { for (const child of children) { - await exportNote(child.noteTreeId, childFileName + "/", pack); + await exportNote(child.noteTreeId, childFileName + "/", pack, repo); } } @@ -66,7 +67,12 @@ async function getMetadata(note) { title: note.title, type: note.type, mime: note.mime, - attributes: await attributes.getNoteAttributeMap(note.noteId) + attributes: (await note.getAttributes()).map(attr => { + return { + name: attr.name, + value: attr.value + }; + }) }; } diff --git a/src/routes/api/import.js b/src/routes/api/import.js index 6cac8e1e9..b64db03b0 100644 --- a/src/routes/api/import.js +++ b/src/routes/api/import.js @@ -4,6 +4,7 @@ const express = require('express'); const router = express.Router(); const sql = require('../../services/sql'); const auth = require('../../services/auth'); +const attributes = require('../../services/attributes'); const notes = require('../../services/notes'); const wrap = require('express-promise-wrap').wrap; const tar = require('tar-stream'); @@ -118,10 +119,13 @@ async function importNotes(files, parentNoteId, sourceId) { const noteId = await notes.createNote(parentNoteId, file.meta.title, file.data, { type: file.meta.type, mime: file.meta.mime, - attributes: file.meta.attributes, sourceId: sourceId }); + for (const attr of file.meta.attributes) { + await attributes.createAttribute(noteId, attr.name, attr.value); + } + if (file.children.length > 0) { await importNotes(file.children, noteId, sourceId); } diff --git a/src/services/attributes.js b/src/services/attributes.js index 1958bd26c..37eb1fa69 100644 --- a/src/services/attributes.js +++ b/src/services/attributes.js @@ -62,12 +62,14 @@ async function createAttribute(noteId, name, value = "", sourceId = null) { const now = utils.nowDate(); const attributeId = utils.newAttributeId(); + const position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM attributes WHERE noteId = ?`, [noteId]); await sql.insert("attributes", { attributeId: attributeId, noteId: noteId, name: name, value: value, + position: position, dateModified: now, dateCreated: now, isDeleted: false