From 84ff95310be5b68453662ef9eb012fb96ecc179c Mon Sep 17 00:00:00 2001 From: James Herdman Date: Sun, 28 Oct 2018 10:43:01 -0400 Subject: [PATCH] Add Note About `filter` Key Casing It's a bit terse, but it's better than what's already there. Resolves vt-elixir#38. --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1de6beb..9393c6f 100644 --- a/README.md +++ b/README.md @@ -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: @@ -236,6 +236,10 @@ 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}]) @@ -243,6 +247,8 @@ defmodule MyApp.ArticleController do 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.