Skip to content

Commit 0a4e7cc

Browse files
2 parents 456f19a + 83ae6e3 commit 0a4e7cc

6 files changed

Lines changed: 47 additions & 8 deletions

File tree

ndb_rest_api/lib/ndb_rest_api/genders.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,12 @@ defmodule NdbRestApi.Genders do
101101
def change_gender(%Gender{} = gender, attrs \\ %{}) do
102102
Gender.changeset(gender, attrs)
103103
end
104+
105+
@doc """
106+
Returns a gender given its name
107+
"""
108+
def get_by_name!(name) do
109+
Gender
110+
|> Repo.get_by!(name: name)
111+
end
104112
end

ndb_rest_api/lib/ndb_rest_api/patients/patient.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ defmodule NdbRestApi.Patients.Patient do
2020
patient
2121
|> cast(attrs, [:firstname, :lastname, :cf, :gender_id, :date_of_birth])
2222
|> validate_required([:firstname, :lastname, :cf, :gender_id, :date_of_birth])
23+
|> unsafe_validate_unique(:cf, NdbRestApi.Repo)
24+
|> unique_constraint(:cf)
2325
end
2426
end

ndb_rest_api/lib/ndb_rest_api_web/controllers/api/condition_controller.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ defmodule NdbRestApiWeb.Api.ConditionController do
1717
end
1818

1919
def create(conn, %{"condition" => condition_params}) do
20-
with {:ok, %Condition{} = condition} <- Conditions.create_condition(condition_params) do
20+
full_condition = condition_params
21+
|> Map.put("patient_id", condition_params |> Map.fetch!("patient") |> Map.fetch!("id"))
22+
|> Map.put("practitioner_id", condition_params |> Map.fetch!("practitioner") |> Map.fetch!("id"))
23+
24+
with {:ok, %Condition{} = condition} <- Conditions.create_condition(full_condition) do
2125
conn
2226
|> put_status(:created)
2327
|> put_resp_header("location", ~p"/api/conditions/#{condition}")
24-
|> render(:show, condition: condition)
28+
|> render(:show, condition: condition
29+
|> Repo.preload([:patient, :practitioner])
30+
)
2531
end
2632
end
2733

ndb_rest_api/lib/ndb_rest_api_web/controllers/api/medication_request_controller.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ defmodule NdbRestApiWeb.Api.MedicationRequestController do
1717
end
1818

1919
def create(conn, %{"medication_request" => medication_request_params}) do
20+
full_medication_request = medication_request_params
21+
|> Map.put("patient_id", medication_request_params |> Map.fetch!("patient") |> Map.fetch!("id"))
22+
|> Map.put("practitioner_id", medication_request_params |> Map.fetch!("practitioner") |> Map.fetch!("id"))
23+
2024
with {:ok, %MedicationRequest{} = medication_request} <-
21-
MedicationRequests.create_medication_request(medication_request_params) do
25+
MedicationRequests.create_medication_request(full_medication_request) do
2226
conn
2327
|> put_status(:created)
2428
|> put_resp_header("location", ~p"/api/medication_requests/#{medication_request}")
25-
|> render(:show, medication_request: medication_request)
29+
|> render(:show, medication_request: medication_request
30+
|> Repo.preload([:patient, :practitioner])
31+
)
2632
end
2733
end
2834

ndb_rest_api/lib/ndb_rest_api_web/controllers/api/observation_controller.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ defmodule NdbRestApiWeb.Api.ObservationController do
1717
end
1818

1919
def create(conn, %{"observation" => observation_params}) do
20+
full_observation = observation_params
21+
|> Map.put("patient_id", observation_params |> Map.fetch!("patient") |> Map.fetch!("id"))
22+
|> Map.put("practitioner_id", observation_params |> Map.fetch!("practitioner") |> Map.fetch!("id"))
23+
2024
with {:ok, %Observation{} = observation} <-
21-
Observations.create_observation(observation_params) do
25+
Observations.create_observation(full_observation) do
2226
conn
2327
|> put_status(:created)
2428
|> put_resp_header("location", ~p"/api/observations/#{observation}")
25-
|> render(:show, observation: observation)
29+
|> render(:show, observation: observation
30+
|> Repo.preload([:patient, :practitioner])
31+
)
2632
end
2733
end
2834

ndb_rest_api/lib/ndb_rest_api_web/controllers/api/patient_controller.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@ defmodule NdbRestApiWeb.Api.PatientController do
1616
end
1717

1818
def create(conn, %{"patient" => patient_params}) do
19-
with {:ok, %Patient{} = patient} <- Patients.create_patient(patient_params) do
19+
# Gender is passed as its name, so we need to retrieve the gender id
20+
gender = NdbRestApi.Genders.get_by_name!(Map.fetch!(patient_params, "gender"))
21+
22+
full_patient = patient_params
23+
|> Map.put("gender_id", gender.id)
24+
|> Map.delete("gender")
25+
26+
with {:ok, %Patient{} = patient} <- Patients.create_patient(full_patient)
27+
do
2028
conn
2129
|> put_status(:created)
2230
|> put_resp_header("location", ~p"/api/patients/#{patient}")
23-
|> render(:show, patient: patient)
31+
|> render(:show, patient:
32+
patient
33+
|> Repo.preload([:gender, :medication_requests, :observations, :conditions])
34+
)
2435
end
2536
end
2637

0 commit comments

Comments
 (0)