commit 0839e3b9e4e0022cdf2d4e31f64bf26a2758b11e
parent bbc38f353e14b3bbc7d7030cabefac094229a676
Author: srfsh <dev@srf.sh>
Date: Fri, 23 Sep 2022 16:57:43 +0300
Zenflows.VF.Organization.{Domain,Filter}: add filtering
Diffstat:
3 files changed, 66 insertions(+), 6 deletions(-)
diff --git a/src/zenflows/vf/organization/domain.ex b/src/zenflows/vf/organization/domain.ex
@@ -18,11 +18,9 @@
defmodule Zenflows.VF.Organization.Domain do
@moduledoc "Domain logic of Organizations."
-import Ecto.Query
-
alias Ecto.Multi
alias Zenflows.DB.{Paging, Repo}
-alias Zenflows.VF.Organization
+alias Zenflows.VF.{Organization, Organization.Filter}
@typep repo() :: Ecto.Repo.t()
@typep chgset() :: Ecto.Changeset.t()
@@ -43,9 +41,11 @@ def one(repo, clauses) do
end
end
-@spec all(Paging.params()) :: Paging.result()
-def all(params) do
- Paging.page(where(Organization, type: :org), params)
+@spec all(Paging.params()) :: Filter.error() | Paging.result()
+def all(params \\ %{}) do
+ with {:ok, q} <- Filter.filter(params[:filter] || %{}) do
+ Paging.page(q, params)
+ end
end
@spec create(params()) :: {:ok, Organization.t()} | {:error, chgset()}
diff --git a/src/zenflows/vf/organization/filter.ex b/src/zenflows/vf/organization/filter.ex
@@ -0,0 +1,55 @@
+# Zenflows is designed to implement the Valueflows vocabulary,
+# written and maintained by srfsh <info@dyne.org>.
+# Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+defmodule Zenflows.VF.Organization.Filter do
+@moduledoc "Filtering logic of Organizations."
+
+use Zenflows.DB.Schema
+
+import Ecto.Query
+
+alias Ecto.Query
+alias Zenflows.DB.Filter
+alias Zenflows.VF.{Organization, Validate}
+
+@type error() :: Filter.error()
+
+@spec filter(Filter.params()) :: Filter.result()
+def filter(params) do
+ case chgset(params) do
+ %{valid?: true, changes: c} ->
+ {:ok, Enum.reduce(c, where(Organization, type: :org), &f(&2, &1))}
+ %{valid?: false} = cset ->
+ {:error, cset}
+ end
+end
+
+@spec f(Query.t(), {atom(), term()}) :: Query.t()
+defp f(q, {:name, v}),
+ do: where(q, [x], ilike(x.name, ^"%#{Filter.escape_like(v)}%"))
+
+embedded_schema do
+ field :name, :string
+end
+
+@spec chgset(params()) :: Changeset.t()
+defp chgset(params) do
+ %__MODULE__{}
+ |> Changeset.cast(params, [:name])
+ |> Validate.name(:name)
+end
+end
diff --git a/src/zenflows/vf/organization/type.ex b/src/zenflows/vf/organization/type.ex
@@ -109,6 +109,10 @@ object :organization_connection do
field :edges, non_null(list_of(non_null(:organization_edge)))
end
+input_object :organization_filter_params do
+ field :name, :string
+end
+
object :query_organization do
@desc "Find an organization (group) agent by its ID."
field :organization, :organization do
@@ -125,6 +129,7 @@ object :query_organization do
arg :after, :id
arg :last, :integer
arg :before, :id
+ arg :filter, :organization_filter_params
resolve &Resolv.organizations/2
end
end