-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFhirController.cs
More file actions
252 lines (239 loc) · 10.3 KB
/
Copy pathFhirController.cs
File metadata and controls
252 lines (239 loc) · 10.3 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
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
242
243
244
245
246
247
248
249
250
251
252
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using LantanaGroup.Link.Shared.Application.Services.Security;
using LantanaGroup.Link.Terminology.Services;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
namespace LantanaGroup.Link.Terminology.Controllers;
/**
* Controller for FHIR terminology operations. Implements portions of FHIR terminology, as defined in these specifications:
* https://build.fhir.org/valueset-operation-expand.html
* https://build.fhir.org/codesystem-operation-validate-code.html
* https://build.fhir.org/valueset-operation-validate-code.html
* The class uses FhirService to handle all FHIR-related operations, which internally uses CodeGroupCacheService
* to retrieve and validate codes in value sets and code systems, as well as expand value sets.
*/
[Route("api/terminology/fhir")]
[SwaggerTag("FHIR Terminology Operations")]
[ApiController]
public class FhirController(FhirService fhirService) : Controller
{
#region Value Sets
/// <summary>
/// Retrieves a ValueSet resource by its unique identifier.
/// </summary>
/// <param name="id">The unique identifier for the ValueSet resource to retrieve.</param>
/// <returns>
/// An <see cref="ActionResult{T}"/> containing the requested <see cref="ValueSet"/>
/// if it exists, a 400 Bad Request response if the id is null or empty, or a
/// 404 Not Found response if the ValueSet is not found.
/// </returns>
[HttpGet("ValueSet/{id}")]
public ActionResult<ValueSet> GetValueSetById([FromRoute] string id)
{
try
{
var cleanId = id.SanitizeAndRemove();
return Ok(fhirService.GetValueSetById(cleanId));
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
catch (KeyNotFoundException ex)
{
return NotFound(ex.Message);
}
}
/// <summary>
/// Retrieves a collection of ValueSet resources based on the specified query parameters.
/// </summary>
/// <param name="url">The canonical URL of the ValueSet to retrieve, if specified.</param>
/// <param name="summary">
/// An optional parameter indicating if a summary of the ValueSet should be included in the response.
/// </param>
/// <returns>
/// An <see cref="ActionResult{T}"/> containing a <see cref="Bundle"/> with the requested
/// ValueSet resources. Returns a 400 Bad Request response if neither <paramref name="url"/>
/// nor <paramref name="summary"/> are provided.
/// </returns>
[HttpGet("ValueSet")]
public ActionResult<Bundle> GetValueSets([FromQuery] string? url,
[FromQuery(Name = "_summary")] SummaryType? summary)
{
try
{
return Ok(fhirService.GetValueSets(url?.Sanitize(), summary));
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
catch (InvalidOperationException ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
/// <summary>
/// Expands a ValueSet resource by its unique identifier or URL.
/// </summary>
/// <param name="id">The unique identifier of the ValueSet resource to expand. Can be null if URL is provided.</param>
/// <param name="url">The URL of the ValueSet resource to expand. Can be null if id is provided.</param>
/// <param name="date">The date to use when querying the ValueSet resource. Optional.</param>
/// <returns>
/// An <see cref="ActionResult{T}"/> containing the expanded <see cref="ValueSet"/>
/// if it exists, or appropriate error responses such as 404 Not Found or 500 Internal Server Error.
/// </returns>
[HttpGet("ValueSet/$expand")]
[HttpGet("ValueSet/{id}/$expand")]
public ActionResult<ValueSet> ExpandValueSet([FromRoute] string? id, [FromQuery] string? url,
[FromQuery] string? date)
{
try
{
return Ok(fhirService.ExpandValueSet(id?.Sanitize(), url?.Sanitize(), date?.Sanitize()));
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
catch (KeyNotFoundException ex)
{
return NotFound(ex.Message);
}
catch (InvalidOperationException ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
#endregion
#region Code Systems
/// <summary>
/// Retrieves a CodeSystem resource by its unique identifier.
/// </summary>
/// <param name="id">The unique identifier for the CodeSystem resource to retrieve.</param>
/// <returns>
/// An <see cref="ActionResult{T}"/> containing the requested <see cref="CodeSystem"/>
/// if it exists, a 400 Bad Request response if the id is null or empty, or a
/// 404 Not Found response if the CodeSystem is not found.
/// </returns>
[HttpGet("CodeSystem/{id}")]
public ActionResult<CodeSystem> GetCodeSystemById([FromRoute] string id)
{
try
{
return Ok(fhirService.GetCodeSystemById(id));
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
catch (KeyNotFoundException ex)
{
return NotFound(ex.Message);
}
}
/// <summary>
/// Retrieves a collection of CodeSystem resources based on the specified query parameters.
/// </summary>
/// <param name="url">The canonical URL of the CodeSystem to retrieve. If provided, retrieves a specific CodeSystem.</param>
/// <param name="summary">
/// An optional parameter to request a summary representation of the CodeSystems.
/// If not specified, full details will be retrieved.
/// </param>
/// <returns>
/// An <see cref="ActionResult{T}"/> containing a <see cref="Bundle"/> with the requested
/// CodeSystem resources. Returns a 400 Bad Request response if neither the <paramref name="url"/>
/// nor <paramref name="summary"/> is provided.
/// </returns>
[HttpGet("CodeSystem")]
public ActionResult<Bundle> GetCodeSystems([FromQuery] string? url, [FromQuery(Name = "_summary")] SummaryType? summary)
{
try
{
return Ok(fhirService.GetCodeSystems(url?.Sanitize(), summary));
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
catch (InvalidOperationException ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
/// <summary>
/// Validates a code in a specific CodeSystem, using either the CodeSystem's unique identifier
/// or its URL. Optionally validates its display value as well.
/// </summary>
/// <param name="url">The URL of the CodeSystem in which the code should be validated. Optional if the id is provided.</param>
/// <param name="id">The unique identifier of the CodeSystem. Optional if the URL is provided.</param>
/// <param name="code">The code to validate. This parameter is required.</param>
/// <param name="display">An optional display value to validate against the code.</param>
/// <param name="parameters">A set of parameters containing validation details, such as the URL, code, and display, if not passed via other parameters.</param>
/// <returns>
/// A <see cref="Parameters"/> resource indicating the validation result. This includes a boolean "result"
/// indicating success or failure, and a message explaining the result if applicable.
/// </returns>
[HttpPost("CodeSystem/$validate-code")]
[HttpPost("CodeSystem/{id}/$validate-code")]
public ActionResult<Parameters> ValidateCodeInCodeSystem([FromQuery] string? url, [FromRoute] string? id,
[FromQuery] string? code, [FromQuery] string? display, [FromBody] Parameters? parameters)
{
try
{
return Ok(fhirService.ValidateCodeInCodeSystem(
url?.Sanitize(),
id?.Sanitize(),
code?.Sanitize(),
display?.Sanitize(),
parameters));
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
}
/// <summary>
/// Validates a given code, optionally with its system and display, against a specified ValueSet.
/// </summary>
/// <param name="url">The canonical URL of the ValueSet to validate against. This parameter is optional if the id is provided.</param>
/// <param name="id">The unique identifier of the ValueSet to validate against. This parameter is optional if the URL is provided.</param>
/// <param name="system">The system of the code to validate. This parameter is optional.</param>
/// <param name="code">The code to validate. This parameter is required.</param>
/// <param name="display">The display text associated with the code to validate. This parameter is optional.</param>
/// <param name="parameters">Additional parameters supplied in the request body to guide the validation operation. This parameter is optional.</param>
/// <returns>
/// A <see cref="Parameters"/> resource that indicates the result of the validation and may contain
/// additional information about the validation outcome.
/// </returns>
[HttpPost("ValueSet/$validate-code")]
[HttpPost("ValueSet/{id}/$validate-code")]
public ActionResult<Parameters> ValidateCodeInValueSet([FromQuery] string? url, [FromRoute] string? id,
[FromQuery] string? system, [FromQuery] string? code, [FromQuery] string? display,
[FromBody] Parameters? parameters)
{
try
{
return Ok(fhirService.ValidateCodeInValueSet(url, id, system, code, display, parameters));
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
}
#endregion
/// <summary>
/// Returns the CapabilityStatement describing the functionalities
/// and conformance requirements of the FHIR Terminology server.
/// </summary>
/// <returns>
/// A <see cref="CapabilityStatement"/> object detailing the supported
/// capabilities, interactions, formats, and resources of the server.
/// </returns>
[HttpGet("metadata")]
public CapabilityStatement GetMetaData()
{
return fhirService.GetMetaData();
}
}