Skip to content

Commit 7a04834

Browse files
committed
Merge Groups into Course
1 parent dc3c134 commit 7a04834

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed

lib/cadet/course/course.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ defmodule Cadet.Course do
77

88
import Ecto.Query
99

10-
alias Cadet.Accounts.User
10+
alias Cadet.{Accounts, Accounts.User}
1111
alias Cadet.Course.{Category, Group, Material, MaterialUpload, Sourcecast, SourcecastUpload}
1212

1313
@upload_file_roles ~w(admin staff)a
14+
@get_overviews_role ~w(staff admin)a
1415

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

53+
@doc """
54+
Returns a list of groups containing information on the each group's id, avenger name and group name
55+
"""
56+
@type group_overview :: %{id: integer, avenger_name: String.t(), name: String.t()}
57+
58+
@spec get_group_overviews(%User{}) :: [group_overview]
59+
def get_group_overviews(_user = %User{role: role}) do
60+
if role in @get_overviews_role do
61+
overviews =
62+
Group
63+
|> Repo.all()
64+
|> Enum.map(fn group_info -> get_group_info(group_info) end)
65+
66+
{:ok, overviews}
67+
else
68+
{:error, {:unauthorized, "Unauthorized"}}
69+
end
70+
end
71+
72+
defp get_group_info(group_info) do
73+
%{
74+
id: group_info.id,
75+
avenger_name: Accounts.get_user(group_info.leader_id).name,
76+
name: group_info.name
77+
}
78+
end
79+
5280
# @doc """
5381
# Reassign a student to a discussion group
5482
# This will un-assign student from the current discussion group

lib/cadet/course/groups.ex

Lines changed: 0 additions & 37 deletions
This file was deleted.

lib/cadet_web/controllers/group_controller.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ defmodule CadetWeb.GroupController do
33

44
use PhoenixSwagger
55

6-
alias Cadet.Course.Groups
6+
alias Cadet.Course
77

88
def index(conn, _) do
99
user = conn.assigns.current_user
10-
result = Groups.get_group_overviews(user)
10+
result = Course.get_group_overviews(user)
1111

1212
case result do
1313
{:ok, groups} ->

test/cadet/course/groups_test.exs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
defmodule Cadet.Course.GroupsTest do
22
use Cadet.DataCase
33

4-
alias Cadet.Accounts
5-
alias Cadet.Accounts.User
6-
alias Cadet.Course.Groups
4+
alias Cadet.{Accounts, Accounts.User}
5+
alias Cadet.Course
76

87
test "get group overviews" do
98
group = insert(:group)
10-
{:ok, result} = Groups.get_group_overviews(%User{role: :staff})
9+
{:ok, result} = Course.get_group_overviews(%User{role: :staff})
1110
avenger_name = Accounts.get_user(group.leader_id).name
1211
group_name = group.name
1312
group_id = group.id

0 commit comments

Comments
 (0)