@@ -8,38 +8,55 @@ defmodule CodeCorps.OrganizationMembershipController do
88
99 plug :scrub_params , "data" when action in [ :create , :update ]
1010
11- def index ( conn , % { "filter" => % { "id" => id_list } } ) do
12- ids = id_list |> coalesce_id_string
11+ def index ( conn , params ) do
1312 memberships =
14- OrganizationMembership
15- |> preload ( [ :organization , :member ] )
16- |> where ( [ p ] , p . id in ^ ids )
17- |> Repo . all
13+ case params do
14+ % { "organization_id" => organization_id , "role" => roles , "filter" => id_list } ->
15+ roles = roles |> coalesce_string
16+ ids = id_list |> coalesce_id_string
17+ OrganizationMembership
18+ |> where ( [ om ] , om . organization_id == ^ organization_id )
19+ |> where ( [ om ] , om . member_id in ^ ids )
20+ |> where ( [ om ] , om . role in ^ roles )
21+ % { "organization_id" => organization_id , "role" => roles } ->
22+ roles = roles |> coalesce_string
23+ OrganizationMembership
24+ |> where ( [ om ] , om . organization_id == ^ organization_id )
25+ |> where ( [ om ] , om . role in ^ roles )
26+ % { "organization_id" => organization_id , "filter" => id_list } ->
27+ id_list = id_list |> coalesce_id_string
28+ OrganizationMembership
29+ |> where ( [ om ] , om . organization_id == ^ organization_id )
30+ |> where ( [ om ] , om . id in ^ id_list )
31+ % { "role" => roles , "filter" => id_list } ->
32+ roles = roles |> coalesce_string
33+ ids = id_list |> coalesce_id_string
34+ OrganizationMembership
35+ |> where ( [ om ] , om . member_id in ^ ids )
36+ |> where ( [ om ] , om . role in ^ roles )
37+ % { "organization_id" => organization_id } ->
38+ OrganizationMembership
39+ |> where ( [ om ] , om . organization_id == ^ organization_id )
40+ % { "filter" => id_list } ->
41+ ids = id_list |> coalesce_id_string
42+ OrganizationMembership
43+ |> where ( [ om ] , om . member_id in ^ ids )
44+ % { "role" => roles } ->
45+ roles = roles |> coalesce_string
46+ OrganizationMembership
47+ |> where ( [ om ] , om . role in ^ roles )
48+ _ ->
49+ OrganizationMembership
50+ end
1851
19- render ( conn , "index.json-api" , data: memberships )
20- end
21-
22- def index ( conn , % { "role" => roles } ) do
23- roles = roles |> coalesce_string
2452 memberships =
25- OrganizationMembership
53+ memberships
2654 |> preload ( [ :organization , :member ] )
27- |> where ( [ p ] , p . role in ^ roles )
2855 |> Repo . all
2956
3057 render ( conn , "index.json-api" , data: memberships )
3158 end
3259
33- def index ( conn , % { "organization_id" => organization_id } ) do
34- memberships =
35- OrganizationMembership
36- |> preload ( [ :organization , :member ] )
37- |> where ( [ m ] , m . organization_id == ^ organization_id )
38- |> Repo . all
39-
40- render ( conn , "index.json-api" , data: memberships )
41- end
42-
4360 def show ( conn , % { "id" => id } ) do
4461 membership =
4562 OrganizationMembership
0 commit comments