Skip to content
This repository has been archived by the owner on Apr 30, 2018. It is now read-only.

Commit

Permalink
Refactor medical records routing
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbarclay committed Mar 8, 2017
1 parent df498e6 commit 2575cec
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
10 changes: 4 additions & 6 deletions app/controllers/medical_records_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class MedicalRecordsController < ApplicationController
def create

@medical_record = MedicalRecord.new(medical_record_params)

if @medical_record.save
render status: 201, json: { success: true }
else
Expand All @@ -11,8 +10,7 @@ def create
end

def show
@medical_record = MedicalRecord.find_by(id: params[:medical_record_id])

@medical_record = MedicalRecord.find_by(id: params[:id])
if @medical_record
render json: { success: true, medical_record: @medical_record }
else
Expand All @@ -21,8 +19,8 @@ def show
end

def index
@medical_records = MedicalRecord.all
filtered_records = filter_medical_records_keys @medical_records
@patient = Patient.find_by(id: params[:patient_id])
filtered_records = filter_medical_records_keys patient.medical_records

render status: :success, json: { success: true, medical_records: filtered_records }
end
Expand All @@ -45,7 +43,7 @@ def medical_record_params
:earsAS, :glandsN, :glandsA, :skinN, :skinA, :abdomenN,
:abdomenA, :urogentialN, :urogentialA, :nervousSystemN, :nervousSystemA,
:musculoskeletalN, :musculoskeletalA, :cardiovascularN, :cardiovascularA, :respiratoryN,
:respiratoryA)
:respiratoryA, :patient_id)
end
end

Expand Down
3 changes: 3 additions & 0 deletions app/models/medical_record.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class MedicalRecord < ApplicationRecord
belongs_to :patient

validates :patient_id, presence: true, allow_bank: false

validates :temperature, presence: true, allow_blank: true, numericality: true

validates :notes, presence: true, allow_blank: true
Expand Down
10 changes: 6 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
post 'user_token', to: 'user_token#create'
post 'login', to: 'user_token#create'

post 'patients/:patients_id/medical_records/:medical_records_id', to: 'medical_record#create'
get 'patients/:patients_id/medical_records', to: 'medical_record#index'

# post 'patient/:patient_id/medical_records/:medical_record_id', to: 'medical_records#create'
# get 'patients/:patient_id/medical_records', to: 'medical_record#index'
# get 'patients/:patient_id/medical_records/:medical_record_id', to: 'medical_records#show'
resources :users
resources :patients
resources :patients do
resources :medical_records
end
resources :client
resources :contacts
end
Expand Down
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170307205911) do
ActiveRecord::Schema.define(version: 20170308192748) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -99,6 +99,7 @@
t.boolean "respiratoryA"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "patient_id", null: false
end

create_table "patients", force: :cascade do |t|
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/medical_records.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# below each fixture, per the syntax in the comments below
#
one:
patient: one
temperature: 38.5
notes: No new notes
medications: This might need to be removed
Expand Down Expand Up @@ -52,5 +53,3 @@ one:
cardiovascularA: True
respiratoryN: True
respiratoryA: True
two: {}
# column: value True
16 changes: 8 additions & 8 deletions test/integration/medical_records_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class MedicalRecordsTest < ActionDispatch::IntegrationTest
# assert true
# end
def setup
@medical_record = {
@patient_id = patients(:one).id.to_s
@medical_record ={
patient_id: @patient_id,
temperature: 38.5,
notes: 'No new notes',
medications: 'This might need to be removed',
Expand Down Expand Up @@ -55,25 +57,23 @@ def setup
respiratoryN: true,
respiratoryA: true
}

@show_record = medical_records(:one)
@patient_id = patients_path(:one).id.to_s

unless @show_record.save
throw Error
end
end

test 'Posting a valid medical record' do
post '/api/medical_records', headers: authenticated_header, params: { medical_record: @medical_record }
post "/api/patients/#{@patient_id}/medical_records", headers: authenticated_header, params: { medical_record: @medical_record }

assert JSON.parse(response.body)['success']
assert_response :created
end

test 'Posting invalid medical record fails' do
@medical_record['heart_rate'] = 'blue'
post '/api/medical_records', headers: authenticated_header, params: { medical_record: @medical_record }
post "/api/patients/#{@patient_id}/medical_records", headers: authenticated_header, params: { medical_record: @medical_record }

assert_response :error
assert_not JSON.parse(response.body)['success']
Expand All @@ -83,16 +83,16 @@ def setup
test 'Get medical_record' do
good_id = @show_record.id.to_s

get '/api/medical_records/' + good_id, headers: authenticated_header
puts response.body['medical_record']
get "/api/patients/#{@patient_id}/medical_records/#{good_id}", headers: authenticated_header

assert_response :success
assert JSON.parse(response.body)['medical_record']['id'].to_s == good_id
end

test 'Get invalid medical_record' do
bad_id = @show_record.id + 1

get '/api/medical_records/' + bad_id.to_s, headers: authenticated_header
get "/api/patients/#{@patient_id}/medical_records/#{bad_id}", headers: authenticated_header

assert_response :error
assert_not JSON.parse(response.body)['success']
Expand Down

0 comments on commit 2575cec

Please sign in to comment.