11 KiB
US-01–US-07: Patient profiles and blood-pressure observations
Implemented with .NET 10 LTS (see the official download page), 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.
The controller calls PatientApplicationService, which uses PatientRepository, implemented by SqlPatientRepository. The service owns the patient table from the ER diagram, including creation/update timestamps stored as UTC in PostgreSQL timestamp columns.
Build and run
Install a .NET 10 SDK and provision an empty PostgreSQL database owned by the Patient/Profile service's database user. From the repository root:
dotnet restore HealthApp.slnx
dotnet build HealthApp.slnx -c Release --no-restore
# In the Patient/Profile service terminal; substitute your own database configuration.
export ConnectionStrings__PatientDatabase='Host=127.0.0.1;Database=patient_profile;Username=patient_profile;Password=YOUR_LOCAL_PASSWORD'
dotnet run --project src/PatientProfileService -c Release --no-build -- --migrate
ASPNETCORE_URLS=http://127.0.0.1:5081 dotnet run --project src/PatientProfileService -c Release --no-build
# In a separate gateway terminal.
Services__PatientProfile__BaseUrl=http://127.0.0.1:5081/ Services__Observation__BaseUrl=http://127.0.0.1:5082/ ASPNETCORE_URLS=http://127.0.0.1:5080 dotnet run --project src/ApiGateway -c Release --no-build
# In a separate observation terminal, with its own provisioned database.
export ConnectionStrings__ObservationDatabase='Host=127.0.0.1;Database=observations;Username=observations;Password=YOUR_LOCAL_PASSWORD'
dotnet run --project src/ObservationService -c Release --no-build -- --migrate
Services__ApiGateway__BaseUrl=http://127.0.0.1:5080/ ASPNETCORE_URLS=http://127.0.0.1:5082 dotnet run --project src/ObservationService -c Release --no-build
--migrate applies committed EF Core migrations and exits. Apply migrations as a deployment step before starting the service; normal startup does not modify the schema. Both services fail startup with a clear message if required configuration is absent. Configuration uses standard .NET JSON files, environment variables, and command-line arguments. Service endpoints and database hosts may be remote.
API
Send requests to the gateway:
curl -i http://127.0.0.1:5080/api/patients \
-H 'Content-Type: application/json' \
-d '{"name":"Steffen Example","dateOfBirth":"1980-01-01","contactInfo":"steffen@example.test"}'
POST /api/patients returns 201 Created, a relative Location: /api/patients/{patientId} header, and a JSON profile containing patientId, name, dateOfBirth, and contactInfo. Identifiers are generated UUIDs and the database primary key enforces uniqueness. Names must be nonblank; dates of birth must be supplied as valid YYYY-MM-DD dates. Invalid requests return 400 validation problem details and do not create profiles. Contact information is optional because US-01 does not require it.
GET /api/patients/{patientId} retrieves the persisted profile, or returns 404 when absent. Retrieval is included because US-01 explicitly requires it. The gateway returns 502 for connection failures and 504 for upstream timeouts.
PUT /api/patients/{patientId} updates the name and contact information and returns 200 with the updated profile. It preserves the patient identifier, date of birth and creation timestamp, and updates the modification timestamp. Names must be nonblank, consistent with profile creation; omitted or null contact information clears that optional value. A missing patient returns 404 without creating a profile. Invalid requests return 400 without changing stored information.
POST /api/patients/{patientId}/observations records a blood-pressure observation. Supply measuredAt, positive integer systolic, diastolic and pulse, plus optional context and note. It returns 201, the saved observation including its generated observationId and patientId, and Location: /api/observations/{observationId}. GET at that location retrieves the persisted observation, or returns 404 if absent. Retrieval is included to satisfy US-04's requirement that a recorded observation can be retrieved later.
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.
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.
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
Tests require a real PostgreSQL instance. Provide an administrator connection with permission to create/drop databases; the fixture creates an isolated database named us01_<random>, applies the committed migration, and deletes that database at teardown. Never point this connection at a production server.
TEST_POSTGRES_CONNECTION='Host=127.0.0.1;Database=postgres;Username=postgres;Password=YOUR_LOCAL_TEST_PASSWORD' \
dotnet test HealthApp.slnx -c Release
The xUnit tests send HTTP requests to the gateway; the gateway reaches a real Kestrel Patient/Profile service over HTTP. They cover every US-01 acceptance scenario, retrieval after host restart, distinct identifiers, optional contact information, invalid JSON/dates, and rejection without database writes. Test assertions use an independent HTTP response contract. Production projects have no references to each other's implementation.
US-02 retrieval scenarios are covered by the creation tests. US-03 tests cover persisted updates after restart, preservation of identity and date of birth, missing-patient rejection without insertion, and invalid-name rejection without changing the profile.
US-04 tests use real HTTP between all three hosts and separate temporary patient and observation databases. They cover every recording acceptance scenario, retrieval after restart, optional fields, distinct identifiers, malformed/missing fields, missing observations, and patient-lookup failure without database writes. Both services' committed migrations are applied twice to verify idempotence. The Observation Service uses the same EF Core PostgreSQL dependencies as Patient/Profile for its independent persistence; no new library types are introduced.
Dependencies: Npgsql's EF Core PostgreSQL provider supplies persistence; EF Core Relational is pinned to the same patch as EF Core Design to keep runtime/tooling versions aligned; EF Core Design supports migration maintenance. xUnit, its Visual Studio runner, Microsoft.NET.Test.Sdk, and ASP.NET Core MVC Testing are used only for automated tests. The gateway uses the platform's HttpClient and has no extra package dependency.
Linux deployment
Publish each service separately:
dotnet publish src/PatientProfileService -c Release -o /tmp/healthapp-publish/patient-profile
dotnet publish src/ApiGateway -c Release -o /tmp/healthapp-publish/api-gateway
dotnet publish src/ObservationService -c Release -o /tmp/healthapp-publish/observation
Copy each output to its corresponding /opt/healthapp/ directory on the intended host, install the ASP.NET Core 10 runtime, and create a healthapp service user/group. Adjust the paths/user in deploy/systemd/*.service for the host. Copy the environment examples to /etc/healthapp/patient-profile.env and /etc/healthapp/api-gateway.env, replace example values, and restrict their permissions to protect credentials. Configure the gateway certificate for the HTTPS client entry point. Its example uses a PFX certificate; standard Kestrel configuration also supports other certificate formats.
Apply the database migration using the published Patient/Profile executable with its database configuration:
dotnet /opt/healthapp/patient-profile/PatientProfileService.dll --migrate
dotnet /opt/healthapp/observation/ObservationService.dll --migrate
Install the corresponding unit on each host and enable/start it with systemd. The units are independent, write logs to journald, restart after failures, and allow graceful shutdown. The gateway has no systemd dependency on a local Patient/Profile process; it uses the configured network endpoint.
The Observation Service has its own healthapp-observation.service unit and environment example. Configure its Services__ApiGateway__BaseUrl and the gateway's Services__Observation__BaseUrl for the deployed hosts. Apply its migration with its own database configuration. Services need not be started in a fixed order; requests needing an unavailable upstream fail without saving observations.