91 lines
4.2 KiB
C#
91 lines
4.2 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
using ObservationService.Application;
|
|
using ObservationService.Contracts;
|
|
using ObservationService.Domain;
|
|
|
|
namespace ObservationService.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api")]
|
|
public sealed class ObservationController(ObservationApplicationService service) : ControllerBase
|
|
{
|
|
[HttpPost("patients/{patientId:guid}/observations")]
|
|
public async Task<ActionResult<ObservationDto>> RecordObservation(Guid patientId, RecordObservationRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var observation = await service.RecordAsync(patientId, request, cancellationToken);
|
|
return observation is null
|
|
? NotFound()
|
|
: CreatedAtAction(nameof(GetObservation), new { observationId = observation.ObservationId }, ToDto(observation));
|
|
}
|
|
catch (HttpRequestException)
|
|
{
|
|
return Problem(statusCode: StatusCodes.Status502BadGateway, title: "Patient lookup is unavailable.");
|
|
}
|
|
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
return Problem(statusCode: StatusCodes.Status504GatewayTimeout, title: "Patient lookup timed out.");
|
|
}
|
|
}
|
|
|
|
[HttpDelete("observations/{observationId:guid}")]
|
|
public async Task<IActionResult> DeleteObservation(Guid observationId, CancellationToken cancellationToken) =>
|
|
await service.DeleteAsync(observationId, cancellationToken) ? NoContent() : NotFound();
|
|
|
|
[HttpPut("observations/{observationId:guid}")]
|
|
public async Task<ActionResult<ObservationDto>> UpdateObservation(Guid observationId, UpdateObservationRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var observation = await service.UpdateAsync(observationId, request, cancellationToken);
|
|
return observation is null ? NotFound() : Ok(ToDto(observation));
|
|
}
|
|
|
|
[HttpPatch("observations/{observationId:guid}")]
|
|
public async Task<ActionResult<ObservationDto>> PatchObservation(Guid observationId, PatchObservationRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var observation = await service.PatchAsync(observationId, request, cancellationToken);
|
|
return observation is null ? NotFound() : Ok(ToDto(observation));
|
|
}
|
|
|
|
[HttpGet("observations/{observationId:guid}")]
|
|
public async Task<ActionResult<ObservationDto>> GetObservation(Guid observationId, CancellationToken cancellationToken)
|
|
{
|
|
var observation = await service.GetByIdAsync(observationId, cancellationToken);
|
|
return observation is null ? NotFound() : Ok(ToDto(observation));
|
|
}
|
|
|
|
[HttpGet("patients/{patientId:guid}/observations/latest")]
|
|
public async Task<ActionResult<ObservationDto>> GetLatest(Guid patientId, CancellationToken cancellationToken)
|
|
{
|
|
var observation = await service.GetLatestAsync(patientId, cancellationToken);
|
|
return observation is null ? NotFound() : Ok(ToDto(observation));
|
|
}
|
|
|
|
[HttpGet("patients/{patientId:guid}/observations")]
|
|
public async Task<ActionResult<List<ObservationDto>>> GetByPeriod(Guid patientId,
|
|
[FromQuery, BindRequired] DateTime from, [FromQuery, BindRequired] DateTime to,
|
|
[FromQuery] ContextSelection selection, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var observations = await service.GetByPeriodAsync(patientId, from, to, selection, cancellationToken);
|
|
Response.Headers["X-Measurement-Setting"] = selection.Setting ?? "All";
|
|
Response.Headers["X-Activity-Condition"] = selection.ActivityCondition ?? "All";
|
|
return Ok(observations.Select(ToDto).ToList());
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
ModelState.AddModelError(nameof(to), "The end time must not be earlier than the start time.");
|
|
return ValidationProblem(ModelState);
|
|
}
|
|
}
|
|
|
|
private static ObservationDto ToDto(BloodPressureObservation observation) =>
|
|
new(observation.ObservationId, observation.PatientId, observation.MeasuredAt, observation.Systolic,
|
|
observation.Diastolic, observation.Pulse, observation.Context, observation.Note, observation.MeasurementContext);
|
|
}
|