Skip to content

Commit

Permalink
Merge pull request #80 from jherdman/patch-1
Browse files Browse the repository at this point in the history
Add Note About `filter` Key Casing
  • Loading branch information
alanpeabody authored Oct 29, 2018
2 parents 66f058a + 84ff953 commit d433980
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ defaults to the results of your `records/1` callback).

For example, given the following request:

`GET /v1/articles?filter[category]=dogs&sort=-published`
`GET /v1/articles?filter[category]=dogs&filter[favourite-snack]=cheese&sort=-published`

You would implement the following callbacks:

Expand All @@ -236,13 +236,19 @@ defmodule MyApp.ArticleController do
def filter(_conn, query, "category", category) do
where(query, category: ^category)
end

def filter(_conn, query, "favourite_snack", snack) do
where(query, favourite_snack: ^favourite_snack)
en

def sort(_conn, query, "published", direction) do
order_by(query, [{^direction, :inserted_at}])
end
end
```

Note that in the case of `filter[favourite-snack]` JaResource has already helpfully converted the filter param's name from dasherized to underscore (or from [whatever you configured](https://github.com/vt-elixir/ja_serializer#key-format-for-attribute-relationship-and-query-param) your API to use).

### Paginate

The handle_index_query/2 can be used to apply query params and render_index/3 to serialize meta tag.
Expand Down

0 comments on commit d433980

Please sign in to comment.