zf

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

domain.ex (4216B)


      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.Organization.Domain do
     19 @moduledoc "Domain logic of Organizations."
     20 
     21 alias Ecto.{Changeset, Multi}
     22 alias Zenflows.DB.{Page, Repo, Schema}
     23 alias Zenflows.VF.{Organization, Organization.Query}
     24 
     25 @spec one(Ecto.Repo.t(), Schema.id() | map() | Keyword.t())
     26 	:: {:ok, Organization.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 	import Ecto.Query
     31 
     32 	case repo.get_by(where(Organization, type: :org), clauses) do
     33 		nil -> {:error, "not found"}
     34 		found -> {:ok, found}
     35 	end
     36 end
     37 
     38 @spec one!(Ecto.Repo.t(), Schema.id() | map() | Keyword.t()) :: Organization.t()
     39 def one!(repo \\ Repo, id_or_clauses) do
     40 	{:ok, value} = one(repo, id_or_clauses)
     41 	value
     42 end
     43 
     44 @spec all(Page.t()) :: {:ok, [Organization.t()]} | {:error, Changeset.t()}
     45 def all(page \\ Page.new()) do
     46 	with {:ok, q} <- Query.all(page) do
     47 		{:ok, Page.all(q, page)}
     48 	end
     49 end
     50 
     51 @spec all!(Page.t()) :: [Organization.t()]
     52 def all!(page \\ Page.new()) do
     53 	{:ok, value} = all(page)
     54 	value
     55 end
     56 
     57 @spec create(Schema.params()) :: {:ok, Organization.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, _, reason, _} -> {:error, reason}
     66 	end
     67 end
     68 
     69 @spec create!(Schema.params()) :: Organization.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, Organization.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()) :: Organization.t()
     89 def update!(id, params) do
     90 	{:ok, value} = update(id, params)
     91 	value
     92 end
     93 
     94 @spec delete(Schema.id())
     95 	:: {:ok, Organization.t()} | {:error, String.t() | Changeset.t()}
     96 def delete(id) do
     97 	key = multi_key()
     98 	Multi.new()
     99 	|> multi_delete(id)
    100 	|> Repo.transaction()
    101 	|> case do
    102 		{:ok, %{^key => value}} -> {:ok, value}
    103 		{:error, _, reason, _} -> {:error, reason}
    104 	end
    105 end
    106 
    107 @spec delete!(Schema.id()) :: Organization.t()
    108 def delete!(id) do
    109 	{:ok, value} = delete(id)
    110 	value
    111 end
    112 
    113 @spec preload(Organization.t(), :images | :primary_location) :: Organization.t()
    114 def preload(org, x) when x in ~w[images primary_location]a do
    115 	Repo.preload(org, x)
    116 end
    117 
    118 @spec multi_key() :: atom()
    119 def multi_key(), do: :organization
    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, Organization.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 		&Organization.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