FriendsOfCake / crud

Production-grade rapid controller development with built in love for API and Search
https://crud.readthedocs.io/en/latest/
MIT License
375 stars 145 forks source link

RFC: JSON API Filtering (search) #524

Closed bravo-kernel closed 7 years ago

bravo-kernel commented 7 years ago

JSON API Filtering should be fully supported using the FriendsOfCake Search Plugin.

The filter query parameter is reserved for filtering data. Servers and clients SHOULD use this key for filtering operations.

Note: JSON API is agnostic about the strategies supported by a server. The filter query parameter can be used as the basis for any number of filtering strategies.

bravo-kernel commented 7 years ago

I would prefer if a FoC member would close this issue after confirming but it looks like this is indeed fully supported by using:

    public function searchConfiguration()
    {
        $search = new Manager($this);
        $search
            ->like('filter', [
                'before' => true,
                'after' => true,
                'field' => [$this->aliasField('name'), $this->aliasField('description')]
            ]);

        return $search;
    }

Connecting to http://cake3api.app/api/cocktails?filter=beer filters the results like shown below:

{
  "data": [
    {
      "type": "cocktails",
      "id": "8",
      "attributes": {
        "name": "Black Velvet",
        "description": "Beer based",
        "created": "2015-04-11T09:52:02+00:00",
        "modified": null
      },
      "links": {
        "self": "/api/cocktails/8"
      }
    }
  ]
}
bravo-kernel commented 7 years ago

Update: adding an Ember Data generated URL with filtering which seems to match information about regarding JSON API filtering out there on the internet. Unsure if this can be matched using the Search plugin though.

/user-profiles?filter[user_id]=1

Multiple filters seem to be possible as well:

GET https://example.com/api/v1/posts
    ?page[number]=2
    &page[size]=50
    &sort=author,-created
    &fields[posts]=id,title,created
    &include=comments,author
    &filter[posts]=1,2
    &filter[comments]=1,2
    &filter[status]=posted
dakota commented 7 years ago

It can if a middleware is written to convert the nested form into a non nested form ☺️

ADmad commented 7 years ago

This issue was moved to FriendsOfCake/crud-json-api#4