Edit

kmx.io/kmxgit/lib/kmxgit_web/controllers/admin/organisation_controller.ex

Branch :

  • lib/kmxgit_web/controllers/admin/organisation_controller.ex
  • ## 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 KmxgitWeb.Admin.OrganisationController do
      use KmxgitWeb, :controller
    
      alias Kmxgit.IndexParams
      alias Kmxgit.GitAuth
      alias Kmxgit.GitManager
      alias Kmxgit.OrganisationManager
      alias Kmxgit.Repo
      alias Kmxgit.SlugManager
      alias KmxgitWeb.ErrorView
    
      def index(conn, params) do
        index_params = %IndexParams{}
        |> KmxgitWeb.Admin.page_params(params["page"], params["per"])
        |> KmxgitWeb.Admin.search_param(params["search"])
        |> KmxgitWeb.Admin.sort_param(params["sort"])
        pagination = OrganisationManager.list_organisations(index_params)
        conn
        |> assign(:index, index_params)
        |> assign(:pagination, pagination)
        |> assign(:search, params["search"])
        |> assign(:search_action, Routes.admin_organisation_path(conn, :index, sort: params["sort"], search: params["search"]))
        |> assign(:sort, params["sort"])
        |> render("index.html")
      end
    
      def new(conn, _params) do
        changeset = OrganisationManager.change_organisation
        conn
        |> assign(:action, Routes.admin_organisation_path(conn, :create))
        |> assign(:changeset, changeset)
        |> assign(:org, nil)
        |> render("new.html")
      end
    
      def create(conn, params) do
        org_params = params["organisation"]
        case OrganisationManager.admin_create_organisation(org_params) do
          {:ok, org} ->
            conn
            |> redirect(to: Routes.admin_organisation_path(conn, :show, org))
          {:error, changeset} ->
            conn
            |> assign(:action, Routes.admin_organisation_path(conn, :create))
            |> assign(:changeset, changeset)
            |> assign(:org, nil)
            |> render("new.html")
        end
      end
    
      def show(conn, params) do
        org = OrganisationManager.get_organisation(params["id"])
        if org do
          org = org
          |> OrganisationManager.put_disk_usage()
          conn
          |> assign(:org, org)
          |> render("show.html")
        else
          not_found(conn)
        end
      end
    
      def edit(conn, params) do
        org = OrganisationManager.get_organisation(params["id"])
        if org do
          changeset = OrganisationManager.change_organisation(org)
          conn
          |> assign(:action, Routes.admin_organisation_path(conn, :update, org))
          |> assign(:changeset, changeset)
          |> assign(:org, org)
          |> render("edit.html")
        else
          not_found(conn)
        end
      end
    
      def update(conn, params) do
        org = OrganisationManager.get_organisation(params["id"])
        if org do
          case Repo.transaction(fn ->
                case OrganisationManager.update_organisation(org, params["organisation"]) do
                  {:ok, org1} ->
                    if org.slug_ != org1.slug_ do
                      case SlugManager.rename_slug(org.slug_, org1.slug_) do
                        {:ok, _slug} ->
                          case GitManager.rename_dir(org.slug_, org1.slug_) do
                            :ok ->
                              GitAuth.update()
                              org1
                            {:error, err} -> Repo.rollback(err)
                          end
                        {:error, err} -> Repo.rollback(err)
                      end
                    else
                      org1
                    end
                  {:error, changeset} -> Repo.rollback(changeset)
                end
              end) do
            {:ok, org1} ->
              conn
              |> redirect(to: Routes.admin_organisation_path(conn, :show, org1))
            {:error, changeset} ->
              conn
              |> assign(:action, Routes.admin_organisation_path(conn, :update, org))
              |> assign(:changeset, changeset)
              |> render("edit.html")
                        
          end
        else
          not_found(conn)
        end
      end
    
      def add_user(conn, params) do
        org = OrganisationManager.get_organisation!(params["organisation_id"])
        conn
        |> assign(:action, Routes.admin_organisation__path(conn, :add_user_post, org))
        |> assign(:org, org)
        |> render("add_user.html")
      end
    
      def add_user_post(conn, params) do
        login = params["organisation"]["login"]
        org = OrganisationManager.get_organisation!(params["organisation_id"])
        case OrganisationManager.add_user(org, login) do
          {:ok, org1} ->
            GitAuth.update()
            conn
            |> redirect(to: Routes.admin_organisation_path(conn, :show, org1))
          {:error, _} ->
            conn
            |> assign(:action, Routes.admin_organisation__path(conn, :add_user_post, org))
            |> assign(:org, org)
            |> render("add_user.html")
        end
      end
    
      def remove_user(conn, params) do
        org = OrganisationManager.get_organisation!(params["organisation_id"])
        conn
        |> assign(:action, Routes.admin_organisation__path(conn, :remove_user_post, org))
        |> assign(:org, org)
        |> render("remove_user.html")
      end
    
      def remove_user_post(conn, params) do
        login = params["organisation"]["login"]
        org = OrganisationManager.get_organisation!(params["organisation_id"])
        case OrganisationManager.remove_user(org, login) do
          {:ok, org} ->
            GitAuth.update()
            conn
            |> redirect(to: Routes.admin_organisation_path(conn, :show, org))
          {:error, _} ->
            conn
            |> assign(:action, Routes.admin_organisation__path(conn, :remove_user_post, org))
            |> assign(:org, org)
            |> render("remove_user.html")
        end
      end
    
      def delete(conn, params) do
        org = OrganisationManager.get_organisation(params["id"])
        if org do
          case OrganisationManager.delete_organisation(org) do
            {:ok, _} ->
              GitAuth.update()
              conn
              |> redirect(to: Routes.admin_organisation_path(conn, :index))
            {:error, _} ->
              conn
              |> redirect(to: Routes.admin_organisation_path(conn, :show, org))
          end
        else
          not_found(conn)
        end
      end
    end