Implement US-09 blood-pressure observation deletion

This commit is contained in:
Steffen committed 2026-10-02 09:35:01 +02:00
1 parent e4379ad2f4
commit 4cb6798ec9
7 files changed
+112 -1

No files matched your search

@@ -10,6 +10,10 @@ public sealed class ObservationController(IHttpClientFactory clients) : Controll
public Task RecordObservation(Guid patientId, CancellationToken cancellationToken) =>
ForwardAsync(HttpMethod.Post, $"api/patients/{patientId}/observations", cancellationToken);
[HttpDelete("observations/{observationId:guid}")]
public Task DeleteObservation(Guid observationId, CancellationToken cancellationToken) =>
ForwardAsync(HttpMethod.Delete, $"api/observations/{observationId}", cancellationToken);
[HttpPut("observations/{observationId:guid}")]
public Task UpdateObservation(Guid observationId, CancellationToken cancellationToken) =>
ForwardAsync(HttpMethod.Put, $"api/observations/{observationId}", cancellationToken);
@@ -6,6 +6,9 @@ namespace ObservationService.Application;
public sealed class ObservationApplicationService(ObservationRepository repository, IHttpClientFactory clients)
{
public Task<bool> DeleteAsync(Guid observationId, CancellationToken cancellationToken) =>
repository.DeleteAsync(observationId, cancellationToken);
public Task<BloodPressureObservation?> UpdateAsync(Guid observationId, UpdateObservationRequest request,
CancellationToken cancellationToken)
{
@@ -4,6 +4,7 @@ namespace ObservationService.Application;
public interface ObservationRepository
{
Task<bool> DeleteAsync(Guid observationId, CancellationToken cancellationToken);
Task<BloodPressureObservation?> UpdateValuesAsync(Guid observationId, int systolic, int diastolic, int pulse,
DateTime updatedAt, CancellationToken cancellationToken);
Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken);
@@ -31,6 +31,10 @@ public sealed class ObservationController(ObservationApplicationService service)
}
}
[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)
@@ -6,6 +6,10 @@ namespace ObservationService.Persistence;
public sealed class SqlObservationRepository(ObservationDbContext db) : ObservationRepository
{
public async Task<bool> DeleteAsync(Guid observationId, CancellationToken cancellationToken) =>
await db.Observations.Where(o => o.ObservationId == observationId)
.ExecuteDeleteAsync(cancellationToken) > 0;
public async Task<BloodPressureObservation?> UpdateValuesAsync(Guid observationId, int systolic, int diastolic,
int pulse, DateTime updatedAt, CancellationToken cancellationToken)
{
+5 -1
View File
@@ -1,4 +1,4 @@
# US-01–US-08: Patient profiles and blood-pressure observations
# US-01–US-09: 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.
@@ -60,6 +60,10 @@ US-07 tests cover all four acceptance scenarios, patient-specific filtering, per
US-08 tests cover the documented correction and rejection scenarios, correction of all three values, persistence after restart, preservation of other fields and creation time, modification timestamps, visibility in latest/history, unaffected other observations, and missing/malformed values. No new dependencies are introduced.
`DELETE /api/observations/{observationId}` removes the observation and returns `204 No Content`. A missing or previously deleted observation returns `404`. The Observation Service deletes the row from its own database in a single statement; deleted observations disappear from specific retrieval, history, and latest selection. Other observations and the patient profile are preserved.
US-09 tests cover both acceptance scenarios, persistence of deletion after restart, remaining/latest observations, deletion of the only observation, repeated deletion, and preservation of other patients' 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
@@ -0,0 +1,91 @@
using System.Net;
using System.Net.Http.Json;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace Observation.AcceptanceTests;
public sealed class DeleteObservationTests(ObservationApiFixture fixture) : IClassFixture<ObservationApiFixture>
{
private sealed record Profile(Guid PatientId);
private sealed record Observation(Guid ObservationId, Guid PatientId, DateTime MeasuredAt,
int Systolic, int Diastolic, int Pulse, string? Context, string? Note);
private async Task<Guid> CreatePatientAsync()
{
using var response = await fixture.Client.PostAsJsonAsync("/api/patients", new
{
name = "Deletion Example", dateOfBirth = "1980-01-01"
});
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
return (await response.Content.ReadFromJsonAsync<Profile>())!.PatientId;
}
private async Task<Observation> RecordAsync(Guid patientId, string measuredAt)
{
using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new
{
measuredAt, systolic = 128, diastolic = 82, pulse = 68, context = "Resting", note = "Before breakfast"
});
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
return (await response.Content.ReadFromJsonAsync<Observation>())!;
}
private Task<Observation[]?> HistoryAsync(Guid patientId) =>
fixture.Client.GetFromJsonAsync<Observation[]>(
$"/api/patients/{patientId}/observations?from=2026-09-01T00:00:00&to=2026-09-30T23:59:59");
[Fact]
public async Task Deleted_observation_stays_absent_after_restart_and_other_observations_are_preserved()
{
var patientId = await CreatePatientAsync();
var earlier = await RecordAsync(patientId, "2026-09-10T08:00:00");
var deleted = await RecordAsync(patientId, "2026-09-12T08:00:00");
var other = await RecordAsync(await CreatePatientAsync(), "2026-09-13T08:00:00");
using var response = await fixture.Client.DeleteAsync($"/api/observations/{deleted.ObservationId}");
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
Assert.Empty(await response.Content.ReadAsStringAsync());
await fixture.RestartHostsAsync();
using var retrieval = await fixture.Client.GetAsync($"/api/observations/{deleted.ObservationId}");
Assert.Equal(HttpStatusCode.NotFound, retrieval.StatusCode);
Assert.Equal(earlier, Assert.Single((await HistoryAsync(patientId))!));
Assert.Equal(earlier, await fixture.Client.GetFromJsonAsync<Observation>($"/api/observations/{earlier.ObservationId}"));
Assert.Equal(earlier, await fixture.Client.GetFromJsonAsync<Observation>(
$"/api/patients/{patientId}/observations/latest"));
Assert.Equal(other, await fixture.Client.GetFromJsonAsync<Observation>($"/api/observations/{other.ObservationId}"));
Assert.Equal(other, Assert.Single((await HistoryAsync(other.PatientId))!));
Assert.Equal(patientId, (await fixture.Client.GetFromJsonAsync<Profile>($"/api/patients/{patientId}"))!.PatientId);
await using var db = fixture.CreateObservationDbContext();
Assert.False(await db.Observations.AnyAsync(o => o.ObservationId == deleted.ObservationId));
}
[Fact]
public async Task Deleting_the_only_observation_leaves_empty_history_and_no_latest_observation()
{
var patientId = await CreatePatientAsync();
var observation = await RecordAsync(patientId, "2026-09-12T08:00:00");
using var response = await fixture.Client.DeleteAsync($"/api/observations/{observation.ObservationId}");
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
Assert.Empty((await HistoryAsync(patientId))!);
using var latest = await fixture.Client.GetAsync($"/api/patients/{patientId}/observations/latest");
Assert.Equal(HttpStatusCode.NotFound, latest.StatusCode);
using var repeated = await fixture.Client.DeleteAsync($"/api/observations/{observation.ObservationId}");
Assert.Equal(HttpStatusCode.NotFound, repeated.StatusCode);
}
[Fact]
public async Task Missing_observation_returns_not_found_without_changing_existing_data()
{
var observation = await RecordAsync(await CreatePatientAsync(), "2026-09-12T08:00:00");
await using var db = fixture.CreateObservationDbContext();
var count = await db.Observations.CountAsync();
using var response = await fixture.Client.DeleteAsync($"/api/observations/{Guid.NewGuid()}");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.Equal(count, await db.Observations.CountAsync());
Assert.Equal(observation, await fixture.Client.GetFromJsonAsync<Observation>(
$"/api/observations/{observation.ObservationId}"));
Assert.Equal(observation, Assert.Single((await HistoryAsync(observation.PatientId))!));
}
}