From c047f0af6d047f5fc483d1d62828be2221ec8fc4 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 29 Nov 2019 17:01:54 -0500 Subject: [PATCH] staff page --- config/footer.json | 2 +- .../controllers/staff_controller.ex | 24 +++++ lib/philomena_web/router.ex | 1 + .../templates/staff/index.html.slime | 12 +++ lib/philomena_web/views/staff_view.ex | 3 + .../controllers/staff_controller_test.exs | 88 +++++++++++++++++++ 6 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 lib/philomena_web/controllers/staff_controller.ex create mode 100644 lib/philomena_web/templates/staff/index.html.slime create mode 100644 lib/philomena_web/views/staff_view.ex create mode 100644 test/philomena_web/controllers/staff_controller_test.exs diff --git a/config/footer.json b/config/footer.json index f9881a0b..d1886a71 100644 --- a/config/footer.json +++ b/config/footer.json @@ -40,7 +40,7 @@ "Help & Information": [ { "title": "Changelog", - "url": "/changelog", + "url": "https://github.com/derpibooru/philomena/commits/master", "bold": true }, { diff --git a/lib/philomena_web/controllers/staff_controller.ex b/lib/philomena_web/controllers/staff_controller.ex new file mode 100644 index 00000000..be351136 --- /dev/null +++ b/lib/philomena_web/controllers/staff_controller.ex @@ -0,0 +1,24 @@ +defmodule PhilomenaWeb.StaffController do + use PhilomenaWeb, :controller + + alias Philomena.Users.User + alias Philomena.Repo + import Ecto.Query + + def index(conn, _params) do + users = + User + |> where([u], u.role in ["admin", "moderator", "assistant"]) + |> order_by(asc: :name) + |> Repo.all() + + categories = %{ + "Administrators" => Enum.filter(users, & &1.role == "admin"), + "Technical Team" => Enum.filter(users, & &1.role != "admin" and &1.secondary_role not in [nil, ""]), + "Moderators" => Enum.filter(users, & &1.role != "admin" and &1.secondary_role in [nil, ""]), + "Assistants" => Enum.filter(users, & &1.role == "assistant" and &1.secondary_role in [nil, ""]) + } + + render(conn, "index.html", categories: categories) + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 073ad65f..86f70353 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -130,6 +130,7 @@ defmodule PhilomenaWeb.Router do resources "/adverts", AdvertController, only: [:show] resources "/pages", PageController, only: [:show] resources "/dnp", DnpEntryController, only: [:index, :show] + resources "/staff", StaffController, only: [:index] get "/:id", ImageController, :show # get "/:forum_id", ForumController, :show # impossible to do without constraints diff --git a/lib/philomena_web/templates/staff/index.html.slime b/lib/philomena_web/templates/staff/index.html.slime new file mode 100644 index 00000000..cb276007 --- /dev/null +++ b/lib/philomena_web/templates/staff/index.html.slime @@ -0,0 +1,12 @@ +h1 Staff +.staff-block + = for {header, users} <- @categories do + + = if Enum.any?(users) do + h4 = header + + = for user <- users do + a.profile-block href=Routes.profile_path(@conn, :show, user) + = render PhilomenaWeb.UserAttributionView, "_user_avatar.html", object: %{user: user}, class: "avatar--125px" + b + => user.name \ No newline at end of file diff --git a/lib/philomena_web/views/staff_view.ex b/lib/philomena_web/views/staff_view.ex new file mode 100644 index 00000000..65611ce8 --- /dev/null +++ b/lib/philomena_web/views/staff_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.StaffView do + use PhilomenaWeb, :view +end diff --git a/test/philomena_web/controllers/staff_controller_test.exs b/test/philomena_web/controllers/staff_controller_test.exs new file mode 100644 index 00000000..fbdfeeb2 --- /dev/null +++ b/test/philomena_web/controllers/staff_controller_test.exs @@ -0,0 +1,88 @@ +defmodule PhilomenaWeb.StaffControllerTest do + use PhilomenaWeb.ConnCase + + alias Philomena.Staffs + + @create_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def fixture(:staff) do + {:ok, staff} = Staffs.create_staff(@create_attrs) + staff + end + + describe "index" do + test "lists all staffs", %{conn: conn} do + conn = get(conn, Routes.staff_path(conn, :index)) + assert html_response(conn, 200) =~ "Listing Staffs" + end + end + + describe "new staff" do + test "renders form", %{conn: conn} do + conn = get(conn, Routes.staff_path(conn, :new)) + assert html_response(conn, 200) =~ "New Staff" + end + end + + describe "create staff" do + test "redirects to show when data is valid", %{conn: conn} do + conn = post(conn, Routes.staff_path(conn, :create), staff: @create_attrs) + + assert %{id: id} = redirected_params(conn) + assert redirected_to(conn) == Routes.staff_path(conn, :show, id) + + conn = get(conn, Routes.staff_path(conn, :show, id)) + assert html_response(conn, 200) =~ "Show Staff" + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = post(conn, Routes.staff_path(conn, :create), staff: @invalid_attrs) + assert html_response(conn, 200) =~ "New Staff" + end + end + + describe "edit staff" do + setup [:create_staff] + + test "renders form for editing chosen staff", %{conn: conn, staff: staff} do + conn = get(conn, Routes.staff_path(conn, :edit, staff)) + assert html_response(conn, 200) =~ "Edit Staff" + end + end + + describe "update staff" do + setup [:create_staff] + + test "redirects when data is valid", %{conn: conn, staff: staff} do + conn = put(conn, Routes.staff_path(conn, :update, staff), staff: @update_attrs) + assert redirected_to(conn) == Routes.staff_path(conn, :show, staff) + + conn = get(conn, Routes.staff_path(conn, :show, staff)) + assert html_response(conn, 200) + end + + test "renders errors when data is invalid", %{conn: conn, staff: staff} do + conn = put(conn, Routes.staff_path(conn, :update, staff), staff: @invalid_attrs) + assert html_response(conn, 200) =~ "Edit Staff" + end + end + + describe "delete staff" do + setup [:create_staff] + + test "deletes chosen staff", %{conn: conn, staff: staff} do + conn = delete(conn, Routes.staff_path(conn, :delete, staff)) + assert redirected_to(conn) == Routes.staff_path(conn, :index) + assert_error_sent 404, fn -> + get(conn, Routes.staff_path(conn, :show, staff)) + end + end + end + + defp create_staff(_) do + staff = fixture(:staff) + {:ok, staff: staff} + end +end