Hash :
49025a97
Author :
Thomas de Grivel
Date :
2021-11-21T12:18:50
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
defmodule KmxgitWeb.RepositoryController do
use KmxgitWeb, :controller
alias Kmxgit.RepositoryManager
alias Kmxgit.RepositoryManager.Repository
alias Kmxgit.SlugManager
alias Kmxgit.Repo
def new(conn, params) do
action = Routes.repository_path(conn, :create, params["owner"])
changeset = RepositoryManager.change_repository
current_user = conn.assigns.current_user
slug = SlugManager.get_slug(params["owner"])
if !slug do
not_found(conn)
else
user = slug.user
if user do
if user != current_user && !current_user.is_admin do
not_found(conn)
else
conn
|> assign(:action, action)
|> assign(:changeset, changeset)
|> assign(:owner, user)
|> render("new.html")
end
else
org = slug.organisation
if org do
conn
|> assign(:action, action)
|> assign(:changeset, changeset)
|> assign(:current_organisation, org)
|> assign(:owner, org)
|> render("new.html")
else
not_found(conn)
end
end
end
end
def create(conn, params) do
current_user = conn.assigns.current_user
slug = SlugManager.get_slug(params["owner"])
if !slug do
not_found(conn)
else
user = slug.user
if user do
if user != current_user && !current_user.is_admin do
not_found(conn)
else
create_repo(conn, user, params["repository"])
end
else
org = slug.organisation
if org do
create_repo(conn, org, params["repository"])
else
not_found(conn)
end
end
end
end
defp create_repo(conn, owner, params) do
case Repo.transaction(fn ->
case RepositoryManager.create_repository(owner, params) do
{:ok, repo} -> {:ok, repo}
{:error, changeset} -> Repo.rollback changeset
end
end) do
{:ok, repo} ->
conn
|> redirect(to: Routes.repository_path(conn, :show, owner.slug.slug, Repository.splat(repo)))
{:error, changeset} ->
IO.inspect(changeset)
conn
|> assign(:action, Routes.repository_path(conn, :create, owner.slug.slug))
|> assign(:changeset, changeset)
|> assign(:owner, owner)
|> render("new.html")
end
end
defp not_found(conn) do
conn
|> put_status(:not_found)
|> put_view(ErrorView)
|> render(:"404")
end
def show(conn, params) do
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo do
org = repo.organisation
conn
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> assign(:members, Repository.members(repo))
|> render("show.html")
else
not_found(conn)
end
end
def edit(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo do
org = repo.organisation
if org && Enum.find(org.users, &(&1.id == current_user.id)) || repo.user_id == current_user.id do
changeset = RepositoryManager.change_repository(repo)
conn
|> assign(:action, Routes.repository_path(conn, :update, params["owner"], Repository.splat(repo)))
|> assign(:changeset, changeset)
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("edit.html")
else
not_found(conn)
end
else
not_found(conn)
end
end
def update(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo do
org = repo.organisation
if org && Enum.find(org.users, &(&1.id == current_user.id)) || repo.user_id == current_user.id do
case RepositoryManager.update_repository(repo, params["repository"]) do
{:ok, repo} ->
conn
|> redirect(to: Routes.repository_path(conn, :show, params["owner"], Repository.splat(repo)))
{:error, changeset} ->
IO.inspect(changeset)
conn
|> assign(:action, Routes.repository_path(conn, :update, params["owner"], Repository.splat(repo)))
|> assign(:changeset, changeset)
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("edit.html")
end
end
else
not_found(conn)
end
end
def delete(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo do
org = repo.organisation
if org && Enum.find(org.users, &(&1.id == current_user.id)) || repo.user_id == current_user.id do
{:ok, _} = RepositoryManager.delete_repository(repo)
conn
|> redirect(to: Routes.slug_path(conn, :show, params["owner"]))
else
not_found(conn)
end
else
not_found(conn)
end
end
defp assign_current_organisation(conn, nil), do: conn
defp assign_current_organisation(conn, %Ecto.Association.NotLoaded{}), do: conn
defp assign_current_organisation(conn, org) do
assign(conn, :current_organisation, org)
end
end