zf

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

commit 493e35eff83e75c1022f07284bf48ce795eeed08
parent a7ce1dfe58aa490035176b5c0f0c835797064535
Author: srfsh <dev@srf.sh>
Date:   Tue, 15 Nov 2022 00:00:42 +0300

Zenflows.VF.EconomicResource: rename Filter to Query

Diffstat:
Msrc/zenflows/vf/economic_resource/domain.ex | 8++++----
Dsrc/zenflows/vf/economic_resource/filter.ex | 69---------------------------------------------------------------------
Asrc/zenflows/vf/economic_resource/query.ex | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 73 insertions(+), 73 deletions(-)

diff --git a/src/zenflows/vf/economic_resource/domain.ex b/src/zenflows/vf/economic_resource/domain.ex @@ -23,7 +23,7 @@ alias Zenflows.DB.{Page, Repo, Schema} alias Zenflows.VF.{ Action, EconomicResource, - EconomicResource.Filter, + EconomicResource.Query, Measure, } @@ -47,15 +47,15 @@ end @spec all(Page.t()) :: {:ok, [EconomicResource.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 @spec all!(Page.t()) :: [EconomicResource.t()] def all!(page \\ Page.new()) do - {:ok, q} = Filter.all(page) - Page.all(q, page) + {:ok, value} = all(page) + value end @spec classifications() :: [String.t()] diff --git a/src/zenflows/vf/economic_resource/filter.ex b/src/zenflows/vf/economic_resource/filter.ex @@ -1,69 +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.EconomicResource.Filter do -@moduledoc false - -import Ecto.Query - -alias Ecto.{Changeset, Queryable} -alias Zenflows.DB.{ID, Page, Schema, Validate} -alias Zenflows.VF.EconomicResource - -@spec all(Page.t()) :: {:ok, Queryable.t()} | {:error, Changeset.t()} -def all(%{filter: nil}), do: {:ok, EconomicResource} -def all(%{filter: params}) do - with {:ok, filters} <- all_validate(params) do - {:ok, Enum.reduce(filters, EconomicResource, &all_f(&2, &1))} - end -end - -@spec all_f(Queryable.t(), {atom(), term()}) :: Queryable.t() -defp all_f(q, {:classified_as, v}), - do: where(q, [x], fragment("? @> ?", x.classified_as, ^v)) -defp all_f(q, {:primary_accountable, v}), - do: where(q, [x], x.primary_accountable_id in ^v) -defp all_f(q, {:custodian, v}), - do: where(q, [x], x.custodian_id in ^v) -defp all_f(q, {:conforms_to, v}), - do: where(q, [x], x.conforms_to_id in ^v) -defp all_f(q, {:gt_onhand_quantity_has_numerical_value, v}), - do: where(q, [x], x.onhand_quantity_has_numerical_value > ^v) - -@spec all_validate(Schema.params()) :: - {:ok, Changeset.data()} | {:error, Changeset.t()} -defp all_validate(params) do - {%{}, %{ - classified_as: {:array, :string}, - primary_accountable: {:array, ID}, - custodian: {:array, ID}, - conforms_to: {:array, ID}, - gt_onhand_quantity_has_numerical_value: :float, - }} - |> Changeset.cast(params, ~w[ - classified_as primary_accountable custodian conforms_to - gt_onhand_quantity_has_numerical_value - ]a) - |> Validate.class(:classified_as) - |> Validate.class(:primary_accountable) - |> Validate.class(:custodian) - |> Validate.class(:conforms_to) - |> Changeset.validate_number(:gt_onhand_quantity_has_numerical_value, - greater_than_or_equal_to: 0) - |> Changeset.apply_action(nil) -end -end diff --git a/src/zenflows/vf/economic_resource/query.ex b/src/zenflows/vf/economic_resource/query.ex @@ -0,0 +1,69 @@ +# 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.EconomicResource.Query do +@moduledoc false + +import Ecto.Query + +alias Ecto.{Changeset, Queryable} +alias Zenflows.DB.{ID, Page, Schema, Validate} +alias Zenflows.VF.EconomicResource + +@spec all(Page.t()) :: {:ok, Queryable.t()} | {:error, Changeset.t()} +def all(%{filter: nil}), do: {:ok, EconomicResource} +def all(%{filter: params}) do + with {:ok, filters} <- all_validate(params) do + {:ok, Enum.reduce(filters, EconomicResource, &all_f(&2, &1))} + end +end + +@spec all_f(Queryable.t(), {atom(), term()}) :: Queryable.t() +defp all_f(q, {:classified_as, v}), + do: where(q, [x], fragment("? @> ?", x.classified_as, ^v)) +defp all_f(q, {:primary_accountable, v}), + do: where(q, [x], x.primary_accountable_id in ^v) +defp all_f(q, {:custodian, v}), + do: where(q, [x], x.custodian_id in ^v) +defp all_f(q, {:conforms_to, v}), + do: where(q, [x], x.conforms_to_id in ^v) +defp all_f(q, {:gt_onhand_quantity_has_numerical_value, v}), + do: where(q, [x], x.onhand_quantity_has_numerical_value > ^v) + +@spec all_validate(Schema.params()) :: + {:ok, Changeset.data()} | {:error, Changeset.t()} +defp all_validate(params) do + {%{}, %{ + classified_as: {:array, :string}, + primary_accountable: {:array, ID}, + custodian: {:array, ID}, + conforms_to: {:array, ID}, + gt_onhand_quantity_has_numerical_value: :float, + }} + |> Changeset.cast(params, ~w[ + classified_as primary_accountable custodian conforms_to + gt_onhand_quantity_has_numerical_value + ]a) + |> Validate.class(:classified_as) + |> Validate.class(:primary_accountable) + |> Validate.class(:custodian) + |> Validate.class(:conforms_to) + |> Changeset.validate_number(:gt_onhand_quantity_has_numerical_value, + greater_than_or_equal_to: 0) + |> Changeset.apply_action(nil) +end +end