From 2ec2c63eea5e3a6cf6039fd6e1490e99073aebf0 Mon Sep 17 00:00:00 2001 From: RafaelOFreitas Date: Mon, 31 Jan 2022 15:58:32 -0300 Subject: [PATCH 1/2] Add Support filters and search in count_ functions. --- lib/context_functions_generator.ex | 9 +++++++- test/crudry_context_test.exs | 34 ++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/context_functions_generator.ex b/lib/context_functions_generator.ex index 783d53f..f5dd885 100644 --- a/lib/context_functions_generator.ex +++ b/lib/context_functions_generator.ex @@ -105,8 +105,15 @@ defmodule ContextFunctionsGenerator do def generate_function(:count, _name, pluralized_name, module, opts) do quote do - def unquote(:"count_#{pluralized_name}")(field \\ :id, repo_opts \\ []) do + def unquote(:"count_#{pluralized_name}")(opts \\ [], repo_opts \\ []) do + field = opts[:field] || :id + search = opts[:search] + search_fields = opts[:search_fields] || [] + filters = opts[:filters] || %{} + unquote(module) + |> Crudry.Query.search(search, search_fields) + |> Crudry.Query.filter(filters) |> unquote(get_repo_module(opts)).aggregate(:count, field, repo_opts) end end diff --git a/test/crudry_context_test.exs b/test/crudry_context_test.exs index cabbddc..1c6e15e 100644 --- a/test/crudry_context_test.exs +++ b/test/crudry_context_test.exs @@ -5,9 +5,10 @@ defmodule CrudryContextTest do alias Crudry.Repo alias Crudry.{Post, User} - @user %{username: "Chuck Norris"} - @user2 %{username: "Will Smith"} + @user %{username: "Chuck Norris", age: 81} + @user2 %{username: "Will Smith", age: 53} @user3 %{username: "Sylvester Stallone"} + @user4 %{username: "Will Ferrell", age: 54} @post %{title: "Chuck Norris threw a grenade and killed 50 people, then it exploded."} setup do @@ -25,8 +26,9 @@ defmodule CrudryContextTest do assert {:ok, %{} = user1} = Repo.insert(User.changeset(%User{}, @user)) assert {:ok, %{} = user2} = Repo.insert(User.changeset(%User{}, @user2)) assert {:ok, %{} = user3} = Repo.insert(User.changeset(%User{}, @user3)) + assert {:ok, %{} = user4} = Repo.insert(User.changeset(%User{}, @user4)) assert {:ok, %{} = post} = Repo.insert(Post.changeset(%Post{}, %{title: "title", user_id: user1.id})) - %{user1: user1, user2: user2, user3: user3, post: post} + %{user1: user1, user2: user2, user3: user3, user4: user4, post: post} end test "create/1" do @@ -39,12 +41,12 @@ defmodule CrudryContextTest do assert %Crudry.User{username: ^username} = UserContext.create_user!(@user) end - test "list/0", %{user1: user1, user2: user2, user3: user3} do - assert UserContext.list_users() == [user1, user2, user3] + test "list/0", %{user1: user1, user2: user2, user3: user3, user4: user4} do + assert UserContext.list_users() == [user1, user2, user3, user4] end - test "list_with_assocs/1", %{user1: user1, user2: user2, user3: user3} do - assert UserContext.list_users_with_assocs(:posts) == Repo.preload([user1, user2, user3], :posts) + test "list_with_assocs/1", %{user1: user1, user2: user2, user3: user3, user4: user4} do + assert UserContext.list_users_with_assocs(:posts) == Repo.preload([user1, user2, user3, user4], :posts) end test "list/1", %{user1: user1} do @@ -64,8 +66,22 @@ defmodule CrudryContextTest do assert UserContext.filter_users(%{username: @user3.username}) == [user3] end - test "count/1" do - assert UserContext.count_users(:id) == 3 + test "count/1", %{user2: user2} do + assert UserContext.count_users() == 4 + assert UserContext.count_users(field: :age) == 3 + + opts_search = [search: "Will", search_fields: [:username]] + assert UserContext.count_users(opts_search) == 2 + + opts_filters = [filters: %{id: user2.id}] + assert UserContext.count_users(opts_filters) == 1 + + opts_search_with_filters = [ + filters: %{age: user2.age}, + search: "Will", + search_fields: [:username], + ] + assert UserContext.count_users(opts_search_with_filters) == 1 end test "exists?/1", %{user1: user1} do From 5d79a13d24fae37fc54ae08bf4f2fb98d2237865 Mon Sep 17 00:00:00 2001 From: RafaelOFreitas Date: Mon, 31 Jan 2022 16:09:06 -0300 Subject: [PATCH 2/2] Update documentation of count_my_schemas function. --- lib/crudry_context.ex | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/crudry_context.ex b/lib/crudry_context.ex index bfabf8a..d2ce04f 100644 --- a/lib/crudry_context.ex +++ b/lib/crudry_context.ex @@ -138,8 +138,16 @@ defmodule Crudry.Context do |> Repo.all() end - def count_my_schemas(field \\\\ :id) do - Repo.aggregate(MySchema, :count, field) + def count_my_schemas(opts \\\\ []) do + field = opts[:field] || :id + search = opts[:search] + search_fields = opts[:search_fields] || [] + filters = opts[:filters] || %{} + + MySchema + |> Crudry.Query.search(search, search_fields) + |> Crudry.Query.filter(filters) + |> Repo.aggregate(:count, field) end ## Create functions