zf

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

domain.ex (3955B)


      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.RoleBehavior.Domain do
     19 @moduledoc "Domain logic of RoleBehaviors."
     20 
     21 alias Ecto.{Changeset, Multi}
     22 alias Zenflows.DB.{Page, Repo, Schema}
     23 alias Zenflows.VF.RoleBehavior
     24 
     25 @spec one(Ecto.Repo.t(), Schema.id() | map() | Keyword.t())
     26 	:: {:ok, RoleBehavior.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(RoleBehavior, 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()) :: RoleBehavior.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, [RoleBehavior.t()]} | {:error, Changeset.t()}
     43 def all(page \\ Page.new()) do
     44 	{:ok, Page.all(RoleBehavior, page)}
     45 end
     46 
     47 @spec all!(Page.t()) :: [RoleBehavior.t()]
     48 def all!(page \\ Page.new()) do
     49 	{:ok, value} = all(page)
     50 	value
     51 end
     52 
     53 @spec create(Schema.params())
     54 	:: {:ok, RoleBehavior.t()} | {:error, Changeset.t()}
     55 def create(params) do
     56 	key = multi_key()
     57 	Multi.new()
     58 	|> multi_insert(params)
     59 	|> Repo.transaction()
     60 	|> case do
     61 		{:ok, %{^key => value}} -> {:ok, value}
     62 		{:error, _, reason, _} -> {:error, reason}
     63 	end
     64 end
     65 
     66 @spec create!(Schema.params()) :: RoleBehavior.t()
     67 def create!(params) do
     68 	{:ok, value} = create(params)
     69 	value
     70 end
     71 
     72 @spec update(Schema.id(), Schema.params())
     73 	:: {:ok, RoleBehavior.t()} | {:error, String.t() | Changeset.t()}
     74 def update(id, params) do
     75 	key = multi_key()
     76 	Multi.new()
     77 	|> multi_update(id, params)
     78 	|> Repo.transaction()
     79 	|> case do
     80 		{:ok, %{^key => value}} -> {:ok, value}
     81 		{:error, _, reason, _} -> {:error, reason}
     82 	end
     83 end
     84 
     85 @spec update!(Schema.id(), Schema.params()) :: RoleBehavior.t()
     86 def update!(id, params) do
     87 	{:ok, value} = update(id, params)
     88 	value
     89 end
     90 
     91 @spec delete(Schema.id())
     92 	:: {:ok, RoleBehavior.t()} | {:error, String.t() | Changeset.t()}
     93 def delete(id) do
     94 	key = multi_key()
     95 	Multi.new()
     96 	|> multi_delete(id)
     97 	|> Repo.transaction()
     98 	|> case do
     99 		{:ok, %{^key => value}} -> {:ok, value}
    100 		{:error, _, reason, _} -> {:error, reason}
    101 	end
    102 end
    103 
    104 @spec delete!(Schema.id()) :: RoleBehavior.t()
    105 def delete!(id) do
    106 	{:ok, value} = delete(id)
    107 	value
    108 end
    109 
    110 @spec multi_key() :: atom()
    111 def multi_key(), do: :role_behavior
    112 
    113 @spec multi_one(Multi.t(), term(), Schema.id()) :: Multi.t()
    114 def multi_one(m, key \\ multi_key(), id) do
    115 	Multi.run(m, key, fn repo, _ -> one(repo, id) end)
    116 end
    117 
    118 @spec multi_insert(Multi.t(), term(), Schema.params()) :: Multi.t()
    119 def multi_insert(m, key \\ multi_key(), params) do
    120 	Multi.insert(m, key, RoleBehavior.changeset(params))
    121 end
    122 
    123 @spec multi_update(Multi.t(), term(), Schema.id(), Schema.params()) :: Multi.t()
    124 def multi_update(m, key \\ multi_key(), id, params) do
    125 	m
    126 	|> multi_one("#{key}.one", id)
    127 	|> Multi.update(key,
    128 		&RoleBehavior.changeset(Map.fetch!(&1, "#{key}.one"), params))
    129 end
    130 
    131 @spec multi_delete(Multi.t(), term(), Schema.id()) :: Multi.t()
    132 def multi_delete(m, key \\ multi_key(), id) do
    133 	m
    134 	|> multi_one("#{key}.one", id)
    135 	|> Multi.delete(key, &Map.fetch!(&1, "#{key}.one"))
    136 end
    137 end