1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Api: Resolves #1956: Allow getting the resources of a note

This commit is contained in:
Laurent Cozic 2019-10-07 09:57:24 +02:00
parent 6460907976
commit 60c1939d26
2 changed files with 20 additions and 3 deletions

View File

@ -150,8 +150,8 @@ class BaseModel {
});
}
static load(id) {
return this.loadByField('id', id);
static load(id, options = null) {
return this.loadByField('id', id, options);
}
static shortId(id) {
@ -250,7 +250,8 @@ class BaseModel {
static loadByField(fieldName, fieldValue, options = null) {
if (!options) options = {};
if (!('caseInsensitive' in options)) options.caseInsensitive = false;
let sql = `SELECT * FROM \`${this.tableName()}\` WHERE \`${fieldName}\` = ?`;
if (!options.fields) options.fields = '*';
let sql = `SELECT ${this.db().escapeFields(options.fields)} FROM \`${this.tableName()}\` WHERE \`${fieldName}\` = ?`;
if (options.caseInsensitive) sql += ' COLLATE NOCASE';
return this.modelSelectOne(sql, [fieldValue]);
}

View File

@ -354,12 +354,28 @@ class Api {
return options;
}
defaultLoadOptions_(request) {
const options = {};
const fields = this.fields_(request, []);
if (fields.length) options.fields = fields;
return options;
}
async action_notes(request, id = null, link = null) {
this.checkToken_(request);
if (request.method === 'GET') {
if (link && link === 'tags') {
return Tag.tagsByNoteId(id);
} else if (link && link === 'resources') {
const note = await Note.load(id);
const resourceIds = await Note.linkedResourceIds(note.body);
const output = [];
const loadOptions = this.defaultLoadOptions_(request);
for (let resourceId of resourceIds) {
output.push(await Resource.load(resourceId, loadOptions));
}
return output;
} else if (link) {
throw new ErrorNotFound();
}