zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

commit bbee16cfb9cdf9b992f54af50039862081d7456a
parent cdcdd4e988c9cf8af69e5fd298e730c1db4b6601
Author: srfsh <dev@srf.sh>
Date:   Tue, 15 Nov 2022 00:02:29 +0300

Zenflows.VF.Person: rename Filter to Query

Diffstat:
Msrc/zenflows/vf/person/domain.ex | 4++--
Dsrc/zenflows/vf/person/filter.ex | 62--------------------------------------------------------------
Asrc/zenflows/vf/person/query.ex | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 64 insertions(+), 64 deletions(-)

diff --git a/src/zenflows/vf/person/domain.ex b/src/zenflows/vf/person/domain.ex @@ -22,7 +22,7 @@ import Ecto.Query alias Ecto.{Changeset, Multi} alias Zenflows.DB.{Page, Repo, Schema} -alias Zenflows.VF.{Person, Person.Filter} +alias Zenflows.VF.{Person, Person.Query} @spec one(Ecto.Repo.t(), Schema.id() | map() | Keyword.t()) :: {:ok, Person.t()} | {:error, String.t()} @@ -43,7 +43,7 @@ end @spec all(Page.t()) :: {:ok, [Person.t()]} | {:error, Changeset.t()} def all(page \\ Page.new()) do - with {:ok, q} <- Filter.all(page) do + with {:ok, q} <- Query.all(page) do {:ok, Page.all(q, page)} end end diff --git a/src/zenflows/vf/person/filter.ex b/src/zenflows/vf/person/filter.ex @@ -1,62 +0,0 @@ -# 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.Person.Filter do -@moduledoc false - -import Ecto.Query - -alias Ecto.{Changeset, Queryable} -alias Zenflows.DB.{Page, Schema, Validate} -alias Zenflows.VF.Person - -@spec all(Page.t()) :: {:ok, Queryable.t()} | {:error, Changeset.t()} -def all(%{filter: nil}), do: {:ok, where(Person, type: :per)} -def all(%{filter: params}) do - with {:ok, filters} <- all_validate(params) do - {:ok, Enum.reduce(filters, where(Person, type: :per), &all_f(&2, &1))} - end -end - -@spec all_f(Queryable.t(), {atom(), term()}) :: Queryable.t() -defp all_f(q, {:name, v}), - do: where(q, [x], ilike(x.name, ^"%#{v}%")) -defp all_f(q, {:or_name, v}), - do: or_where(q, [x], ilike(x.name, ^"%#{v}%")) -defp all_f(q, {:user, v}), - do: where(q, [x], ilike(x.user, ^"%#{v}%")) -defp all_f(q, {:or_user, v}), - do: or_where(q, [x], ilike(x.user, ^"%#{v}")) - -@spec all_validate(Schema.params()) :: - {:ok, Changeset.data()} | {:error, Changeset.t()} -defp all_validate(params) do - {%{}, %{name: :string, or_name: :string, user: :string, or_user: :string}} - |> Changeset.cast(params, ~w[name or_name user or_user]a) - |> Validate.name(:name) - |> Validate.name(:or_name) - |> Validate.name(:user) - |> Validate.name(:or_user) - |> Validate.exist_xor([:name, :or_name]) - |> Validate.exist_xor([:user, :or_user]) - |> Validate.escape_like(:name) - |> Validate.escape_like(:or_name) - |> Validate.escape_like(:user) - |> Validate.escape_like(:or_user) - |> Changeset.apply_action(nil) -end -end diff --git a/src/zenflows/vf/person/query.ex b/src/zenflows/vf/person/query.ex @@ -0,0 +1,62 @@ +# 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.Person.Query do +@moduledoc false + +import Ecto.Query + +alias Ecto.{Changeset, Queryable} +alias Zenflows.DB.{Page, Schema, Validate} +alias Zenflows.VF.Person + +@spec all(Page.t()) :: {:ok, Queryable.t()} | {:error, Changeset.t()} +def all(%{filter: nil}), do: {:ok, where(Person, type: :per)} +def all(%{filter: params}) do + with {:ok, filters} <- all_validate(params) do + {:ok, Enum.reduce(filters, where(Person, type: :per), &all_f(&2, &1))} + end +end + +@spec all_f(Queryable.t(), {atom(), term()}) :: Queryable.t() +defp all_f(q, {:name, v}), + do: where(q, [x], ilike(x.name, ^"%#{v}%")) +defp all_f(q, {:or_name, v}), + do: or_where(q, [x], ilike(x.name, ^"%#{v}%")) +defp all_f(q, {:user, v}), + do: where(q, [x], ilike(x.user, ^"%#{v}%")) +defp all_f(q, {:or_user, v}), + do: or_where(q, [x], ilike(x.user, ^"%#{v}")) + +@spec all_validate(Schema.params()) :: + {:ok, Changeset.data()} | {:error, Changeset.t()} +defp all_validate(params) do + {%{}, %{name: :string, or_name: :string, user: :string, or_user: :string}} + |> Changeset.cast(params, ~w[name or_name user or_user]a) + |> Validate.name(:name) + |> Validate.name(:or_name) + |> Validate.name(:user) + |> Validate.name(:or_user) + |> Validate.exist_xor([:name, :or_name]) + |> Validate.exist_xor([:user, :or_user]) + |> Validate.escape_like(:name) + |> Validate.escape_like(:or_name) + |> Validate.escape_like(:user) + |> Validate.escape_like(:or_user) + |> Changeset.apply_action(nil) +end +end