-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.rb
More file actions
142 lines (119 loc) · 3.41 KB
/
Copy pathapi.rb
File metadata and controls
142 lines (119 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# author: Thato Semoko
# REST API
require 'sinatra'
require 'sinatra/namespace'
require 'mongoid'
require './models/patient'
require './models/department'
require './models/aggs'
require './models/health_record'
require './helpers/api_helper'
require './helpers/aggregator'
# DB Setup
Mongoid.load!(File.join(File.dirname(__FILE__), 'config', 'mongoid.yml'))
set :public_folder, __dir__ + '/static'
before do
headers "Access-Control-Allow-Headers" => "origin, x-requested-with, content-type"
end
# Endpoints
get '/' do
@departments = Department.all
if params['department']
@records = Department.where(name: "#{params['department']}").first.health_records
elsif params['id_number']
@records = HealthRecord.where(id_number: "#{params['id_number']}")
@patient = Patient.where(id_number: "#{params['id_number']}").first
else
@records = HealthRecord.all
end
erb :index, locals: { records: @records, departments: @departments, patient: @patient }
end
get '/insights' do
@ag = Aggregator::Consolidate.new
if params['role']
@aggs = Agg.where(role: "#{params['role']}").all
else
@aggs = Agg.all
end
erb :insights, locals: { roles: @ag.roles, records: @aggs, patient: @patient }
end
namespace '/api/v1' do
# respond_to :json
before do
content_type 'application/json'
end
# GET /patients
get '/patients' do
Patient.all.to_json
end
# GET /patients/:id
get '/patients/:id' do |id|
patient = Patient.where(id: id).first
halt(404, { message: 'Patient not found'}.to_json) unless patient
patient.to_json
status 302
end
# POST /patients
post '/patients' do
patient = Patient.new(patient_params)
if patient.save
response.headers['Location'] = "#{base_url}/api/v1/patients/#{patient.id}"
status 201 # created
else
status 422 # unprocessable entity
end
end
# PUT /patients
put '/patients/:id' do |id|
patient = Patient.where(id: id).first
halt(404, { message: 'Patient not found'}.to_json) unless patient
if patient.update_attributes(patient_params)
status 204
else
status 422 # unprocessable entity
end
end
# DELETE /patients
delete 'patients/:id' do |id|
patient = Patient.where(id: id).first
patient.destroy if patient
status 204
end
# GET /health_records
get '/health_records' do
HealthRecord.all.to_json
end
# GET /health_records/:id
get '/health_records/:id' do |id|
health_record = HealthRecord.where(id: id).first
halt(404, { message: 'Patient not found'}.to_json) unless health_record
health_record.to_json
status 302
end
# POST /health_records
post '/health_records' do
health_record = HealthRecord.new(record_params)
if health_record.save
response.headers['Location'] = "#{base_url}/api/v1/health_records/#{health_record.id}"
status 201 # created
else
status 422 # unprocessable entity
end
end
# PUT /health_records
put '/health_records/:id' do |id|
health_record = HealthRecord.where(id: id).first
halt(404, { message: 'health_record not found'}.to_json) unless health_record
if health_record.update_attributes(record_params)
status 204
else
status 422 # unprocessable entity
end
end
# DELETE /health_records
delete 'health_records/:id' do |id|
health_record = HealthRecord.where(id: id).first
health_record.destroy if health_record
status 204
end
end