zf

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

domain.ex (4470B)


      1 # Zenflows is designed to implement the Valueflows vocabulary,
      2 # written and maintained by srfsh <info@dyne.org>.
      3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>.
      4 #
      5 # This program is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU Affero General Public License as published by
      7 # the Free Software Foundation, either version 3 of the License, or
      8 # (at your option) any later version.
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU Affero General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU Affero General Public License
     16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 
     18 defmodule Zenflows.VF.Intent.Domain do
     19 @moduledoc "Domain logic of Intents."
     20 
     21 alias Ecto.{Changeset, Multi}
     22 alias Zenflows.DB.{Page, Repo, Schema}
     23 alias Zenflows.VF.{
     24 	Action,
     25 	Intent,
     26 	Measure,
     27 }
     28 
     29 @spec one(Ecto.Repo.t(), Schema.id() | map() | Keyword.t())
     30 	:: {:ok, Intent.t()} | {:error, String.t()}
     31 def one(repo \\ Repo, _)
     32 def one(repo, id) when is_binary(id), do: one(repo, id: id)
     33 def one(repo, clauses) do
     34 	case repo.get_by(Intent, clauses) do
     35 		nil -> {:error, "not found"}
     36 		found -> {:ok, found}
     37 	end
     38 end
     39 
     40 @spec one!(Ecto.Repo.t(), Schema.id() | map() | Keyword.t()) :: Intent.t()
     41 def one!(repo \\ Repo, id_or_clauses) do
     42 	{:ok, value} = one(repo, id_or_clauses)
     43 	value
     44 end
     45 
     46 @spec all(Page.t()) :: {:ok, [Intent.t()]} | {:error, Changeset.t()}
     47 def all(page \\ Page.new()) do
     48 	{:ok, Page.all(Intent, page)}
     49 end
     50 
     51 @spec all!(Page.t()) :: [Intent.t()]
     52 def all!(page \\ Page.new()) do
     53 	{:ok, value} = all(page)
     54 	value
     55 end
     56 
     57 @spec create(Schema.params()) :: {:ok, Intent.t()} | {:error, Changeset.t()}
     58 def create(params) do
     59 	key = multi_key()
     60 	Multi.new()
     61 	|> multi_insert(params)
     62 	|> Repo.transaction()
     63 	|> case do
     64 		{:ok, %{^key => value}} -> {:ok, value}
     65 		{:error, _, cset, _} -> {:error, cset}
     66 	end
     67 end
     68 
     69 @spec create!(Schema.params()) :: Intent.t()
     70 def create!(params) do
     71 	{:ok, value} = create(params)
     72 	value
     73 end
     74 
     75 @spec update(Schema.id(), Schema.params())
     76 	:: {:ok, Intent.t()} | {:error, String.t() | Changeset.t()}
     77 def update(id, params) do
     78 	key = multi_key()
     79 	Multi.new()
     80 	|> multi_update(id, params)
     81 	|> Repo.transaction()
     82 	|> case do
     83 		{:ok, %{^key => value}} -> {:ok, value}
     84 		{:error, _, reason, _} -> {:error, reason}
     85 	end
     86 end
     87 
     88 @spec update!(Schema.id(), Schema.params()) :: Intent.t()
     89 def update!(id, params) do
     90 	{:ok, value} = update(id, params)
     91 	value
     92 end
     93 
     94 @spec delete(Schema.id()) :: {:ok, Intent.t()} | {:error, String.t() | Changeset.t()}
     95 def delete(id) do
     96 	key = multi_key()
     97 	Multi.new()
     98 	|> multi_delete(id)
     99 	|> Repo.transaction()
    100 	|> case do
    101 		{:ok, %{^key => value}} -> {:ok, value}
    102 		{:error, _, reason, _} -> {:error, reason}
    103 	end
    104 end
    105 
    106 @spec delete!(Schema.id) :: Intent.t()
    107 def delete!(id) do
    108 	{:ok, value} = delete(id)
    109 	value
    110 end
    111 
    112 @spec preload(Intent.t(), :action | :input_of | :output_of
    113 		| :provider | :receiver
    114 		| :resource_inventoried_as  | :resource_conforms_to
    115 		| :resource_quantity | :effort_quantity | :available_quantity
    116 		| :at_location | :published_in)
    117 	:: Intent.t()
    118 def preload(int, x) when x in ~w[
    119 	input_of output_of provider receiver resource_inventoried_as
    120 	resource_conforms_to at_location published_in
    121 ]a,
    122 	do: Repo.preload(int, x)
    123 def preload(int, :action), do: Action.preload(int, :action)
    124 def preload(int, x) when x in ~w[
    125 	effort_quantity available_quantity resource_quantity
    126 ]a,
    127 	do: Measure.preload(int, x)
    128 
    129 @spec multi_key() :: atom()
    130 def multi_key(), do: :intent
    131 
    132 @spec multi_one(Multi.t(), term(), Schema.id()) :: Multi.t()
    133 def multi_one(m, key \\ multi_key(), id) do
    134 	Multi.run(m, key, fn repo, _ -> one(repo, id) end)
    135 end
    136 
    137 @spec multi_insert(Multi.t(), term(), Schema.params()) :: Multi.t()
    138 def multi_insert(m, key \\ multi_key(), params) do
    139 	Multi.insert(m, key, Intent.changeset(params))
    140 end
    141 
    142 @spec multi_update(Multi.t(), term(), Schema.id(), Schema.params()) :: Multi.t()
    143 def multi_update(m, key \\ multi_key(), id, params) do
    144 	m
    145 	|> multi_one("#{key}.one", id)
    146 	|> Multi.update(key,
    147 		&Intent.changeset(Map.fetch!(&1, "#{key}.one"), params))
    148 end
    149 
    150 @spec multi_delete(Multi.t(), term(), Schema.id()) :: Multi.t()
    151 def multi_delete(m, key \\ multi_key(), id) do
    152 	m
    153 	|> multi_one("#{key}.one", id)
    154 	|> Multi.delete(key, &Map.fetch!(&1, "#{key}.one"))
    155 end
    156 end