Branch
Hash :
4d8f6ed5
Author :
Thomas de Grivel
Date :
2022-11-02T18:24:08
licence
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
## kmxgit
## Copyright 2022 kmx.io <contact@kmx.io>
##
## Permission is hereby granted to use this software granted
## the above copyright notice and this permission paragraph
## are included in all copies and substantial portions of this
## software.
##
## THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
## PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
## THIS SOFTWARE.
defmodule Kmxgit.OrganisationManager.Organisation do
use Ecto.Schema
import Ecto.Changeset
alias Kmxgit.Git
alias Kmxgit.RepositoryManager.Repository
alias Kmxgit.SlugManager.Slug
alias Kmxgit.UserManager.User
schema "organisations" do
field :description, :string
field :disk_usage, :integer, default: 0
field :name, :string
has_many :owned_repositories, Repository, on_delete: :delete_all
many_to_many :users, User, join_through: "users_organisations", on_replace: :delete, on_delete: :delete_all
has_one :slug, Slug, on_delete: :delete_all
field :slug_, :string
timestamps()
end
@doc false
def changeset(organisation, attrs \\ %{}) do
organisation
|> cast(attrs, [:description, :name, :slug_])
|> validate_required([:slug_])
|> Markdown.validate_markdown(:description)
|> foreign_key_constraint(:owned_repositories, name: :repositories_organisation_id_fkey)
|> validate_format(:slug_, ~r/^[A-Za-z][-_+.0-9A-Za-z]{1,64}$/)
end
def owner?(org, user) do
if user do
org.users
|> Enum.find(fn u ->
u.id == user.id
end)
end
end
def owned_repositories(org) do
if org do
org.owned_repositories
|> Enum.sort_by(&Repository.full_slug/1)
end
end
def disk_usage(org) do
if org do
Git.dir_disk_usage(org.slug_)
else
0
end
end
end