File tree Expand file tree Collapse file tree 2 files changed +65
-7
lines changed Expand file tree Collapse file tree 2 files changed +65
-7
lines changed Original file line number Diff line number Diff line change 11defmodule Cadet.Course.Groups do
22 use Cadet , [ :context , :display ]
33
4+ import Cadet.Accounts
5+
6+ alias Cadet.Accounts.User
47 alias Cadet.Course.Group
5- import Cadet.Accounts
68
7- def get_group_overviews ( ) do
8- Group
9- |> Repo . all ( )
10- |> Enum . map ( fn group_info -> get_group_info ( group_info ) end )
9+ @ get_overviews_role ~w( staff admin) a
10+
11+ @ doc """
12+ Returns a list of groups containing information on the each group's id, avenger name and group name
13+ """
14+ @ type group_overview :: % { id: integer , avenger_name: String . t , name: String . t }
15+
16+ @ spec get_group_overviews ( % User { } ) :: [ group_overview ]
17+ def get_group_overviews ( _user = % User { role: role } ) do
18+ if role in @ get_overviews_role do
19+ overviews =
20+ Group
21+ |> Repo . all ( )
22+ |> Enum . map ( fn group_info -> get_group_info ( group_info ) end )
23+ { :ok , overviews }
24+ else
25+ { :error , { :unauthorised , "Unauthorised" } }
26+ end
1127 end
1228
1329 defp get_group_info ( group_info ) do
Original file line number Diff line number Diff line change @@ -6,8 +6,50 @@ defmodule CadetWeb.GroupController do
66 alias Cadet.Course.Groups
77
88 def index ( conn , _ ) do
9- groups = Groups . get_group_overviews ( )
9+ user = conn . assigns . current_user
10+ result = Groups . get_group_overviews ( user )
1011
11- render ( conn , "index.json" , groups: groups )
12+ case result do
13+ { :ok , groups } ->
14+ render ( conn , "index.json" , groups: groups )
15+
16+ { :error , { status , message } } ->
17+ conn
18+ |> put_status ( status )
19+ |> text ( message )
20+ end
21+
22+ end
23+
24+ swagger_path :index do
25+ get ( "/groups" )
26+
27+ summary ( "Get a list of all the groups" )
28+
29+ security ( [ % { JWT: [ ] } ] )
30+
31+ produces ( "application/json" )
32+
33+ response ( 200 , "OK" , Schema . ref ( :GroupsList ) )
34+ response ( 401 , "Unauthorised" )
35+ end
36+
37+ def swagger_definitions do
38+ % {
39+ GroupsList:
40+ swagger_schema do
41+ description ( "A list of all groups" )
42+ type ( :array )
43+ items ( Schema . ref ( :GroupOverview ) )
44+ end ,
45+ GroupOverview:
46+ swagger_schema do
47+ properties do
48+ id ( :integer , "The group id" , required: true )
49+ avengerName ( :string , "The name of the group's avenger" , required: true )
50+ groupName ( :string , "The name of the group" , required: true )
51+ end
52+ end
53+ }
1254 end
1355end
You can’t perform that action at this time.
0 commit comments