-
Notifications
You must be signed in to change notification settings - Fork 166
/
FhirController.cs
241 lines (205 loc) · 9.28 KB
/
FhirController.cs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Spark.Engine;
using Spark.Engine.Core;
using Spark.Engine.Extensions;
using Spark.Engine.Service;
using Spark.Engine.Utility;
namespace Spark.Web.Controllers
{
[Route("fhir"), ApiController, EnableCors]
public class FhirController : ControllerBase
{
private readonly IFhirService _fhirService;
private readonly SparkSettings _settings;
public FhirController(IFhirService fhirService, SparkSettings settings)
{
_fhirService = fhirService ?? throw new ArgumentNullException(nameof(fhirService));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
}
[HttpGet("{type}/{id}")]
public async Task<ActionResult<FhirResponse>> Read(string type, string id)
{
ConditionalHeaderParameters parameters = new ConditionalHeaderParameters(Request);
Key key = Key.Create(type, id);
var response = await _fhirService.ReadAsync(key, parameters).ConfigureAwait(false);
return new ActionResult<FhirResponse>(response);
}
[HttpGet("{type}/{id}/_history/{vid}")]
public async Task<FhirResponse> VRead(string type, string id, string vid)
{
Key key = Key.Create(type, id, vid);
return await _fhirService.VersionReadAsync(key).ConfigureAwait(false);
}
[HttpPut("{type}/{id?}")]
public async Task<ActionResult<FhirResponse>> Update(string type, Resource resource, string id = null)
{
string versionId = Request.GetTypedHeaders().IfMatch?.FirstOrDefault()?.Tag.Buffer;
Key key = Key.Create(type, id, versionId);
if (key.HasResourceId())
{
Request.TransferResourceIdIfRawBinary(resource, id);
return new ActionResult<FhirResponse>(await _fhirService.UpdateAsync(key, resource).ConfigureAwait(false));
}
else
{
return new ActionResult<FhirResponse>(await _fhirService.ConditionalUpdateAsync(key, resource,
SearchParams.FromUriParamList(Request.TupledParameters())).ConfigureAwait(false));
}
}
[HttpPost("{type}")]
public async Task<FhirResponse> Create(string type, Resource resource)
{
Key key = Key.Create(type, resource?.Id);
if (Request.Headers.ContainsKey(FhirHttpHeaders.IfNoneExist))
{
NameValueCollection searchQueryString = HttpUtility.ParseQueryString(Request.GetTypedHeaders().IfNoneExist());
IEnumerable<Tuple<string, string>> searchValues =
searchQueryString.Keys.Cast<string>()
.Select(k => new Tuple<string, string>(k, searchQueryString[k]));
return await _fhirService.ConditionalCreateAsync(key, resource, SearchParams.FromUriParamList(searchValues)).ConfigureAwait(false);
}
return await _fhirService.CreateAsync(key, resource).ConfigureAwait(false);
}
[HttpPatch("{type}/{id}")]
public async Task<FhirResponse> Patch(string type, string id, Parameters patch)
{
// TODO: conditional PATCH support (http://www.hl7.org/fhir/R4/http.html#concurrency)
var key = Key.Create(type, id, Request.IfMatchVersionId());
return await _fhirService.PatchAsync(key, patch).ConfigureAwait(false);
}
[HttpDelete("{type}/{id}")]
public async Task<FhirResponse> Delete(string type, string id)
{
Key key = Key.Create(type, id);
FhirResponse response = await _fhirService.DeleteAsync(key).ConfigureAwait(false);
return response;
}
[HttpDelete("{type}")]
public async Task<FhirResponse> ConditionalDelete(string type)
{
Key key = Key.Create(type);
return await _fhirService.ConditionalDeleteAsync(key, Request.TupledParameters()).ConfigureAwait(false);
}
[HttpGet("{type}/{id}/_history")]
public async Task<FhirResponse> History(string type, string id)
{
Key key = Key.Create(type, id);
var parameters = new HistoryParameters(Request);
return await _fhirService.HistoryAsync(key, parameters).ConfigureAwait(false);
}
// ============= Validate
[HttpPost("{type}/{id}/$validate")]
public async Task<FhirResponse> Validate(string type, string id, Resource resource)
{
Key key = Key.Create(type, id);
return await _fhirService.ValidateOperationAsync(key, resource).ConfigureAwait(false);
}
[HttpPost("{type}/$validate")]
public async Task<FhirResponse> Validate(string type, Resource resource)
{
Key key = Key.Create(type);
return await _fhirService.ValidateOperationAsync(key, resource).ConfigureAwait(false);
}
// ============= Type Level Interactions
[HttpGet("{type}")]
public async Task<FhirResponse> Search(string type)
{
var offset = Request.GetPagingOffsetParameter();
var searchparams = Request.GetSearchParams();
return await _fhirService.SearchAsync(type, searchparams, offset).ConfigureAwait(false);
}
[HttpPost("{type}/_search")]
public async Task<FhirResponse> SearchWithOperator(string type)
{
var offset = Request.GetPagingOffsetParameter();
SearchParams searchparams = Request.GetSearchParamsFromBody();
return await _fhirService.SearchAsync(type, searchparams, offset).ConfigureAwait(false);
}
[HttpGet("{type}/_history")]
public async Task<FhirResponse> History(string type)
{
var parameters = new HistoryParameters(Request);
return await _fhirService.HistoryAsync(type, parameters).ConfigureAwait(false);
}
// ============= Whole System Interactions
[HttpGet, Route("metadata")]
public async Task<FhirResponse> Metadata()
{
return await _fhirService.CapabilityStatementAsync(_settings.Version).ConfigureAwait(false);
}
[HttpOptions, Route("")]
public async Task<FhirResponse> Options()
{
return await _fhirService.CapabilityStatementAsync(_settings.Version).ConfigureAwait(false);
}
[HttpPost, Route("")]
public async Task<FhirResponse> Transaction(Bundle bundle)
{
return await _fhirService.TransactionAsync(bundle).ConfigureAwait(false);
}
[HttpGet, Route("_history")]
public async Task<FhirResponse> History()
{
var parameters = new HistoryParameters(Request);
return await _fhirService.HistoryAsync(parameters).ConfigureAwait(false);
}
[HttpGet, Route("_snapshot")]
public async Task<FhirResponse> Snapshot()
{
string snapshot = Request.GetParameter(FhirParameter.SNAPSHOT_ID);
var offset = Request.GetPagingOffsetParameter();
return await _fhirService.GetPageAsync(snapshot, offset).ConfigureAwait(false);
}
// Operations
[HttpPost, Route("${operation}")]
public FhirResponse ServerOperation(string operation)
{
switch (operation.ToLower())
{
case "error": throw new Exception("This error is for testing purposes");
default: return Respond.WithError(HttpStatusCode.NotFound, "Unknown operation");
}
}
[HttpPost, Route("{type}/{id}/${operation}")]
public async Task<FhirResponse> InstanceOperation(string type, string id, string operation, Parameters parameters)
{
Key key = Key.Create(type, id);
switch (operation.ToLower())
{
case "meta": return await _fhirService.ReadMetaAsync(key).ConfigureAwait(false);
case "meta-add": return await _fhirService.AddMetaAsync(key, parameters).ConfigureAwait(false);
case "meta-delete":
default: return Respond.WithError(HttpStatusCode.NotFound, "Unknown operation");
}
}
[HttpPost, HttpGet, Route("{type}/{id}/$everything")]
public async Task<FhirResponse> Everything(string type, string id = null)
{
Key key = Key.Create(type, id);
return await _fhirService.EverythingAsync(key).ConfigureAwait(false);
}
[HttpPost, HttpGet, Route("{type}/$everything")]
public async Task<FhirResponse> Everything(string type)
{
Key key = Key.Create(type);
return await _fhirService.EverythingAsync(key).ConfigureAwait(false);
}
[HttpPost, HttpGet, Route("Composition/{id}/$document")]
public async Task<FhirResponse> Document(string id)
{
Key key = Key.Create("Composition", id);
return await _fhirService.DocumentAsync(key).ConfigureAwait(false);
}
}
}