Commit 14df077d371fc9313deaa211cbd1bc0aac549863

Thomas de Grivel 2021-11-19T12:06:33

repository view

diff --git a/assets/css/app.scss b/assets/css/app.scss
index cc4e9da..ad386c8 100644
--- a/assets/css/app.scss
+++ b/assets/css/app.scss
@@ -4,7 +4,7 @@
 
 @import "./flash.scss";
 
-/* users */
+/* user */
 textarea#user_description {
     min-height: 10em;
 }
@@ -26,6 +26,17 @@ pre.ssh_keys {
     word-wrap: break-word;       /* Internet Explorer 5.5+ */
 }
 
+/* repository */
+#repo_title {
+    a {
+        text-decoration: none;
+        color: black;
+    }
+    a:hover {
+        color: blue;
+    }
+}
+
 /* LiveView specific classes for your customization */
 .phx-no-feedback.invalid-feedback,
 .phx-no-feedback .invalid-feedback {
diff --git a/lib/kmxgit/repository_manager.ex b/lib/kmxgit/repository_manager.ex
index 0adb30d..d596007 100644
--- a/lib/kmxgit/repository_manager.ex
+++ b/lib/kmxgit/repository_manager.ex
@@ -52,15 +52,17 @@ defmodule Kmxgit.RepositoryManager do
     downcase_owner = String.downcase(owner)
     downcase_slug = String.downcase(slug)
     Repo.one from r in Repository,
-      join: o in Organisation,
+      full_join: o in Organisation,
       on: o.id == r.organisation_id,
-      join: os in Slug,
+      full_join: os in Slug,
       on: os.organisation_id == o.id,
-      join: u in User,
+      full_join: u in User,
       on: u.id == r.user_id,
-      join: us in Slug,
+      full_join: us in Slug,
       on: us.user_id == u.id,
-      where: (fragment("lower(?)", os.slug) == ^downcase_owner or fragment("lower(?)", us.slug) == ^downcase_owner) and fragment("lower(?)", r.slug) == ^downcase_slug
+      where: (fragment("lower(?)", os.slug) == ^downcase_owner or fragment("lower(?)", us.slug) == ^downcase_owner) and fragment("lower(?)", r.slug) == ^downcase_slug,
+      preload: [organisation: :slug],
+      preload: [user: :slug]
   end
 
   def delete_repository(%Repository{} = repository) do
diff --git a/lib/kmxgit/repository_manager/repository.ex b/lib/kmxgit/repository_manager/repository.ex
index 7a182c6..ca96d00 100644
--- a/lib/kmxgit/repository_manager/repository.ex
+++ b/lib/kmxgit/repository_manager/repository.ex
@@ -23,4 +23,22 @@ defmodule Kmxgit.RepositoryManager.Repository do
     |> Markdown.validate_markdown(:description)
   end
 
+  def owner(repo = %__MODULE__{organisation: org = %Organisation{}}) do
+    org
+  end
+  def owner(repo = %__MODULE__{user: user = %User{}}) do
+    user
+  end
+
+  def owner_slug(repo) do
+    owner(repo).slug.slug
+  end
+
+  def full_slug(repo) do
+    "#{owner_slug(repo)}/#{repo.slug}"
+  end
+
+  def splat(repo) do
+    String.split(repo.slug, "/")
+  end
 end
diff --git a/lib/kmxgit/slug_manager.ex b/lib/kmxgit/slug_manager.ex
index 4366b87..f71a06a 100644
--- a/lib/kmxgit/slug_manager.ex
+++ b/lib/kmxgit/slug_manager.ex
@@ -25,8 +25,12 @@ defmodule Kmxgit.SlugManager do
   def get_slug(slug) do
     Repo.one from s in Slug,
       where: fragment("lower(slug)") == ^String.downcase(slug),
-      preload: [organisation: [:slug, [users: :slug]]],
-      preload: [user: [[organisations: :slug], :repositories, :slug]],
+      preload: [organisation: [:slug,
+                               users: :slug],
+                user: [:slug,
+                       organisations: :slug,
+                       repositories: [organisation: :slug,
+                                      user: :slug]]],
       limit: 1
   end
 
diff --git a/lib/kmxgit/user_manager.ex b/lib/kmxgit/user_manager.ex
index bed1665..f419854 100644
--- a/lib/kmxgit/user_manager.ex
+++ b/lib/kmxgit/user_manager.ex
@@ -19,7 +19,7 @@ defmodule Kmxgit.UserManager do
     user = Repo.one(from user in User,
       where: [id: ^id],
       preload: [organisations: :slug],
-      preload: :repositories,
+      preload: [repositories: [organisation: :slug, user: :slug]],
       preload: :slug
     )
     user || raise Ecto.NoResultsError
@@ -29,7 +29,7 @@ defmodule Kmxgit.UserManager do
     Repo.one from user in User,
       where: [id: ^id],
       preload: [organisations: :slug],
-      preload: :repositories,
+      preload: [repositories: [organisation: :slug, user: :slug]],
       preload: :slug
   end
 
@@ -38,7 +38,10 @@ defmodule Kmxgit.UserManager do
       join: s in Slug,
       on: s.id == u.slug_id,
       where: fragment("lower(?)", s.slug) == ^String.downcase(slug),
-      limit: 1
+      limit: 1,
+      preload: [:slug,
+                organisations: :slug,
+                repositories: [organisation: :slug, user: :slug]]
   end
 
   def create_user(attrs \\ %{}) do
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index 161cc2c..038c2b7 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -45,28 +45,28 @@ defmodule KmxgitWeb.RepositoryController do
       org = repo.organisation
       user = repo.user
       conn
-      |> assign(:current_repository, repo)
       |> assign_current_organisation(org)
-      |> assign(:owner, org || user)
+      |> assign(:current_repository, repo)
+      |> assign(:repo, repo)
       |> render("show.html")
     else
       not_found(conn)
     end
   end
 
-  defp assign_current_organisation(conn, nil), do: conn
-  defp assign_current_organisation(conn, org) do
-    assign(conn, :current_organisation, org)
-  end
-  
   def edit(conn, params) do
-    org =  RepositoryManager.get_repository_by_slug(params["slug"])
-    changeset = RepositoryManager.change_repository(org)
-    if org do
+    slug = Enum.join(params["slug"], "/")
+    repo =  RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
+    changeset = RepositoryManager.change_repository(repo)
+    if repo do
+      org = repo.organisation
+      user = repo.user
       conn
       |> assign(:action, Routes.repository_path(conn, :update, org.slug.slug))
       |> assign(:changeset, changeset)
-      |> assign(:current_repository, org)
+      |> assign_current_organisation(org)
+      |> assign(:current_repository, repo)
+      |> assign(:repo, repo)
       |> render("edit.html")
     else
       not_found(conn)
@@ -90,4 +90,11 @@ defmodule KmxgitWeb.RepositoryController do
       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
diff --git a/lib/kmxgit_web/controllers/user_controller.ex b/lib/kmxgit_web/controllers/user_controller.ex
index 93461e3..df689a6 100644
--- a/lib/kmxgit_web/controllers/user_controller.ex
+++ b/lib/kmxgit_web/controllers/user_controller.ex
@@ -6,12 +6,12 @@ defmodule KmxgitWeb.UserController do
   alias KmxgitWeb.ErrorView
 
   def show(conn, params) do
-    current_user = conn.assigns[:current_user]
-    user = if current_user && params["login"] == current_user.slug.slug do
-      current_user
-    else
-      UserManager.get_user_by_slug(params["login"])
-    end
+    #current_user = conn.assigns[:current_user]
+    #user = if current_user && params["login"] == current_user.slug.slug do
+    #  current_user
+    #else
+     user = UserManager.get_user_by_slug(params["login"])
+    #end
     if user do
       conn
       |> assign(:page_title, gettext("User %{login}", login: user.slug.slug))
diff --git a/lib/kmxgit_web/templates/layout/nav_connected.html.heex b/lib/kmxgit_web/templates/layout/nav_connected.html.heex
index 277741d..fa077c0 100644
--- a/lib/kmxgit_web/templates/layout/nav_connected.html.heex
+++ b/lib/kmxgit_web/templates/layout/nav_connected.html.heex
@@ -1,3 +1,10 @@
+<%= if @conn.assigns[:current_repository] do %>
+  <li class="nav-item">
+    <%= link Repository.full_slug(@repo),
+             to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo), Repository.splat(@repo)),
+             class: "nav-link" %>
+  </li>
+<% end %>
 <%= if @conn.assigns[:current_organisation] do %>
   <li class="nav-item">
     <%= link @current_organisation.name || @current_organisation.slug.slug,
