zf

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

domain.ex (4261B)


      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.RecipeProcess.Domain do
     19 @moduledoc "Domain logic of RecipeProcesses."
     20 
     21 alias Ecto.{Changeset, Multi}
     22 alias Zenflows.DB.{Page, Repo, Schema}
     23 alias Zenflows.VF.{Duration, RecipeProcess}
     24 
     25 @spec one(Ecto.Repo.t(), Schema.id() | map() | Keyword.t())
     26 	:: {:ok, RecipeProcess.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(RecipeProcess, 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())
     37 	:: RecipeProcess.t()
     38 def one!(repo \\ Repo, id_or_clauses) do
     39 	{:ok, value} = one(repo, id_or_clauses)
     40 	value
     41 end
     42 
     43 @spec all(Page.t()) :: {:ok, [RecipeProcess.t()]} | {:error, Changeset.t()}
     44 def all(page \\ Page.new()) do
     45 	{:ok, Page.all(RecipeProcess, page)}
     46 end
     47 
     48 @spec all!(Page.t()) :: [RecipeProcess.t()]
     49 def all!(page \\ Page.new()) do
     50 	{:ok, value} = all(page)
     51 	value
     52 end
     53 
     54 @spec create(Schema.params())
     55 	:: {:ok, RecipeProcess.t()} | {:error, Changeset.t()}
     56 def create(params) do
     57 	key = multi_key()
     58 	Multi.new()
     59 	|> multi_insert(params)
     60 	|> Repo.transaction()
     61 	|> case do
     62 		{:ok, %{^key => value}} -> {:ok, value}
     63 		{:error, _, reason, _} -> {:error, reason}
     64 	end
     65 end
     66 
     67 @spec create!(Schema.params()) :: RecipeProcess.t()
     68 def create!(params) do
     69 	{:ok, value} = create(params)
     70 	value
     71 end
     72 
     73 @spec update(Schema.id(), Schema.params())
     74 	:: {:ok, RecipeProcess.t()} | {:error, String.t() | Changeset.t()}
     75 def update(id, params) do
     76 	key = multi_key()
     77 	Multi.new()
     78 	|> multi_update(id, params)
     79 	|> Repo.transaction()
     80 	|> case do
     81 		{:ok, %{^key => value}} -> {:ok, value}
     82 		{:error, _, reason, _} -> {:error, reason}
     83 	end
     84 end
     85 
     86 @spec update!(Schema.id(), Schema.params()) :: RecipeProcess.t()
     87 def update!(id, params) do
     88 	{:ok, value} = update(id, params)
     89 	value
     90 end
     91 
     92 @spec delete(Schema.id())
     93 	:: {:ok, RecipeProcess.t()} | {:error, String.t() | Changeset.t()}
     94 def delete(id) do
     95 	key = multi_key()
     96 	Multi.new()
     97 	|> multi_delete(id)
     98 	|> Repo.transaction()
     99 	|> case do
    100 		{:ok, %{^key => value}} -> {:ok, value}
    101 		{:error, _, reason, _} -> {:error, reason}
    102 	end
    103 end
    104 
    105 @spec delete!(Schema.id()) :: RecipeProcess.t()
    106 def delete!(id) do
    107 	{:ok, value} = delete(id)
    108 	value
    109 end
    110 
    111 @spec preload(RecipeProcess.t(), :has_duration | :process_conforms_to)
    112 	:: RecipeProcess.t()
    113 def preload(rec_proc, :has_duration),
    114 	do: Duration.preload(rec_proc, :has_duration)
    115 def preload(rec_proc, :process_conforms_to),
    116 	do: Repo.preload(rec_proc, :process_conforms_to)
    117 
    118 @spec multi_key() :: atom()
    119 def multi_key(), do: :recipe_process
    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, RecipeProcess.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 		&RecipeProcess.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