1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-02-13 19:42:36 +02:00

API: Fix note and resource association end points

This commit is contained in:
Laurent Cozic 2020-11-13 21:45:25 +00:00
parent 91e7c66f9e
commit 8305eb4403
4 changed files with 24 additions and 4 deletions

View File

@ -9,7 +9,8 @@ module.exports = {
],
testPathIgnorePatterns: [
'/node_modules/',
'tests/support/',
'/tests\\/support/',
'/build/',
'test-utils.js',
'file_api_driver.js',
],

View File

@ -696,7 +696,18 @@ describe('services_rest_Api', function() {
const r = await api.route(RequestMethod.GET, `resources/${resource.id}/notes`);
expect(r.items.length).toBe(1);
expect(r.items[0]).toBe(note.id);
expect(r.items[0].id).toBe(note.id);
}));
it('should return the resources associated with a note', asyncTest(async () => {
const note = await Note.save({});
await shim.attachFileToNote(note, `${__dirname}/../tests/support/photo.jpg`);
const resource = (await Resource.all())[0];
const r = await api.route(RequestMethod.GET, `notes/${note.id}/resources`);
expect(r.items.length).toBe(1);
expect(r.items[0].id).toBe(resource.id);
}));
it('should return search results', asyncTest(async () => {

View File

@ -306,7 +306,7 @@ export default async function(request: Request, id: string = null, link: string
if (!note) throw new ErrorNotFound();
const resourceIds = await Note.linkedResourceIds(note.body);
const output = [];
const loadOptions = defaultLoadOptions(request, BaseModel.TYPE_NOTE);
const loadOptions = defaultLoadOptions(request, BaseModel.TYPE_RESOURCE);
for (const resourceId of resourceIds) {
output.push(await Resource.load(resourceId, loadOptions));
}

View File

@ -7,7 +7,9 @@ import readonlyProperties from '../utils/readonlyProperties';
import ApiResponse from '../ApiResponse';
import NoteResource from '../../../models/NoteResource';
import collectionToPaginatedResults from '../utils/collectionToPaginatedResults';
import defaultLoadOptions from '../utils/defaultLoadOptions';
const Resource = require('../../../models/Resource');
const Note = require('../../../models/Note');
export default async function(request: Request, id: string = null, link: string = null) {
// fieldName: "data"
@ -33,7 +35,13 @@ export default async function(request: Request, id: string = null, link: string
}
if (link === 'notes') {
return collectionToPaginatedResults(await NoteResource.associatedNoteIds(id), request);
const noteIds = await NoteResource.associatedNoteIds(id);
const loadOptions = defaultLoadOptions(request, BaseModel.TYPE_NOTE);
const notes = [];
for (const noteId of noteIds) {
notes.push(await Note.load(noteId, loadOptions));
}
return collectionToPaginatedResults(notes, request);
}
if (link) throw new ErrorNotFound();