Implement US-06 latest blood-pressure observation retrieval

This commit is contained in:
Steffen committed 2026-10-02 09:15:09 +02:00
1 parent 05f57dcba3
commit df47bcbbd7
8 files changed
+97 -2

No files matched your search

@@ -14,6 +14,10 @@ public sealed class ObservationController(IHttpClientFactory clients) : Controll
public Task GetObservation(Guid observationId, CancellationToken cancellationToken) =>
ForwardAsync(HttpMethod.Get, $"api/observations/{observationId}", cancellationToken);
[HttpGet("patients/{patientId:guid}/observations/latest")]
public Task GetLatest(Guid patientId, CancellationToken cancellationToken) =>
ForwardAsync(HttpMethod.Get, $"api/patients/{patientId}/observations/latest", cancellationToken);
private async Task ForwardAsync(HttpMethod method, string path, CancellationToken cancellationToken)
{
using var request = new HttpRequestMessage(method, path);
@@ -37,4 +37,7 @@ public sealed class ObservationApplicationService(ObservationRepository reposito
public Task<BloodPressureObservation?> GetByIdAsync(Guid observationId, CancellationToken cancellationToken) =>
repository.FindByIdAsync(observationId, cancellationToken);
public Task<BloodPressureObservation?> GetLatestAsync(Guid patientId, CancellationToken cancellationToken) =>
repository.FindLatestByPatientIdAsync(patientId, cancellationToken);
}
@@ -6,4 +6,5 @@ public interface ObservationRepository
{
Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken);
Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken);
Task<BloodPressureObservation?> FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken);
}
@@ -37,6 +37,13 @@ public sealed class ObservationController(ObservationApplicationService service)
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));
}
private static ObservationDto ToDto(BloodPressureObservation observation) =>
new(observation.ObservationId, observation.PatientId, observation.MeasuredAt, observation.Systolic,
observation.Diastolic, observation.Pulse, observation.Context, observation.Note);
@@ -15,4 +15,9 @@ public sealed class SqlObservationRepository(ObservationDbContext db) : Observat
public Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken) =>
db.Observations.AsNoTracking().SingleOrDefaultAsync(o => o.ObservationId == observationId, cancellationToken);
public Task<BloodPressureObservation?> FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken) =>
db.Observations.AsNoTracking().Where(o => o.PatientId == patientId)
.OrderByDescending(o => o.MeasuredAt).ThenByDescending(o => o.ObservationId)
.FirstOrDefaultAsync(cancellationToken);
}
+6 -2
View File
@@ -1,4 +1,4 @@
# US-01–US-04: Patient profiles and blood-pressure recording
# US-01–US-06: 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.
@@ -48,7 +48,11 @@ curl -i http://127.0.0.1:5080/api/patients \
The Observation Service checks patient existence by calling the gateway's patient retrieval API over HTTP before saving. It has no reference to Patient/Profile implementation code and no access to that service's database. Missing patients return `404`; validation failures return `400`. Patient lookup failures return `502` or `504` and do not save an observation. Measurement timestamps without an offset are stored as supplied; timestamps with an offset are normalized to UTC. PostgreSQL uses the diagram's `timestamp` columns without a time-zone component.
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. These endpoints currently have no access restrictions.
`GET /api/patients/{patientId}/observations/latest` returns `200` with that patient's observation having the greatest measurement timestamp, regardless of recording order. It returns `404` when no observation is available. Equal measurement timestamps are resolved consistently by observation identifier; US-06 does not prescribe which tied observation to return. Selection happens in the Observation Service's own database through the documented controller/application-service/repository layers.
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.
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