commit 72af8b1b5385a95ca6d26599e0ee35a194679af9
parent 352d65b1fafad3fe14276c18648f8e1ccab23e26
Author: srfsh <dev@srf.sh>
Date: Fri, 23 Sep 2022 16:56:57 +0300
Zenflows.VF.Agent.{Domain,Filter}: add filtering
Diffstat:
3 files changed, 66 insertions(+), 4 deletions(-)
diff --git a/src/zenflows/vf/agent/domain.ex b/src/zenflows/vf/agent/domain.ex
@@ -19,7 +19,7 @@ defmodule Zenflows.VF.Agent.Domain do
@moduledoc "Domain logic of Agents."
alias Zenflows.DB.{Paging, Repo}
-alias Zenflows.VF.Agent
+alias Zenflows.VF.{Agent, Agent.Filter}
@typep repo() :: Ecto.Repo.t()
@typep id() :: Zenflows.DB.Schema.id()
@@ -34,9 +34,11 @@ def one(repo, clauses) do
end
end
-@spec all(Paging.params()) :: Paging.result()
-def all(params) do
- Paging.page(Agent, 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 preload(Agent.t(), :images | :primary_location) :: Agent.t()
diff --git a/src/zenflows/vf/agent/filter.ex b/src/zenflows/vf/agent/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.Agent.Filter do
+@moduledoc "Filtering logic of Agents."
+
+use Zenflows.DB.Schema
+
+import Ecto.Query
+
+alias Ecto.Query
+alias Zenflows.DB.Filter
+alias Zenflows.VF.{Agent, 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, Agent, &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/agent/type.ex b/src/zenflows/vf/agent/type.ex
@@ -70,6 +70,10 @@ object :agent_connection do
field :edges, non_null(list_of(non_null(:agent_edge)))
end
+input_object :agent_filter_params do
+ field :name, :string
+end
+
object :query_agent do
@desc "Loads details of the currently authenticated agent."
field :my_agent, :agent do
@@ -91,6 +95,7 @@ object :query_agent do
arg :after, :id
arg :last, :integer
arg :before, :id
+ arg :filter, :agent_filter_params
resolve &Resolv.agents/2
end
end