diff --git a/lib/kmxgit_web/templates/repository/show.html.heex b/lib/kmxgit_web/templates/repository/show.html.heex
new file mode 100644
index 0000000..4046b8b
--- /dev/null
+++ b/lib/kmxgit_web/templates/repository/show.html.heex
@@ -0,0 +1,14 @@
+<div class="container-fluid">
+
+  <div class="row">
+    <div class="col col-12 col-sm-7">
+      <h1 id="repo_title"><%= link Repository.owner_slug(@repo), to: Routes.slug_path(@conn, :show, Repository.owner_slug(@repo)) %>/<%= link @repo.slug, to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo), String.split(@repo.slug, "/")) %></h1>
+    </div>
+    <div class="col col-12 col-sm-4">
+      <%= link gettext("Edit"),
+               to: Routes.repository_path(@conn, :edit, Repository.owner_slug(@repo), String.split(@repo.slug, "/")),
+               class: "btn btn-primary" %>
+    </div>
+  </div>
+
+</div>
diff --git a/lib/kmxgit_web/templates/user/show.html.heex b/lib/kmxgit_web/templates/user/show.html.heex
index dfbcd3a..14189a0 100644
--- a/lib/kmxgit_web/templates/user/show.html.heex
+++ b/lib/kmxgit_web/templates/user/show.html.heex
@@ -18,7 +18,7 @@
       <hr/>
       <h2><%= gettext "Repositories" %></h2>
       <%= for repo <- @user.repositories do %>
