@@ -3,42 +3,49 @@ defmodule NdbRestApiWeb.HospitalController do
33
44 alias NdbRestApi.Hospitals
55 alias NdbRestApi.Hospitals.Hospital
6+ alias NdbRestApi.Practitioners
7+ alias NdbRestApi.Repo
68
79 def index ( conn , _params ) do
8- hospitals = Hospitals . list_hospitals ( )
10+ hospitals = Hospitals . list_hospitals ( ) |> Repo . preload ( :practitioners )
911 render ( conn , :index , hospitals: hospitals )
1012 end
1113
1214 def new ( conn , _params ) do
13- changeset = Hospitals . change_hospital ( % Hospital { } )
14- render ( conn , :new , changeset: changeset )
15+ changeset = Hospitals . change_hospital ( % Hospital { practitioners: [ ] } )
16+ practitioners = Practitioners . list_practitioners ( )
17+ render ( conn , :new , changeset: changeset , practitioners: practitioners )
1518 end
1619
1720 def create ( conn , % { "hospital" => hospital_params } ) do
1821 case Hospitals . create_hospital ( hospital_params ) do
1922 { :ok , hospital } ->
23+ hospital = Repo . preload ( hospital , :practitioners )
24+
2025 conn
2126 |> put_flash ( :info , "Hospital created successfully." )
2227 |> redirect ( to: ~p" /hospitals/#{ hospital } " )
2328
2429 { :error , % Ecto.Changeset { } = changeset } ->
25- render ( conn , :new , changeset: changeset )
30+ practitioners = Practitioners . list_practitioners ( )
31+ render ( conn , :new , changeset: changeset , practitioners: practitioners )
2632 end
2733 end
2834
2935 def show ( conn , % { "id" => id } ) do
30- hospital = Hospitals . get_hospital! ( id )
36+ hospital = Hospitals . get_hospital! ( id ) |> Repo . preload ( :practitioners )
3137 render ( conn , :show , hospital: hospital )
3238 end
3339
3440 def edit ( conn , % { "id" => id } ) do
35- hospital = Hospitals . get_hospital! ( id )
41+ hospital = Hospitals . get_hospital! ( id ) |> Repo . preload ( :practitioners )
3642 changeset = Hospitals . change_hospital ( hospital )
37- render ( conn , :edit , hospital: hospital , changeset: changeset )
43+ practitioners = Practitioners . list_practitioners ( )
44+ render ( conn , :edit , hospital: hospital , changeset: changeset , practitioners: practitioners )
3845 end
3946
4047 def update ( conn , % { "id" => id , "hospital" => hospital_params } ) do
41- hospital = Hospitals . get_hospital! ( id )
48+ hospital = Hospitals . get_hospital! ( id ) |> Repo . preload ( :practitioners )
4249
4350 case Hospitals . update_hospital ( hospital , hospital_params ) do
4451 { :ok , hospital } ->
@@ -47,12 +54,18 @@ defmodule NdbRestApiWeb.HospitalController do
4754 |> redirect ( to: ~p" /hospitals/#{ hospital } " )
4855
4956 { :error , % Ecto.Changeset { } = changeset } ->
50- render ( conn , :edit , hospital: hospital , changeset: changeset )
57+ practitioners = Practitioners . list_practitioners ( )
58+
59+ render ( conn , :edit ,
60+ hospital: hospital ,
61+ changeset: changeset ,
62+ practitioners: practitioners
63+ )
5164 end
5265 end
5366
5467 def delete ( conn , % { "id" => id } ) do
55- hospital = Hospitals . get_hospital! ( id )
68+ hospital = Hospitals . get_hospital! ( id ) |> Repo . preload ( :practitioners )
5669 { :ok , _hospital } = Hospitals . delete_hospital ( hospital )
5770
5871 conn
0 commit comments