Skip to content

CP3108 Staff Dashboard - Backend Modifications for Avengers Leaderboard #582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion lib/cadet/course/course.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ defmodule Cadet.Course do

import Ecto.Query

alias Cadet.Accounts.User
alias Cadet.{Accounts, Accounts.User}
alias Cadet.Course.{Category, Group, Material, MaterialUpload, Sourcecast, SourcecastUpload}

@upload_file_roles ~w(admin staff)a
@get_overviews_role ~w(staff admin)a

@doc """
Get a group based on the group name or create one if it doesn't exist
Expand Down Expand Up @@ -49,6 +50,33 @@ defmodule Cadet.Course do
|> Repo.insert_or_update()
end

@doc """
Returns a list of groups containing information on the each group's id, avenger name and group name
"""
@type group_overview :: %{id: integer, avenger_name: String.t(), name: String.t()}

@spec get_group_overviews(%User{}) :: [group_overview]
def get_group_overviews(_user = %User{role: role}) do
if role in @get_overviews_role do
overviews =
Group
|> Repo.all()
|> Enum.map(fn group_info -> get_group_info(group_info) end)

{:ok, overviews}
else
{:error, {:unauthorized, "Unauthorized"}}
end
end

defp get_group_info(group_info) do
%{
id: group_info.id,
avenger_name: Accounts.get_user(group_info.leader_id).name,
name: group_info.name
}
end

# @doc """
# Reassign a student to a discussion group
# This will un-assign student from the current discussion group
Expand Down
54 changes: 54 additions & 0 deletions lib/cadet_web/controllers/group_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
defmodule CadetWeb.GroupController do
use CadetWeb, :controller

use PhoenixSwagger

alias Cadet.Course

def index(conn, _) do
user = conn.assigns.current_user
result = Course.get_group_overviews(user)

case result do
{:ok, groups} ->
render(conn, "index.json", groups: groups)

{:error, {status, message}} ->
conn
|> put_status(status)
|> text(message)
end
end

swagger_path :index do
get("/groups")

summary("Get a list of all the groups")

security([%{JWT: []}])

produces("application/json")

response(200, "OK", Schema.ref(:GroupsList))
response(401, "Unauthorised")
end

def swagger_definitions do
%{
GroupsList:
swagger_schema do
description("A list of all groups")
type(:array)
items(Schema.ref(:GroupOverview))
end,
GroupOverview:
swagger_schema do
properties do
id(:integer, "The group id", required: true)
avengerName(:string, "The name of the group's avenger", required: true)
groupName(:string, "The name of the group", required: true)
end
end
}
end
end
2 changes: 2 additions & 0 deletions lib/cadet_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ defmodule CadetWeb.Router do
post("/grading/:submissionid/unsubmit", GradingController, :unsubmit)
post("/grading/:submissionid/:questionid", GradingController, :update)

get("/groups", GroupController, :index)

get("/notification", NotificationController, :index)
post("/notification/acknowledge", NotificationController, :acknowledge)

Expand Down
16 changes: 16 additions & 0 deletions lib/cadet_web/views/group_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule CadetWeb.GroupView do
use CadetWeb, :view
use Timex

def render("index.json", %{groups: groups}) do
render_many(groups, CadetWeb.GroupView, "overview.json", as: :group)
end

def render("overview.json", %{group: group}) do
transform_map_for_view(group, %{
id: :id,
groupName: :name,
avengerName: :avenger_name
})
end
end
15 changes: 15 additions & 0 deletions test/cadet/course/groups_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Cadet.Course.GroupsTest do
use Cadet.DataCase

alias Cadet.{Accounts, Accounts.User}
alias Cadet.Course

test "get group overviews" do
group = insert(:group)
{:ok, result} = Course.get_group_overviews(%User{role: :staff})
avenger_name = Accounts.get_user(group.leader_id).name
group_name = group.name
group_id = group.id
assert result == [%{id: group_id, avenger_name: avenger_name, name: group_name}]
end
end
34 changes: 34 additions & 0 deletions test/cadet_web/controllers/group_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule CadetWeb.GroupsControllerTest do
use CadetWeb.ConnCase

describe "GET /, unauthenticated" do
test "unauthorized", %{conn: conn} do
conn = get(conn, build_url())
assert response(conn, 401) =~ "Unauthorised"
end
end

describe "GET /, student only" do
@tag authenticate: :student
test "unauthorized", %{conn: conn} do
conn = get(conn, build_url())
assert response(conn, 401) == "Unauthorized"
end
end

describe "GET /, staff" do
@tag authenticate: :staff
test "successful", %{conn: conn} do
avenger = insert(:user, %{name: "avenger", role: :staff})
mentor = insert(:user, %{name: "mentor", role: :staff})
group = insert(:group, %{leader: avenger, mentor: mentor})
conn = get(conn, build_url())
group_name = group.name
group_id = group.id
expected = [%{"id" => group_id, "avengerName" => "avenger", "groupName" => group_name}]
assert json_response(conn, 200) == expected
end
end

defp build_url, do: "/v1/groups/"
end