From 4cb6798ec98f407b809660950970623eb8f72665 Mon Sep 17 00:00:00 2001 From: Steffen Date: Fri, 2 Oct 2026 09:35:01 +0200 Subject: [PATCH] Implement US-09 blood-pressure observation deletion --- .../Controllers/ObservationController.cs | 4 + .../ObservationApplicationService.cs | 3 + .../Application/ObservationRepository.cs | 1 + .../Controllers/ObservationController.cs | 4 + .../Persistence/SqlObservationRepository.cs | 4 + src/README.md | 6 +- .../DeleteObservationTests.cs | 91 +++++++++++++++++++ 7 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 tests/Observation.AcceptanceTests/DeleteObservationTests.cs diff --git a/src/ApiGateway/Controllers/ObservationController.cs b/src/ApiGateway/Controllers/ObservationController.cs index 428e2af..b19a87a 100644 --- a/src/ApiGateway/Controllers/ObservationController.cs +++ b/src/ApiGateway/Controllers/ObservationController.cs @@ -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); diff --git a/src/ObservationService/Application/ObservationApplicationService.cs b/src/ObservationService/Application/ObservationApplicationService.cs index d48a0b6..d5ba9eb 100644 --- a/src/ObservationService/Application/ObservationApplicationService.cs +++ b/src/ObservationService/Application/ObservationApplicationService.cs @@ -6,6 +6,9 @@ namespace ObservationService.Application; public sealed class ObservationApplicationService(ObservationRepository repository, IHttpClientFactory clients) { + public Task DeleteAsync(Guid observationId, CancellationToken cancellationToken) => + repository.DeleteAsync(observationId, cancellationToken); + public Task UpdateAsync(Guid observationId, UpdateObservationRequest request, CancellationToken cancellationToken) { diff --git a/src/ObservationService/Application/ObservationRepository.cs b/src/ObservationService/Application/ObservationRepository.cs index 0f5dcb8..7b0067b 100644 --- a/src/ObservationService/Application/ObservationRepository.cs +++ b/src/ObservationService/Application/ObservationRepository.cs @@ -4,6 +4,7 @@ namespace ObservationService.Application; public interface ObservationRepository { + Task DeleteAsync(Guid observationId, CancellationToken cancellationToken); Task UpdateValuesAsync(Guid observationId, int systolic, int diastolic, int pulse, DateTime updatedAt, CancellationToken cancellationToken); Task SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken); diff --git a/src/ObservationService/Controllers/ObservationController.cs b/src/ObservationService/Controllers/ObservationController.cs index ca8c742..d126703 100644 --- a/src/ObservationService/Controllers/ObservationController.cs +++ b/src/ObservationService/Controllers/ObservationController.cs @@ -31,6 +31,10 @@ public sealed class ObservationController(ObservationApplicationService service) } } + [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) diff --git a/src/ObservationService/Persistence/SqlObservationRepository.cs b/src/ObservationService/Persistence/SqlObservationRepository.cs index bc7479e..625c2dc 100644 --- a/src/ObservationService/Persistence/SqlObservationRepository.cs +++ b/src/ObservationService/Persistence/SqlObservationRepository.cs @@ -6,6 +6,10 @@ namespace ObservationService.Persistence; public sealed class SqlObservationRepository(ObservationDbContext db) : ObservationRepository { + public async Task DeleteAsync(Guid observationId, CancellationToken cancellationToken) => + await db.Observations.Where(o => o.ObservationId == observationId) + .ExecuteDeleteAsync(cancellationToken) > 0; + public async Task UpdateValuesAsync(Guid observationId, int systolic, int diastolic, int pulse, DateTime updatedAt, CancellationToken cancellationToken) { diff --git a/src/README.md b/src/README.md index c4d7b02..5bd6563 100644 --- a/src/README.md +++ b/src/README.md @@ -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 diff --git a/tests/Observation.AcceptanceTests/DeleteObservationTests.cs b/tests/Observation.AcceptanceTests/DeleteObservationTests.cs new file mode 100644 index 0000000..9592363 --- /dev/null +++ b/tests/Observation.AcceptanceTests/DeleteObservationTests.cs @@ -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 +{ + 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 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())!.PatientId; + } + + private async Task 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())!; + } + + private Task HistoryAsync(Guid patientId) => + fixture.Client.GetFromJsonAsync( + $"/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($"/api/observations/{earlier.ObservationId}")); + Assert.Equal(earlier, await fixture.Client.GetFromJsonAsync( + $"/api/patients/{patientId}/observations/latest")); + Assert.Equal(other, await fixture.Client.GetFromJsonAsync($"/api/observations/{other.ObservationId}")); + Assert.Equal(other, Assert.Single((await HistoryAsync(other.PatientId))!)); + Assert.Equal(patientId, (await fixture.Client.GetFromJsonAsync($"/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( + $"/api/observations/{observation.ObservationId}")); + Assert.Equal(observation, Assert.Single((await HistoryAsync(observation.PatientId))!)); + } +}