mirror of
https://github.com/flarum/core.git
synced 2025-01-29 20:49:14 +01:00
Change the way we fetch multiple posts by ID
JSON-API specifies that multiple resources should be fetched with a comma-separated list of IDs, i.e. discussions/1,2,3,4. But this is problematic because if we do a findQuery with only one ID, then a single object will come back from the API where the serializer is expecting an array containing a single object. Instead, I’ve just implemented an ids “filter” on the discussions index API route (which is the default way that the adapter finds multiple IDs.)
This commit is contained in:
parent
5e288f55f5
commit
abc794c966
@ -1,37 +1,8 @@
|
||||
import JsonApiAdapter from 'ember-json-api/json-api-adapter';
|
||||
|
||||
export default JsonApiAdapter.extend({
|
||||
host: '/api',
|
||||
|
||||
findQuery: function(store, type, query) {
|
||||
var ids = null;
|
||||
if (query.ids) {
|
||||
ids = query.ids.join(',');
|
||||
delete query.ids;
|
||||
}
|
||||
return this.ajax(this.buildURL(type.typeKey, ids), 'GET', {data: query});
|
||||
},
|
||||
});
|
||||
|
||||
// export default DS.JsonApiAdapter.extend({
|
||||
// host: '/api',
|
||||
|
||||
// // xhr: [],
|
||||
|
||||
// // ajax: function(url, type, hash) {
|
||||
// // var adapter = this;
|
||||
|
||||
// // return new Ember.RSVP.Promise(function(resolve, reject) {
|
||||
// // hash = adapter.ajaxOptions(url, type, hash);
|
||||
|
||||
// // hash.success = function(json) {
|
||||
// // Ember.run(null, resolve, json);
|
||||
// // };
|
||||
|
||||
// // hash.error = function(jqXHR, textStatus, errorThrown) {
|
||||
// // Ember.run(null, reject, adapter.ajaxError(jqXHR));
|
||||
// // };
|
||||
|
||||
// // adapter.xhr.push(Ember.$.ajax(hash));
|
||||
// // }, "DS: RestAdapter#ajax " + type + " to " + url);
|
||||
// // },
|
||||
// });
|
||||
// Todo: make this loaded via an environment variable or something
|
||||
host: '/api'
|
||||
|
||||
});
|
@ -1,4 +1,5 @@
|
||||
import JsonApiSerializer from 'ember-json-api/json-api-serializer';
|
||||
|
||||
export default JsonApiSerializer.extend({
|
||||
normalize: function(type, hash, property) {
|
||||
var json = {};
|
||||
|
@ -8,13 +8,14 @@ use Flarum\Api\Serializers\PostSerializer;
|
||||
class Index extends Base
|
||||
{
|
||||
/**
|
||||
* Show posts from a discussion.
|
||||
* Show posts from a discussion, or by providing an array of IDs.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function run()
|
||||
{
|
||||
$discussionId = $this->input('discussions');
|
||||
$postIds = (array) $this->input('ids');
|
||||
|
||||
$count = $this->count(20, 50);
|
||||
|
||||
@ -40,9 +41,16 @@ class Index extends Base
|
||||
|
||||
// @todo move to post repository
|
||||
$posts = Post::with($relations)
|
||||
->whereCanView()
|
||||
->where('discussion_id', $discussionId)
|
||||
->skip($start)
|
||||
->whereCanView();
|
||||
|
||||
if ($discussionId) {
|
||||
$posts->where('discussion_id', $discussionId);
|
||||
}
|
||||
if (count($postIds)) {
|
||||
$posts->whereIn('id', $postIds);
|
||||
}
|
||||
|
||||
$posts = $posts->skip($start)
|
||||
->take($count)
|
||||
->orderBy($sort['by'], $sort['order'] ?: 'asc')
|
||||
->get();
|
||||
|
Loading…
x
Reference in New Issue
Block a user