1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/lib/services/rest/routes/folders.ts

41 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-11-05 18:58:23 +02:00
import { Request, RequestMethod } from '../Api';
2020-11-05 18:58:23 +02:00
import defaultAction from '../utils/defaultAction';
import paginatedResults from '../utils/paginatedResults';
import BaseModel from '../../../BaseModel';
import requestFields from '../utils/requestFields';
import Folder from '../../../models/Folder';
import { allForDisplay } from '../../../folders-screen-utils';
const { ErrorNotFound } = require('../utils/errors');
2020-11-05 18:58:23 +02:00
export default async function(request: Request, id: string = null, link: string = null) {
if (request.method === RequestMethod.GET && !id) {
2020-11-05 18:58:23 +02:00
if (request.query.as_tree) {
const folders = await allForDisplay({
fields: requestFields(request, BaseModel.TYPE_FOLDER),
includeDeleted: false,
});
2020-11-05 18:58:23 +02:00
const output = await Folder.allAsTree(folders);
return output;
} else {
return defaultAction(BaseModel.TYPE_FOLDER, request, id, link, null, { sql: 'deleted_time = 0' });
2020-11-05 18:58:23 +02:00
}
}
if (request.method === RequestMethod.GET && id) {
2020-11-05 18:58:23 +02:00
if (link && link === 'notes') {
const folder = await Folder.load(id);
return paginatedResults(BaseModel.TYPE_NOTE, request, { sql: 'parent_id = ? AND deleted_time = 0', params: [folder.id] });
2020-11-05 18:58:23 +02:00
} else if (link) {
throw new ErrorNotFound();
}
}
if (request.method === RequestMethod.DELETE) {
await Folder.delete(id, { toTrash: request.query.permanent !== '1', sourceDescription: 'api/folders DELETE' });
return;
}
2020-11-05 18:58:23 +02:00
return defaultAction(BaseModel.TYPE_FOLDER, request, id, link);
}