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> 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 DeleteObservation(Guid observationId, CancellationToken cancellationToken) => await service.DeleteAsync(observationId, cancellationToken) ? NoContent() : NotFound(); [HttpPut("observations/{observationId:guid}")] public async Task> 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> 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> 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> 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>> 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); }