Implement US-04 blood-pressure observation recording
This commit is contained in:
1 parent
a9633e49e1
commit
05f57dcba3
25 files changed
+843
-9
No files matched your search
@@ -0,0 +1,40 @@
|
||||
using System.Net;
|
||||
using ObservationService.Contracts;
|
||||
using ObservationService.Domain;
|
||||
|
||||
namespace ObservationService.Application;
|
||||
|
||||
public sealed class ObservationApplicationService(ObservationRepository repository, IHttpClientFactory clients)
|
||||
{
|
||||
public async Task<BloodPressureObservation?> RecordAsync(Guid patientId, RecordObservationRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.MeasuredAt is null || request.Systolic is null or <= 0 ||
|
||||
request.Diastolic is null or <= 0 || request.Pulse is null or <= 0)
|
||||
throw new ArgumentException("A timestamp and positive measured values are required.", nameof(request));
|
||||
|
||||
using var patient = await clients.CreateClient("ApiGateway").GetAsync($"api/patients/{patientId}",
|
||||
HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
||||
if (patient.StatusCode == HttpStatusCode.NotFound)
|
||||
return null;
|
||||
// Upstream failures must never be interpreted as an existing patient.
|
||||
patient.EnsureSuccessStatusCode();
|
||||
|
||||
var now = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified);
|
||||
var measuredAt = request.MeasuredAt.Value;
|
||||
if (measuredAt.Kind != DateTimeKind.Unspecified)
|
||||
measuredAt = measuredAt.ToUniversalTime();
|
||||
|
||||
return await repository.SaveAsync(new BloodPressureObservation
|
||||
{
|
||||
ObservationId = Guid.NewGuid(), PatientId = patientId,
|
||||
MeasuredAt = DateTime.SpecifyKind(measuredAt, DateTimeKind.Unspecified),
|
||||
Systolic = request.Systolic.Value, Diastolic = request.Diastolic.Value,
|
||||
Pulse = request.Pulse.Value, Context = request.Context, Note = request.Note,
|
||||
CreatedAt = now, UpdatedAt = now
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<BloodPressureObservation?> GetByIdAsync(Guid observationId, CancellationToken cancellationToken) =>
|
||||
repository.FindByIdAsync(observationId, cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using ObservationService.Domain;
|
||||
|
||||
namespace ObservationService.Application;
|
||||
|
||||
public interface ObservationRepository
|
||||
{
|
||||
Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken);
|
||||
Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace ObservationService.Contracts;
|
||||
|
||||
public sealed record ObservationDto(Guid ObservationId, Guid PatientId, DateTime MeasuredAt,
|
||||
int Systolic, int Diastolic, int Pulse, string? Context, string? Note);
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ObservationService.Contracts;
|
||||
|
||||
public sealed record RecordObservationRequest
|
||||
{
|
||||
[Required]
|
||||
public DateTime? MeasuredAt { get; init; }
|
||||
|
||||
[Required, Range(1, int.MaxValue)]
|
||||
public int? Systolic { get; init; }
|
||||
|
||||
[Required, Range(1, int.MaxValue)]
|
||||
public int? Diastolic { get; init; }
|
||||
|
||||
[Required, Range(1, int.MaxValue)]
|
||||
public int? Pulse { get; init; }
|
||||
|
||||
public string? Context { get; init; }
|
||||
public string? Note { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using ObservationService.Application;
|
||||
using ObservationService.Contracts;
|
||||
using ObservationService.Domain;
|
||||
|
||||
namespace ObservationService.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api")]
|
||||
public sealed class ObservationController(ObservationApplicationService service) : ControllerBase
|
||||
{
|
||||
[HttpPost("patients/{patientId:guid}/observations")]
|
||||
public async Task<ActionResult<ObservationDto>> RecordObservation(Guid patientId, RecordObservationRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var observation = await service.RecordAsync(patientId, request, cancellationToken);
|
||||
return observation is null
|
||||
? NotFound()
|
||||
: CreatedAtAction(nameof(GetObservation), new { observationId = observation.ObservationId }, ToDto(observation));
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
return Problem(statusCode: StatusCodes.Status502BadGateway, title: "Patient lookup is unavailable.");
|
||||
}
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return Problem(statusCode: StatusCodes.Status504GatewayTimeout, title: "Patient lookup timed out.");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("observations/{observationId:guid}")]
|
||||
public async Task<ActionResult<ObservationDto>> GetObservation(Guid observationId, CancellationToken cancellationToken)
|
||||
{
|
||||
var observation = await service.GetByIdAsync(observationId, 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);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace ObservationService.Domain;
|
||||
|
||||
public sealed class BloodPressureObservation
|
||||
{
|
||||
public Guid ObservationId { get; set; }
|
||||
public Guid PatientId { get; set; }
|
||||
public DateTime MeasuredAt { get; set; }
|
||||
public int Systolic { get; set; }
|
||||
public int Diastolic { get; set; }
|
||||
public int Pulse { get; set; }
|
||||
public string? Context { get; set; }
|
||||
public string? Note { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.12" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.12" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Generated
+77
@@ -0,0 +1,77 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using ObservationService.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace ObservationService.Persistence.Migrations
|
||||
{
|
||||
[DbContext(typeof(ObservationDbContext))]
|
||||
[Migration("20261001095222_InitialObservationSchema")]
|
||||
partial class InitialObservationSchema
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.12")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("ObservationService.Domain.BloodPressureObservation", b =>
|
||||
{
|
||||
b.Property<Guid>("ObservationId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("observation_id");
|
||||
|
||||
b.Property<string>("Context")
|
||||
.HasColumnType("character varying")
|
||||
.HasColumnName("context");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("created_at");
|
||||
|
||||
b.Property<int>("Diastolic")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("diastolic");
|
||||
|
||||
b.Property<DateTime>("MeasuredAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("measured_at");
|
||||
|
||||
b.Property<string>("Note")
|
||||
.HasColumnType("character varying")
|
||||
.HasColumnName("note");
|
||||
|
||||
b.Property<Guid>("PatientId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("patient_id");
|
||||
|
||||
b.Property<int>("Pulse")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("pulse");
|
||||
|
||||
b.Property<int>("Systolic")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("systolic");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("updated_at");
|
||||
|
||||
b.HasKey("ObservationId");
|
||||
|
||||
b.ToTable("blood_pressure_observation", (string)null);
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace ObservationService.Persistence.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialObservationSchema : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "blood_pressure_observation",
|
||||
columns: table => new
|
||||
{
|
||||
observation_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
patient_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
measured_at = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
|
||||
systolic = table.Column<int>(type: "integer", nullable: false),
|
||||
diastolic = table.Column<int>(type: "integer", nullable: false),
|
||||
pulse = table.Column<int>(type: "integer", nullable: false),
|
||||
context = table.Column<string>(type: "character varying", nullable: true),
|
||||
note = table.Column<string>(type: "character varying", nullable: true),
|
||||
created_at = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
|
||||
updated_at = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_blood_pressure_observation", x => x.observation_id);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "blood_pressure_observation");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using ObservationService.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace ObservationService.Persistence.Migrations
|
||||
{
|
||||
[DbContext(typeof(ObservationDbContext))]
|
||||
partial class ObservationDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.12")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("ObservationService.Domain.BloodPressureObservation", b =>
|
||||
{
|
||||
b.Property<Guid>("ObservationId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("observation_id");
|
||||
|
||||
b.Property<string>("Context")
|
||||
.HasColumnType("character varying")
|
||||
.HasColumnName("context");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("created_at");
|
||||
|
||||
b.Property<int>("Diastolic")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("diastolic");
|
||||
|
||||
b.Property<DateTime>("MeasuredAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("measured_at");
|
||||
|
||||
b.Property<string>("Note")
|
||||
.HasColumnType("character varying")
|
||||
.HasColumnName("note");
|
||||
|
||||
b.Property<Guid>("PatientId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("patient_id");
|
||||
|
||||
b.Property<int>("Pulse")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("pulse");
|
||||
|
||||
b.Property<int>("Systolic")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("systolic");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("updated_at");
|
||||
|
||||
b.HasKey("ObservationId");
|
||||
|
||||
b.ToTable("blood_pressure_observation", (string)null);
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ObservationService.Domain;
|
||||
|
||||
namespace ObservationService.Persistence;
|
||||
|
||||
public sealed class ObservationDbContext(DbContextOptions<ObservationDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<BloodPressureObservation> Observations => Set<BloodPressureObservation>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
var observation = modelBuilder.Entity<BloodPressureObservation>();
|
||||
observation.ToTable("blood_pressure_observation");
|
||||
observation.HasKey(o => o.ObservationId);
|
||||
observation.Property(o => o.ObservationId).HasColumnName("observation_id").ValueGeneratedNever();
|
||||
// An external patient identifier, not a cross-database foreign key.
|
||||
observation.Property(o => o.PatientId).HasColumnName("patient_id");
|
||||
observation.Property(o => o.MeasuredAt).HasColumnName("measured_at").HasColumnType("timestamp without time zone");
|
||||
observation.Property(o => o.Systolic).HasColumnName("systolic");
|
||||
observation.Property(o => o.Diastolic).HasColumnName("diastolic");
|
||||
observation.Property(o => o.Pulse).HasColumnName("pulse");
|
||||
observation.Property(o => o.Context).HasColumnName("context").HasColumnType("character varying");
|
||||
observation.Property(o => o.Note).HasColumnName("note").HasColumnType("character varying");
|
||||
observation.Property(o => o.CreatedAt).HasColumnName("created_at").HasColumnType("timestamp without time zone");
|
||||
observation.Property(o => o.UpdatedAt).HasColumnName("updated_at").HasColumnType("timestamp without time zone");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ObservationService.Application;
|
||||
using ObservationService.Domain;
|
||||
|
||||
namespace ObservationService.Persistence;
|
||||
|
||||
public sealed class SqlObservationRepository(ObservationDbContext db) : ObservationRepository
|
||||
{
|
||||
public async Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken)
|
||||
{
|
||||
db.Observations.Add(observation);
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
return observation;
|
||||
}
|
||||
|
||||
public Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken) =>
|
||||
db.Observations.AsNoTracking().SingleOrDefaultAsync(o => o.ObservationId == observationId, cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ObservationService.Application;
|
||||
using ObservationService.Persistence;
|
||||
|
||||
namespace ObservationService;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddProblemDetails();
|
||||
builder.Services.AddDbContext<ObservationDbContext>((services, options) =>
|
||||
options.UseNpgsql(services.GetRequiredService<IConfiguration>().GetConnectionString("ObservationDatabase")));
|
||||
builder.Services.AddScoped<ObservationRepository, SqlObservationRepository>();
|
||||
builder.Services.AddScoped<ObservationApplicationService>();
|
||||
builder.Services.AddHttpClient("ApiGateway", (services, client) =>
|
||||
client.BaseAddress = GetGatewayUri(services.GetRequiredService<IConfiguration>()))
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
|
||||
|
||||
var app = builder.Build();
|
||||
if (string.IsNullOrWhiteSpace(app.Configuration.GetConnectionString("ObservationDatabase")))
|
||||
throw new InvalidOperationException("ConnectionStrings:ObservationDatabase must be configured.");
|
||||
|
||||
if (args.Contains("--migrate"))
|
||||
{
|
||||
await using var scope = app.Services.CreateAsyncScope();
|
||||
await scope.ServiceProvider.GetRequiredService<ObservationDbContext>().Database.MigrateAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
_ = GetGatewayUri(app.Configuration);
|
||||
app.UseExceptionHandler();
|
||||
app.MapControllers();
|
||||
await app.RunAsync();
|
||||
}
|
||||
|
||||
private static Uri GetGatewayUri(IConfiguration configuration)
|
||||
{
|
||||
var endpoint = configuration["Services:ApiGateway:BaseUrl"];
|
||||
if (!Uri.TryCreate(endpoint, UriKind.Absolute, out var uri) ||
|
||||
(uri.Scheme != "http" && uri.Scheme != "https") || uri.AbsolutePath != "/" ||
|
||||
!string.IsNullOrEmpty(uri.Query) || !string.IsNullOrEmpty(uri.Fragment) || !string.IsNullOrEmpty(uri.UserInfo))
|
||||
throw new InvalidOperationException("Services:ApiGateway:BaseUrl must be an HTTP(S) origin URL.");
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user