Implement US-07 blood-pressure observation history

This commit is contained in:
Steffen committed 2026-10-02 09:22:49 +02:00
1 parent df47bcbbd7
commit 0ea7866f82
7 files changed
+155 -1

No files matched your search

@@ -18,6 +18,10 @@ public sealed class ObservationController(IHttpClientFactory clients) : Controll
public Task GetLatest(Guid patientId, CancellationToken cancellationToken) =>
ForwardAsync(HttpMethod.Get, $"api/patients/{patientId}/observations/latest", cancellationToken);
[HttpGet("patients/{patientId:guid}/observations")]
public Task GetByPeriod(Guid patientId, CancellationToken cancellationToken) =>
ForwardAsync(HttpMethod.Get, $"api/patients/{patientId}/observations{Request.QueryString}", cancellationToken);
private async Task ForwardAsync(HttpMethod method, string path, CancellationToken cancellationToken)
{
using var request = new HttpRequestMessage(method, path);
@@ -40,4 +40,18 @@ public sealed class ObservationApplicationService(ObservationRepository reposito
public Task<BloodPressureObservation?> GetLatestAsync(Guid patientId, CancellationToken cancellationToken) =>
repository.FindLatestByPatientIdAsync(patientId, cancellationToken);
public Task<List<BloodPressureObservation>> GetByPeriodAsync(Guid patientId, DateTime from, DateTime to,
CancellationToken cancellationToken)
{
from = NormalizeTimestamp(from);
to = NormalizeTimestamp(to);
if (to < from)
throw new ArgumentException("The end time must not be earlier than the start time.", nameof(to));
return repository.FindByPatientIdAndPeriodAsync(patientId, from, to, cancellationToken);
}
private static DateTime NormalizeTimestamp(DateTime timestamp) =>
DateTime.SpecifyKind(timestamp.Kind == DateTimeKind.Unspecified ? timestamp : timestamp.ToUniversalTime(),
DateTimeKind.Unspecified);
}
@@ -7,4 +7,6 @@ public interface ObservationRepository
Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken);
Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken);
Task<BloodPressureObservation?> FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken);
Task<List<BloodPressureObservation>> FindByPatientIdAndPeriodAsync(Guid patientId, DateTime from, DateTime to,
CancellationToken cancellationToken);
}
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using ObservationService.Application;
using ObservationService.Contracts;
using ObservationService.Domain;
@@ -44,6 +45,23 @@ public sealed class ObservationController(ObservationApplicationService service)
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,
CancellationToken cancellationToken)
{
try
{
var observations = await service.GetByPeriodAsync(patientId, from, to, cancellationToken);
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);
@@ -20,4 +20,10 @@ public sealed class SqlObservationRepository(ObservationDbContext db) : Observat
db.Observations.AsNoTracking().Where(o => o.PatientId == patientId)
.OrderByDescending(o => o.MeasuredAt).ThenByDescending(o => o.ObservationId)
.FirstOrDefaultAsync(cancellationToken);
public Task<List<BloodPressureObservation>> FindByPatientIdAndPeriodAsync(Guid patientId, DateTime from, DateTime to,
CancellationToken cancellationToken) =>
db.Observations.AsNoTracking()
.Where(o => o.PatientId == patientId && o.MeasuredAt >= from && o.MeasuredAt <= to)
.OrderBy(o => o.MeasuredAt).ThenBy(o => o.ObservationId).ToListAsync(cancellationToken);
}
+5 -1
View File
@@ -1,4 +1,4 @@
# US-01–US-06: Patient profiles and blood-pressure observations
# US-01–US-07: Patient profiles and blood-pressure observations
Implemented with .NET 10 LTS (see the [official download page](https://dotnet.microsoft.com/en-us/download/dotnet/10.0)), ASP.NET Core controllers, and PostgreSQL/EF Core. The API gateway, Patient/Profile service and Observation Service are separate executables and can run on separate machines. Each domain service owns its database.
@@ -52,6 +52,10 @@ The Observation Service checks patient existence by calling the gateway's patien
US-06 acceptance tests cover out-of-order measurements, patient-specific selection, retrieval after restart, and no available observation even when another patient has recorded measurements. No new dependencies or schema changes are required.
`GET /api/patients/{patientId}/observations?from=2026-09-03T00:00:00&to=2026-09-08T23:59:59` returns `200` with a JSON array of that patient's observations within the inclusive measurement-time bounds, ordered chronologically. A valid period with no matches returns `[]`. Both timestamps are required; missing, malformed, or reversed bounds return `400` validation problem details. Equal bounds are valid. Offset timestamps are normalized to UTC consistently with recording; encode `+` as `%2B` in query parameters. Equal measurement timestamps are ordered by observation identifier. The query runs in the Observation Service's own database; no patient-profile lookup is needed.
US-07 tests cover all four acceptance scenarios, patient-specific filtering, persistence after restart, inclusive and equal bounds, offset normalization, and malformed/missing bounds without changing saved observations. No new dependencies or schema changes are required.
Authentication and access control are defined in US-13, US-14 and `Docs/Design/authentication-and-access-control-design.md`, and remain to be implemented as separate stories, as explicitly instructed. These endpoints currently have no access restrictions.
## Automated acceptance tests