zf

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

domain.ex (4339B)


      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.Satisfaction.Domain do
     19 @moduledoc "Domain logic of Satisfactions."
     20 
     21 alias Ecto.{Changeset, Multi}
     22 alias Zenflows.DB.{Page, Repo, Schema}
     23 alias Zenflows.VF.{Measure, Satisfaction}
     24 
     25 @spec one(Ecto.Repo.t(), Schema.id() | map() | Keyword.t())
     26 	:: {:ok, Satisfaction.t()} | {:error, String.t()}
     27 def one(repo \\ Repo, _)
     28 def one(repo, id) when is_binary(id), do: one(repo, id: id)
     29 def one(repo, clauses) do
     30 	case repo.get_by(Satisfaction, clauses) do
     31 		nil -> {:error, "not found"}
     32 		found -> {:ok, found}
     33 	end
     34 end
     35 
     36 @spec one!(Ecto.Repo.t(), Schema.id() | map() | Keyword.t()) :: Satisfaction.t()
     37 def one!(repo \\ Repo, id_or_clauses) do
     38 	{:ok, value} = one(repo, id_or_clauses)
     39 	value
     40 end
     41 
     42 @spec all(Page.t()) :: {:ok, [Satisfaction.t()]} | {:error, Changeset.t()}
     43 def all(page \\ Page.new()) do
     44 	{:ok, Page.all(Satisfaction, page)}
     45 end
     46 
     47 @spec all!(Page.t()) :: [Satisfaction.t()]
     48 def all!(page \\ Page.new()) do
     49 	{:ok, value} = all(page)
     50 	value
     51 end
     52 
     53 @spec create(Schema.params()) :: {:ok, Satisfaction.t()} | {:error, Changeset.t()}
     54 def create(params) do
     55 	key = multi_key()
     56 	Multi.new()
     57 	|> multi_insert(params)
     58 	|> Repo.transaction()
     59 	|> case do
     60 		{:ok, %{^key => value}} -> {:ok, value}
     61 		{:error, _, cset, _} -> {:error, cset}
     62 	end
     63 end
     64 
     65 @spec create!(Schema.params()) :: Satisfaction.t()
     66 def create!(params) do
     67 	{:ok, value} = create(params)
     68 	value
     69 end
     70 
     71 @spec update(Schema.id(), Schema.params())
     72 	:: {:ok, Satisfaction.t()} | {:error, String.t() | Changeset.t()}
     73 def update(id, params) do
     74 	key = multi_key()
     75 	Multi.new()
     76 	|> multi_update(id, params)
     77 	|> Repo.transaction()
     78 	|> case do
     79 		{:ok, %{^key => value}} -> {:ok, value}
     80 		{:error, _, reason, _} -> {:error, reason}
     81 	end
     82 end
     83 
     84 @spec update!(Schema.id(), Schema.params()) :: Satisfaction.t()
     85 def update!(id, params) do
     86 	{:ok, value} = update(id, params)
     87 	value
     88 end
     89 
     90 @spec delete(Schema.id()) :: {:ok, Satisfaction.t()} | {:error, String.t() | Changeset.t()}
     91 def delete(id) do
     92 	key = multi_key()
     93 	Multi.new()
     94 	|> multi_delete(id)
     95 	|> Repo.transaction()
     96 	|> case do
     97 		{:ok, %{^key => value}} -> {:ok, value}
     98 		{:error, _, reason, _} -> {:error, reason}
     99 	end
    100 end
    101 
    102 @spec delete!(Schema.id) :: Satisfaction.t()
    103 def delete!(id) do
    104 	{:ok, value} = delete(id)
    105 	value
    106 end
    107 
    108 @spec preload(Satisfaction.t(), :satisfies | :satisfied_by_event
    109 		| :satisfied_by_commitment | :resource_quantity | :effort_quantity)
    110 	:: Satisfaction.t()
    111 def preload(satis, x) when x in ~w[
    112 	satisfies satisfied_by_event satisfied_by_commitment
    113 ]a,
    114 	do: Repo.preload(satis, x)
    115 def preload(satis, x) when x in ~w[effort_quantity resource_quantity]a,
    116 	do: Measure.preload(satis, x)
    117 
    118 @spec multi_key() :: atom()
    119 def multi_key(), do: :satisfaction
    120 
    121 @spec multi_one(Multi.t(), term(), Schema.id()) :: Multi.t()
    122 def multi_one(m, key \\ multi_key(), id) do
    123 	Multi.run(m, key, fn repo, _ -> one(repo, id) end)
    124 end
    125 
    126 @spec multi_insert(Multi.t(), term(), Schema.params()) :: Multi.t()
    127 def multi_insert(m, key \\ multi_key(), params) do
    128 	Multi.insert(m, key, Satisfaction.changeset(params))
    129 end
    130 
    131 @spec multi_update(Multi.t(), term(), Schema.id(), Schema.params()) :: Multi.t()
    132 def multi_update(m, key \\ multi_key(), id, params) do
    133 	m
    134 	|> multi_one("#{key}.one", id)
    135 	|> Multi.update(key,
    136 		&Satisfaction.changeset(Map.fetch!(&1, "#{key}.one"), params))
    137 end
    138 
    139 @spec multi_delete(Multi.t(), term(), Schema.id()) :: Multi.t()
    140 def multi_delete(m, key \\ multi_key(), id) do
    141 	m
    142 	|> multi_one("#{key}.one", id)
    143 	|> Multi.delete(key, &Map.fetch!(&1, "#{key}.one"))
    144 end
    145 end