Hash :
4d8f6ed5
Author :
Thomas de Grivel
Date :
2022-11-02T18:24:08
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 69 70 71 72 73 74 75 76 77 78
## 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.SlugManager do
import Ecto.Query, warn: false
alias Kmxgit.Repo
alias Kmxgit.OrganisationManager.Organisation
alias Kmxgit.SlugManager.Slug
alias Kmxgit.UserManager.User
def list_all_slugs do
Slug
|> Repo.all()
end
def create_slug(slug) when is_binary(slug) do
%Slug{}
|> Slug.changeset(%{slug: slug})
|> Repo.insert()
end
def create_slug(%Organisation{id: id, slug_: slug}) do
%Slug{}
|> Slug.create_changeset(%{slug: slug, organisation_id: id})
|> Repo.insert()
end
def create_slug(%User{id: id, slug_: slug}) do
%Slug{}
|> Slug.create_changeset(%{slug: slug, user_id: id})
|> Repo.insert()
end
def update_slug(slug, attrs) do
slug
|> Slug.changeset(attrs)
|> Repo.update()
end
def rename_slug(from, to) do
slug = Repo.one from s in Slug,
where: s.slug == ^from
update_slug(slug, %{slug: to})
end
def get_slug(slug) do
Repo.one from s in Slug,
where: s.slug == ^slug,
preload: [organisation: [:users,
owned_repositories: [:members,
:user,
organisation: [:users]]],
user: [:organisations,
owned_repositories: [:members,
:organisation,
:user]]],
limit: 1
end
def delete_slug(slug) when is_binary(slug) do
s = Repo.one from s in Slug,
where: s.slug == ^slug
if s, do: Repo.delete(s), else: :ok
end
def delete_slug(%Slug{} = slug) do
Repo.delete(slug)
end
end