Skip to content

Commit

Permalink
Api: Resolves #1956: Allow getting the resources of a note
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Oct 7, 2019
1 parent 6460907 commit 60c1939
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ReactNativeClient/lib/BaseModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]);
}
Expand Down
16 changes: 16 additions & 0 deletions ReactNativeClient/lib/services/rest/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 60c1939

Please sign in to comment.