-        <%= link repo.slug, to: Routes.repository_path(@conn, :show, @user.slug.slug, String.split(repo.slug, "/")) %>
+        <%= link Repository.full_slug(repo), to: Routes.repository_path(@conn, :show, @user.slug.slug, Repository.splat(repo)) %>
       <% end %>
     </div>
     <div class="col col-12 col-md-4">
diff --git a/lib/kmxgit_web/views/layout_view.ex b/lib/kmxgit_web/views/layout_view.ex
index ab057fe..73e0a36 100644
--- a/lib/kmxgit_web/views/layout_view.ex
+++ b/lib/kmxgit_web/views/layout_view.ex
@@ -1,6 +1,8 @@
 defmodule KmxgitWeb.LayoutView do
   use KmxgitWeb, :view
 
+  alias Kmxgit.RepositoryManager.Repository
+
   def flash_json(conn) do
     {:ok, result} = Jason.encode(get_flash(conn))
     %{"data-flash": result}
diff --git a/lib/kmxgit_web/views/repository_view.ex b/lib/kmxgit_web/views/repository_view.ex
index 08f0b6a..75777eb 100644
--- a/lib/kmxgit_web/views/repository_view.ex
+++ b/lib/kmxgit_web/views/repository_view.ex
@@ -1,3 +1,5 @@
 defmodule KmxgitWeb.RepositoryView do
   use KmxgitWeb, :view
+
+  alias Kmxgit.RepositoryManager.Repository
 end
diff --git a/lib/kmxgit_web/views/user_view.ex b/lib/kmxgit_web/views/user_view.ex
index 2f4632e..cbc67d4 100644
--- a/lib/kmxgit_web/views/user_view.ex
+++ b/lib/kmxgit_web/views/user_view.ex
@@ -1,3 +1,5 @@
 defmodule KmxgitWeb.UserView do
   use KmxgitWeb, :view
+
+  alias Kmxgit.RepositoryManager.Repository
 end