changes in API format

This commit is contained in:
azivner 2018-04-01 20:33:10 -04:00
parent 311952d4dd
commit a066c6fe2b
18 changed files with 79 additions and 91 deletions

View File

@ -33,7 +33,7 @@ async function showDialog() {
async function savePrefix() {
const prefix = $treePrefixInput.val();
await server.put('tree/' + branchId + '/set-prefix', { prefix: prefix });
await server.put('branches/' + branchId + '/set-prefix', { prefix: prefix });
await treeService.setPrefix(branchId, prefix);

View File

@ -25,7 +25,7 @@ async function showNoteRevisionsDialog(noteId, noteRevisionId) {
$list.empty();
$content.empty();
revisionItems = await server.get('note-revisions/' + noteId);
revisionItems = await server.get('notes/' + noteId + '/revisions');
for (const item of revisionItems) {
const dateModified = utils.parseDate(item.dateModifiedFrom);

View File

@ -34,10 +34,7 @@ async function showDialog() {
}
async function saveOptions(optionName, optionValue) {
await server.post('options', {
name: optionName,
value: optionValue
});
await server.put('options/' + encodeURIComponent(optionName) + '/' + encodeURIComponent(optionValue));
infoService.showMessage("Options change have been saved.");
}

View File

@ -22,7 +22,7 @@ import ScriptApi from './script_api.js';
import ScriptContext from './script_context.js';
import sync from './sync.js';
import treeService from './tree.js';
import treeChanges from './tree_changes.js';
import treeChanges from './branches.js';
import treeUtils from './tree_utils.js';
import utils from './utils.js';
import server from './server.js';

View File

@ -6,7 +6,7 @@ import treeCache from "./tree_cache.js";
async function moveBeforeNode(nodesToMove, beforeNode) {
for (const nodeToMove of nodesToMove) {
const resp = await server.put('tree/' + nodeToMove.data.branchId + '/move-before/' + beforeNode.data.branchId);
const resp = await server.put('branches/' + nodeToMove.data.branchId + '/move-before/' + beforeNode.data.branchId);
if (!resp.success) {
alert(resp.message);
@ -21,7 +21,7 @@ async function moveAfterNode(nodesToMove, afterNode) {
nodesToMove.reverse(); // need to reverse to keep the note order
for (const nodeToMove of nodesToMove) {
const resp = await server.put('tree/' + nodeToMove.data.branchId + '/move-after/' + afterNode.data.branchId);
const resp = await server.put('branches/' + nodeToMove.data.branchId + '/move-after/' + afterNode.data.branchId);
if (!resp.success) {
alert(resp.message);
@ -34,7 +34,7 @@ async function moveAfterNode(nodesToMove, afterNode) {
async function moveToNode(nodesToMove, toNode) {
for (const nodeToMove of nodesToMove) {
const resp = await server.put('tree/' + nodeToMove.data.branchId + '/move-to/' + toNode.data.noteId);
const resp = await server.put('branches/' + nodeToMove.data.branchId + '/move-to/' + toNode.data.noteId);
if (!resp.success) {
alert(resp.message);
@ -64,7 +64,7 @@ async function deleteNodes(nodes) {
}
for (const node of nodes) {
await server.remove('tree/' + node.data.branchId);
await server.remove('branches/' + node.data.branchId);
}
// following code assumes that nodes contain only top-most selected nodes - getSelectedNodes has been
@ -96,7 +96,7 @@ async function moveNodeUpInHierarchy(node) {
return;
}
const resp = await server.put('tree/' + node.data.branchId + '/move-after/' + node.getParent().data.branchId);
const resp = await server.put('branches/' + node.data.branchId + '/move-after/' + node.getParent().data.branchId);
if (!resp.success) {
alert(resp.message);

View File

@ -3,9 +3,8 @@ import cloningService from './cloning.js';
import exportService from './export.js';
import messagingService from './messaging.js';
import protectedSessionService from './protected_session.js';
import treeChangesService from './tree_changes.js';
import treeChangesService from './branches.js';
import treeUtils from './tree_utils.js';
import utils from './utils.js';
import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js';
import infoService from "./info.js";
import treeCache from "./tree_cache.js";
@ -86,18 +85,18 @@ const contextMenuOptions = {
{title: "----"},
{title: "Edit tree prefix <kbd>F2</kbd>", cmd: "editTreePrefix", uiIcon: "ui-icon-pencil"},
{title: "----"},
{title: "Protect sub-tree", cmd: "protectSubTree", uiIcon: "ui-icon-locked"},
{title: "Unprotect sub-tree", cmd: "unprotectSubTree", uiIcon: "ui-icon-unlocked"},
{title: "Protect branch", cmd: "protectBranch", uiIcon: "ui-icon-locked"},
{title: "Unprotect branch", cmd: "unprotectBranch", uiIcon: "ui-icon-unlocked"},
{title: "----"},
{title: "Copy / clone <kbd>Ctrl+C</kbd>", cmd: "copy", uiIcon: "ui-icon-copy"},
{title: "Cut <kbd>Ctrl+X</kbd>", cmd: "cut", uiIcon: "ui-icon-scissors"},
{title: "Paste into <kbd>Ctrl+V</kbd>", cmd: "pasteInto", uiIcon: "ui-icon-clipboard"},
{title: "Paste after", cmd: "pasteAfter", uiIcon: "ui-icon-clipboard"},
{title: "----"},
{title: "Export sub-tree", cmd: "exportSubTree", uiIcon: " ui-icon-arrowthick-1-ne"},
{title: "Import sub-tree into", cmd: "importSubTree", uiIcon: "ui-icon-arrowthick-1-sw"},
{title: "Export branch", cmd: "exportBranch", uiIcon: " ui-icon-arrowthick-1-ne"},
{title: "Import into branch", cmd: "importBranch", uiIcon: "ui-icon-arrowthick-1-sw"},
{title: "----"},
{title: "Collapse sub-tree <kbd>Alt+-</kbd>", cmd: "collapseSubTree", uiIcon: "ui-icon-minus"},
{title: "Collapse branch <kbd>Alt+-</kbd>", cmd: "collapseBranch", uiIcon: "ui-icon-minus"},
{title: "Force note sync", cmd: "forceNoteSync", uiIcon: "ui-icon-refresh"},
{title: "Sort alphabetically <kbd>Alt+S</kbd>", cmd: "sortAlphabetically", uiIcon: " ui-icon-arrowthick-2-n-s"}
@ -113,8 +112,8 @@ const contextMenuOptions = {
$tree.contextmenu("enableEntry", "pasteInto", clipboardIds.length > 0 && note.type !== 'search');
$tree.contextmenu("enableEntry", "insertNoteHere", !parentNote || parentNote.type !== 'search');
$tree.contextmenu("enableEntry", "insertChildNote", note.type !== 'search');
$tree.contextmenu("enableEntry", "importSubTree", note.type !== 'search');
$tree.contextmenu("enableEntry", "exportSubTree", note.type !== 'search');
$tree.contextmenu("enableEntry", "importBranch", note.type !== 'search');
$tree.contextmenu("enableEntry", "exportBranch", note.type !== 'search');
// Activate node on right-click
node.setActive();
@ -138,11 +137,11 @@ const contextMenuOptions = {
else if (ui.cmd === "editTreePrefix") {
editTreePrefixDialog.showDialog(node);
}
else if (ui.cmd === "protectSubTree") {
protectedSessionService.protectSubTree(node.data.noteId, true);
else if (ui.cmd === "protectBranch") {
protectedSessionService.protectBranch(node.data.noteId, true);
}
else if (ui.cmd === "unprotectSubTree") {
protectedSessionService.protectSubTree(node.data.noteId, false);
else if (ui.cmd === "unprotectBranch") {
protectedSessionService.protectBranch(node.data.noteId, false);
}
else if (ui.cmd === "copy") {
copy(treeService.getSelectedNodes());
@ -159,13 +158,13 @@ const contextMenuOptions = {
else if (ui.cmd === "delete") {
treeChangesService.deleteNodes(treeService.getSelectedNodes(true));
}
else if (ui.cmd === "exportSubTree") {
exportService.exportSubTree(node.data.noteId);
else if (ui.cmd === "exportBranch") {
exportService.exportBranch(node.data.noteId);
}
else if (ui.cmd === "importSubTree") {
exportService.importSubTree(node.data.noteId);
else if (ui.cmd === "importBranch") {
exportService.importBranch(node.data.noteId);
}
else if (ui.cmd === "collapseSubTree") {
else if (ui.cmd === "collapseBranch") {
treeService.collapseTree(node);
}
else if (ui.cmd === "forceNoteSync") {

View File

@ -1,5 +1,5 @@
import treeService from './tree.js';
import treeChangesService from './tree_changes.js';
import treeChangesService from './branches.js';
const dragAndDropSetup = {
autoExpandMS: 600,

View File

@ -3,8 +3,8 @@ import protectedSessionHolder from './protected_session_holder.js';
import utils from './utils.js';
import server from './server.js';
function exportSubTree(noteId) {
const url = utils.getHost() + "/api/export/" + noteId + "?protectedSessionId="
function exportBranch(noteId) {
const url = utils.getHost() + "/api/notes/" + noteId + "/export?protectedSessionId="
+ encodeURIComponent(protectedSessionHolder.getProtectedSessionId());
utils.download(url);
@ -12,7 +12,7 @@ function exportSubTree(noteId) {
let importNoteId;
function importSubTree(noteId) {
function importBranch(noteId) {
importNoteId = noteId;
$("#import-upload").trigger('click');
@ -23,7 +23,7 @@ $("#import-upload").change(async function() {
formData.append('upload', this.files[0]);
await $.ajax({
url: baseApiUrl + 'import/' + importNoteId,
url: baseApiUrl + 'notes/' + importNoteId + '/import',
headers: server.getHeaders(),
data: formData,
type: 'POST',
@ -35,6 +35,6 @@ $("#import-upload").change(async function() {
});
export default {
exportSubTree,
importSubTree
exportBranch,
importBranch
};

View File

@ -119,10 +119,10 @@ async function unprotectNoteAndSendToServer() {
noteDetail.setNoteBackgroundIfProtected(note);
}
async function protectSubTree(noteId, protect) {
async function protectBranch(noteId, protect) {
await ensureProtectedSession(true, true);
await server.put('notes/' + noteId + "/protect-sub-tree/" + (protect ? 1 : 0));
await server.put('notes/' + noteId + "/protect/" + (protect ? 1 : 0));
infoService.showMessage("Request to un/protect sub tree has finished successfully");
@ -143,6 +143,6 @@ export default {
ensureProtectedSession,
protectNoteAndSendToServer,
unprotectNoteAndSendToServer,
protectSubTree,
protectBranch,
ensureDialogIsClosed
};

View File

@ -6,7 +6,7 @@ let protectedSessionTimeout = null;
let protectedSessionId = null;
$(document).ready(() => {
server.get('options/all').then(options => protectedSessionTimeout = options.protected_session_timeout);
server.get('options').then(options => protectedSessionTimeout = options.protected_session_timeout);
});
setInterval(() => {

View File

@ -4,7 +4,7 @@ import linkService from './link.js';
import messagingService from './messaging.js';
import noteDetailService from './note_detail.js';
import protectedSessionHolder from './protected_session_holder.js';
import treeChangesService from './tree_changes.js';
import treeChangesService from './branches.js';
import treeUtils from './tree_utils.js';
import utils from './utils.js';
import server from './server.js';
@ -238,7 +238,7 @@ async function setExpandedToServer(branchId, isExpanded) {
const expandedNum = isExpanded ? 1 : 0;
await server.put('tree/' + branchId + '/expanded/' + expandedNum);
await server.put('branches/' + branchId + '/expanded/' + expandedNum);
}
function setCurrentNotePathToHash(node) {

View File

@ -1,6 +1,6 @@
import noteDetailService from "./note_detail.js";
import utils from "./utils.js";
import treeChangesService from "./tree_changes.js";
import treeChangesService from "./branches.js";
import contextMenuService from "./context_menu.js";
import treeService from "./tree.js";
import editTreePrefixDialog from "../dialogs/edit_tree_prefix.js";

View File

@ -105,10 +105,20 @@ async function deleteBranch(req) {
await notes.deleteNote(branch);
}
async function setPrefix(req) {
const branchId = req.params.branchId;
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
const branch = await repository.getBranch(branchId);
branch.prefix = prefix;
await branch.save();
}
module.exports = {
moveBranchToParent,
moveBranchBeforeNote,
moveBranchAfterNote,
setExpanded,
deleteBranch
deleteBranch,
setPrefix
};

View File

@ -1,17 +1,16 @@
"use strict";
const sql = require('../../services/sql');
const utils = require('../../services/utils');
const sync_table = require('../../services/sync_table');
const tree = require('../../services/tree');
const Branch = require('../../entities/branch');
async function cloneNoteToParent(req) {
const noteId = req.params.noteId;
const parentNoteId = req.params.parentNoteId;
const childNoteId = req.params.childNoteId;
const prefix = req.body.prefix;
const validationResult = await tree.validateParentChild(parentNoteId, childNoteId);
const validationResult = await tree.validateParentChild(parentNoteId, noteId);
if (!validationResult.success) {
return validationResult;
@ -21,7 +20,7 @@ async function cloneNoteToParent(req) {
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
const branch = new Branch({
noteId: childNoteId,
noteId: noteId,
parentNoteId: parentNoteId,
prefix: prefix,
notePosition: newNotePos,

View File

@ -86,18 +86,18 @@ async function parseImportFile(file) {
}
async function importTar(req) {
const parentNoteId = req.params.parentNoteId;
const noteId = req.params.noteId;
const file = req.file;
const parentNote = await repository.getNote(parentNoteId);
const parentNote = await repository.getNote(noteId);
if (!parentNote) {
return [404, `Note ${parentNoteId} doesn't exist.`];
return [404, `Note ${noteId} doesn't exist.`];
}
const files = await parseImportFile(file);
await importNotes(files, parentNoteId);
await importNotes(files, noteId);
}
async function importNotes(files, parentNoteId) {

View File

@ -6,11 +6,7 @@ const options = require('../../services/options');
// options allowed to be updated directly in options dialog
const ALLOWED_OPTIONS = ['protected_session_timeout', 'note_revision_snapshot_time_interval'];
async function getAllOptions() {
return await sql.getMap("SELECT name, value FROM options");
}
async function getAllowedOptions() {
async function getOptions() {
const options = await sql.getMap("SELECT name, value FROM options WHERE name IN ("
+ ALLOWED_OPTIONS.map(x => '?').join(",") + ")", ALLOWED_OPTIONS);
@ -18,17 +14,16 @@ async function getAllowedOptions() {
}
async function updateOption(req) {
const body = req.body;
const {name, value} = req.params;
if (!ALLOWED_OPTIONS.includes(body['name'])) {
if (!ALLOWED_OPTIONS.includes(name)) {
return [400, "not allowed option to set"];
}
await options.setOption(body['name'], body['value']);
await options.setOption(name, value);
}
module.exports = {
getAllowedOptions,
getAllOptions,
getOptions,
updateOption
};

View File

@ -60,16 +60,6 @@ async function getTree() {
};
}
async function setPrefix(req) {
const branchId = req.params.branchId;
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
const branch = await repository.getBranch(branchId);
branch.prefix = prefix;
await branch.save();
}
module.exports = {
getTree,
setPrefix
getTree
};

View File

@ -7,7 +7,7 @@ const multer = require('multer')();
// API routes
const treeApiRoute = require('./api/tree');
const notesApiRoute = require('./api/notes');
const treeChangesApiRoute = require('./api/tree_changes');
const branchesApiRoute = require('./api/branches');
const cloningApiRoute = require('./api/cloning');
const noteRevisionsApiRoute = require('./api/note_revisions');
const recentChangesApiRoute = require('./api/recent_changes');
@ -99,36 +99,37 @@ function register(app) {
route(GET, '/setup', [auth.checkAppNotInitialized], setupRoute.setupPage);
apiRoute(GET, '/api/tree', treeApiRoute.getTree);
apiRoute(PUT, '/api/tree/:branchId/set-prefix', treeApiRoute.setPrefix);
apiRoute(PUT, '/api/branches/:branchId/set-prefix', branchesApiRoute.setPrefix);
apiRoute(PUT, '/api/tree/:branchId/move-to/:parentNoteId', treeChangesApiRoute.moveBranchToParent);
apiRoute(PUT, '/api/tree/:branchId/move-before/:beforeBranchId', treeChangesApiRoute.moveBranchBeforeNote);
apiRoute(PUT, '/api/tree/:branchId/move-after/:afterBranchId', treeChangesApiRoute.moveBranchAfterNote);
apiRoute(PUT, '/api/tree/:branchId/expanded/:expanded', treeChangesApiRoute.setExpanded);
apiRoute(DELETE, '/api/tree/:branchId', treeChangesApiRoute.deleteBranch);
apiRoute(PUT, '/api/branches/:branchId/move-to/:parentNoteId', branchesApiRoute.moveBranchToParent);
apiRoute(PUT, '/api/branches/:branchId/move-before/:beforeBranchId', branchesApiRoute.moveBranchBeforeNote);
apiRoute(PUT, '/api/branches/:branchId/move-after/:afterBranchId', branchesApiRoute.moveBranchAfterNote);
apiRoute(PUT, '/api/branches/:branchId/expanded/:expanded', branchesApiRoute.setExpanded);
apiRoute(DELETE, '/api/branches/:branchId', branchesApiRoute.deleteBranch);
apiRoute(GET, '/api/notes/:noteId', notesApiRoute.getNote);
apiRoute(PUT, '/api/notes/:noteId', notesApiRoute.updateNote);
apiRoute(POST, '/api/notes/:parentNoteId/children', notesApiRoute.createNote);
apiRoute(PUT, '/api/notes/:noteId/sort', notesApiRoute.sortNotes);
apiRoute(PUT, '/api/notes/:noteId/protect-sub-tree/:isProtected', notesApiRoute.protectBranch);
apiRoute(PUT, '/api/notes/:noteId/protect/:isProtected', notesApiRoute.protectBranch);
apiRoute(PUT, /\/api\/notes\/(.*)\/type\/(.*)\/mime\/(.*)/, notesApiRoute.setNoteTypeMime);
apiRoute(GET, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.getNoteRevisions);
apiRoute(PUT, '/api/notes/:childNoteId/clone-to/:parentNoteId', cloningApiRoute.cloneNoteToParent);
apiRoute(PUT, '/api/notes/:noteId/clone-to/:parentNoteId', cloningApiRoute.cloneNoteToParent);
apiRoute(PUT, '/api/notes/:noteId/clone-after/:afterBranchId', cloningApiRoute.cloneNoteAfter);
route(GET, '/api/notes/:noteId/export', [auth.checkApiAuthOrElectron], exportRoute.exportNote);
route(POST, '/api/notes/:noteId/import', [auth.checkApiAuthOrElectron, uploadMiddleware], importRoute.importTar, apiResultHandler);
apiRoute(GET, '/api/notes/:noteId/labels', labelsRoute.getNoteLabels);
apiRoute(PUT, '/api/notes/:noteId/labels', labelsRoute.updateNoteLabels);
apiRoute(GET, '/api/labels/names', labelsRoute.getAllLabelNames);
apiRoute(GET, '/api/labels/values/:labelName', labelsRoute.getValuesForLabel);
apiRoute(GET, '/api/note-revisions/:noteId', noteRevisionsApiRoute.getNoteRevisions);
apiRoute(GET, '/api/recent-changes', recentChangesApiRoute.getRecentChanges);
apiRoute(GET, '/api/options', optionsApiRoute.getAllowedOptions);
apiRoute(GET, '/api/options/all', optionsApiRoute.getAllOptions);
apiRoute(POST, '/api/options', optionsApiRoute.updateOption);
apiRoute(GET, '/api/options', optionsApiRoute.getOptions);
apiRoute(PUT, '/api/options/:name/:value', optionsApiRoute.updateOption);
apiRoute(POST, '/api/password/change', passwordApiRoute.changePassword);
@ -164,9 +165,6 @@ function register(app) {
apiRoute(PUT, '/api/recent-notes/:branchId/:notePath', recentNotesRoute.addRecentNote);
apiRoute(GET, '/api/app-info', appInfoRoute.getAppInfo);
route(GET, '/api/export/:noteId', [auth.checkApiAuthOrElectron], exportRoute.exportNote);
route(POST, '/api/import/:parentNoteId', [auth.checkApiAuthOrElectron], importRoute.importTar, apiResultHandler);
route(POST, '/api/setup', [auth.checkAppNotInitialized], setupApiRoute.setup, apiResultHandler);
apiRoute(POST, '/api/sql/execute', sqlRoute.execute);