mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
server-ts: Convert routes/api/branches
This commit is contained in:
parent
706b9d0f46
commit
fd77c5e8c4
@ -129,7 +129,7 @@ export default class Becca {
|
|||||||
return this.branches[branchId];
|
return this.branches[branchId];
|
||||||
}
|
}
|
||||||
|
|
||||||
getBranchOrThrow(branchId: string): BBranch | null {
|
getBranchOrThrow(branchId: string): BBranch {
|
||||||
const branch = this.getBranch(branchId);
|
const branch = this.getBranch(branchId);
|
||||||
if (!branch) {
|
if (!branch) {
|
||||||
throw new NotFoundError(`Branch '${branchId}' was not found in becca.`);
|
throw new NotFoundError(`Branch '${branchId}' was not found in becca.`);
|
||||||
|
@ -203,9 +203,9 @@ class BNote extends AbstractBeccaEntity<BNote> {
|
|||||||
return this.children && this.children.length > 0;
|
return this.children && this.children.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
getChildBranches(): (BBranch | null)[] {
|
getChildBranches(): BBranch[] {
|
||||||
return this.children
|
return this.children
|
||||||
.map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId));
|
.map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId)) as BBranch[];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1,23 +1,24 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const sql = require('../../services/sql');
|
import sql = require('../../services/sql');
|
||||||
const utils = require('../../services/utils');
|
import utils = require('../../services/utils');
|
||||||
const entityChangesService = require('../../services/entity_changes');
|
import entityChangesService = require('../../services/entity_changes');
|
||||||
const treeService = require('../../services/tree');
|
import treeService = require('../../services/tree');
|
||||||
const eraseService = require('../../services/erase');
|
import eraseService = require('../../services/erase');
|
||||||
const becca = require('../../becca/becca');
|
import becca = require('../../becca/becca');
|
||||||
const TaskContext = require('../../services/task_context');
|
import TaskContext = require('../../services/task_context');
|
||||||
const branchService = require('../../services/branches');
|
import branchService = require('../../services/branches');
|
||||||
const log = require('../../services/log');
|
import log = require('../../services/log');
|
||||||
const ValidationError = require('../../errors/validation_error');
|
import ValidationError = require('../../errors/validation_error');
|
||||||
const eventService = require("../../services/events");
|
import eventService = require("../../services/events");
|
||||||
|
import { Request } from 'express';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Code in this file deals with moving and cloning branches. The relationship between note and parent note is unique
|
* Code in this file deals with moving and cloning branches. The relationship between note and parent note is unique
|
||||||
* for not deleted branches. There may be multiple deleted note-parent note relationships.
|
* for not deleted branches. There may be multiple deleted note-parent note relationships.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function moveBranchToParent(req) {
|
function moveBranchToParent(req: Request) {
|
||||||
const {branchId, parentBranchId} = req.params;
|
const {branchId, parentBranchId} = req.params;
|
||||||
|
|
||||||
const branchToMove = becca.getBranch(branchId);
|
const branchToMove = becca.getBranch(branchId);
|
||||||
@ -30,7 +31,7 @@ function moveBranchToParent(req) {
|
|||||||
return branchService.moveBranchToBranch(branchToMove, targetParentBranch, branchId);
|
return branchService.moveBranchToBranch(branchToMove, targetParentBranch, branchId);
|
||||||
}
|
}
|
||||||
|
|
||||||
function moveBranchBeforeNote(req) {
|
function moveBranchBeforeNote(req: Request) {
|
||||||
const {branchId, beforeBranchId} = req.params;
|
const {branchId, beforeBranchId} = req.params;
|
||||||
|
|
||||||
const branchToMove = becca.getBranchOrThrow(branchId);
|
const branchToMove = becca.getBranchOrThrow(branchId);
|
||||||
@ -51,7 +52,7 @@ function moveBranchBeforeNote(req) {
|
|||||||
[beforeBranch.parentNoteId, originalBeforeNotePosition]);
|
[beforeBranch.parentNoteId, originalBeforeNotePosition]);
|
||||||
|
|
||||||
// also need to update becca positions
|
// also need to update becca positions
|
||||||
const parentNote = becca.getNote(beforeBranch.parentNoteId);
|
const parentNote = becca.getNoteOrThrow(beforeBranch.parentNoteId);
|
||||||
|
|
||||||
for (const childBranch of parentNote.getChildBranches()) {
|
for (const childBranch of parentNote.getChildBranches()) {
|
||||||
if (childBranch.notePosition >= originalBeforeNotePosition) {
|
if (childBranch.notePosition >= originalBeforeNotePosition) {
|
||||||
@ -80,11 +81,11 @@ function moveBranchBeforeNote(req) {
|
|||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
function moveBranchAfterNote(req) {
|
function moveBranchAfterNote(req: Request) {
|
||||||
const {branchId, afterBranchId} = req.params;
|
const {branchId, afterBranchId} = req.params;
|
||||||
|
|
||||||
const branchToMove = becca.getBranch(branchId);
|
const branchToMove = becca.getBranchOrThrow(branchId);
|
||||||
const afterNote = becca.getBranch(afterBranchId);
|
const afterNote = becca.getBranchOrThrow(afterBranchId);
|
||||||
|
|
||||||
const validationResult = treeService.validateParentChild(afterNote.parentNoteId, branchToMove.noteId, branchId);
|
const validationResult = treeService.validateParentChild(afterNote.parentNoteId, branchToMove.noteId, branchId);
|
||||||
|
|
||||||
@ -100,7 +101,7 @@ function moveBranchAfterNote(req) {
|
|||||||
[afterNote.parentNoteId, originalAfterNotePosition]);
|
[afterNote.parentNoteId, originalAfterNotePosition]);
|
||||||
|
|
||||||
// also need to update becca positions
|
// also need to update becca positions
|
||||||
const parentNote = becca.getNote(afterNote.parentNoteId);
|
const parentNote = becca.getNoteOrThrow(afterNote.parentNoteId);
|
||||||
|
|
||||||
for (const childBranch of parentNote.getChildBranches()) {
|
for (const childBranch of parentNote.getChildBranches()) {
|
||||||
if (childBranch.notePosition > originalAfterNotePosition) {
|
if (childBranch.notePosition > originalAfterNotePosition) {
|
||||||
@ -131,7 +132,7 @@ function moveBranchAfterNote(req) {
|
|||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
function setExpanded(req) {
|
function setExpanded(req: Request) {
|
||||||
const {branchId} = req.params;
|
const {branchId} = req.params;
|
||||||
const expanded = parseInt(req.params.expanded);
|
const expanded = parseInt(req.params.expanded);
|
||||||
|
|
||||||
@ -153,11 +154,11 @@ function setExpanded(req) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setExpandedForSubtree(req) {
|
function setExpandedForSubtree(req: Request) {
|
||||||
const {branchId} = req.params;
|
const {branchId} = req.params;
|
||||||
const expanded = parseInt(req.params.expanded);
|
const expanded = parseInt(req.params.expanded);
|
||||||
|
|
||||||
let branchIds = sql.getColumn(`
|
let branchIds = sql.getColumn<string>(`
|
||||||
WITH RECURSIVE
|
WITH RECURSIVE
|
||||||
tree(branchId, noteId) AS (
|
tree(branchId, noteId) AS (
|
||||||
SELECT branchId, noteId FROM branches WHERE branchId = ?
|
SELECT branchId, noteId FROM branches WHERE branchId = ?
|
||||||
@ -186,12 +187,12 @@ function setExpandedForSubtree(req) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteBranch(req) {
|
function deleteBranch(req: Request) {
|
||||||
const last = req.query.last === 'true';
|
const last = req.query.last === 'true';
|
||||||
const eraseNotes = req.query.eraseNotes === 'true';
|
const eraseNotes = req.query.eraseNotes === 'true';
|
||||||
const branch = becca.getBranchOrThrow(req.params.branchId);
|
const branch = becca.getBranchOrThrow(req.params.branchId);
|
||||||
|
|
||||||
const taskContext = TaskContext.getInstance(req.query.taskId, 'deleteNotes');
|
const taskContext = TaskContext.getInstance(req.query.taskId as string, 'deleteNotes');
|
||||||
|
|
||||||
const deleteId = utils.randomString(10);
|
const deleteId = utils.randomString(10);
|
||||||
let noteDeleted;
|
let noteDeleted;
|
||||||
@ -214,16 +215,16 @@ function deleteBranch(req) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function setPrefix(req) {
|
function setPrefix(req: Request) {
|
||||||
const branchId = req.params.branchId;
|
const branchId = req.params.branchId;
|
||||||
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
|
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
|
||||||
|
|
||||||
const branch = becca.getBranch(branchId);
|
const branch = becca.getBranchOrThrow(branchId);
|
||||||
branch.prefix = prefix;
|
branch.prefix = prefix;
|
||||||
branch.save();
|
branch.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export = {
|
||||||
moveBranchToParent,
|
moveBranchToParent,
|
||||||
moveBranchBeforeNote,
|
moveBranchBeforeNote,
|
||||||
moveBranchAfterNote,
|
moveBranchAfterNote,
|
@ -27,7 +27,8 @@ function moveBranchToNote(branchToMove: BBranch, targetParentNoteId: string) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function moveBranchToBranch(branchToMove: BBranch, targetParentBranch: BBranch) {
|
function moveBranchToBranch(branchToMove: BBranch, targetParentBranch: BBranch, branchId: string) {
|
||||||
|
// TODO: Unused branch ID argument.
|
||||||
const res = moveBranchToNote(branchToMove, targetParentBranch.noteId);
|
const res = moveBranchToNote(branchToMove, targetParentBranch.noteId);
|
||||||
|
|
||||||
if (!("success" in res) || !res.success) {
|
if (!("success" in res) || !res.success) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user