Implement US-01 patient profile creation and retrieval
This commit is contained in:
1 parent
d6f79a91c0
commit
eb2cf2e54a
30 files changed
+792
No files matched your search
@@ -0,0 +1,29 @@
|
||||
using PatientProfileService.Contracts;
|
||||
using PatientProfileService.Domain;
|
||||
|
||||
namespace PatientProfileService.Application;
|
||||
|
||||
public sealed class PatientApplicationService(PatientRepository repository)
|
||||
{
|
||||
public Task<Patient> CreatePatientAsync(CreatePatientRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(request.Name);
|
||||
if (request.DateOfBirth is null)
|
||||
throw new ArgumentException("A date of birth is required.", nameof(request));
|
||||
|
||||
// The documented TIMESTAMP columns store UTC without a time-zone component.
|
||||
var now = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified);
|
||||
return repository.SaveAsync(new Patient
|
||||
{
|
||||
PatientId = Guid.NewGuid(),
|
||||
Name = request.Name,
|
||||
DateOfBirth = request.DateOfBirth.Value,
|
||||
ContactInfo = request.ContactInfo,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<Patient?> GetPatientAsync(Guid patientId, CancellationToken cancellationToken) =>
|
||||
repository.FindByIdAsync(patientId, cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using PatientProfileService.Domain;
|
||||
|
||||
namespace PatientProfileService.Application;
|
||||
|
||||
public interface PatientRepository
|
||||
{
|
||||
Task<Patient> SaveAsync(Patient patient, CancellationToken cancellationToken);
|
||||
Task<Patient?> FindByIdAsync(Guid patientId, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace PatientProfileService.Contracts;
|
||||
|
||||
public sealed record CreatePatientRequest
|
||||
{
|
||||
[Required]
|
||||
public string? Name { get; init; }
|
||||
|
||||
[Required]
|
||||
public DateOnly? DateOfBirth { get; init; }
|
||||
|
||||
public string? ContactInfo { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace PatientProfileService.Contracts;
|
||||
|
||||
public sealed record PatientDto(Guid PatientId, string Name, DateOnly DateOfBirth, string? ContactInfo);
|
||||
@@ -0,0 +1,28 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using PatientProfileService.Application;
|
||||
using PatientProfileService.Contracts;
|
||||
using PatientProfileService.Domain;
|
||||
|
||||
namespace PatientProfileService.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/patients")]
|
||||
public sealed class PatientController(PatientApplicationService service) : ControllerBase
|
||||
{
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<PatientDto>> CreatePatient(CreatePatientRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var patient = await service.CreatePatientAsync(request, cancellationToken);
|
||||
return CreatedAtAction(nameof(GetPatient), new { patientId = patient.PatientId }, ToDto(patient));
|
||||
}
|
||||
|
||||
[HttpGet("{patientId:guid}")]
|
||||
public async Task<ActionResult<PatientDto>> GetPatient(Guid patientId, CancellationToken cancellationToken)
|
||||
{
|
||||
var patient = await service.GetPatientAsync(patientId, cancellationToken);
|
||||
return patient is null ? NotFound() : Ok(ToDto(patient));
|
||||
}
|
||||
|
||||
private static PatientDto ToDto(Patient patient) =>
|
||||
new(patient.PatientId, patient.Name, patient.DateOfBirth, patient.ContactInfo);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace PatientProfileService.Domain;
|
||||
|
||||
public sealed class Patient
|
||||
{
|
||||
public Guid PatientId { get; set; }
|
||||
public string Name { get; set; } = "";
|
||||
public DateOnly DateOfBirth { get; set; }
|
||||
public string? ContactInfo { 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
+62
@@ -0,0 +1,62 @@
|
||||
// <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 PatientProfileService.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace PatientProfileService.Persistence.Migrations
|
||||
{
|
||||
[DbContext(typeof(PatientDbContext))]
|
||||
[Migration("20260930200031_InitialPatientSchema")]
|
||||
partial class InitialPatientSchema
|
||||
{
|
||||
/// <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("PatientProfileService.Domain.Patient", b =>
|
||||
{
|
||||
b.Property<Guid>("PatientId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("patient_id");
|
||||
|
||||
b.Property<string>("ContactInfo")
|
||||
.HasColumnType("character varying")
|
||||
.HasColumnName("contact_info");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("created_at");
|
||||
|
||||
b.Property<DateOnly>("DateOfBirth")
|
||||
.HasColumnType("date")
|
||||
.HasColumnName("date_of_birth");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("character varying")
|
||||
.HasColumnName("name");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("updated_at");
|
||||
|
||||
b.HasKey("PatientId");
|
||||
|
||||
b.ToTable("patient", (string)null);
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace PatientProfileService.Persistence.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialPatientSchema : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "patient",
|
||||
columns: table => new
|
||||
{
|
||||
patient_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
name = table.Column<string>(type: "character varying", nullable: false),
|
||||
date_of_birth = table.Column<DateOnly>(type: "date", nullable: false),
|
||||
contact_info = 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_patient", x => x.patient_id);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "patient");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using PatientProfileService.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace PatientProfileService.Persistence.Migrations
|
||||
{
|
||||
[DbContext(typeof(PatientDbContext))]
|
||||
partial class PatientDbContextModelSnapshot : 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("PatientProfileService.Domain.Patient", b =>
|
||||
{
|
||||
b.Property<Guid>("PatientId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("patient_id");
|
||||
|
||||
b.Property<string>("ContactInfo")
|
||||
.HasColumnType("character varying")
|
||||
.HasColumnName("contact_info");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("created_at");
|
||||
|
||||
b.Property<DateOnly>("DateOfBirth")
|
||||
.HasColumnType("date")
|
||||
.HasColumnName("date_of_birth");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("character varying")
|
||||
.HasColumnName("name");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("timestamp without time zone")
|
||||
.HasColumnName("updated_at");
|
||||
|
||||
b.HasKey("PatientId");
|
||||
|
||||
b.ToTable("patient", (string)null);
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PatientProfileService.Domain;
|
||||
|
||||
namespace PatientProfileService.Persistence;
|
||||
|
||||
public sealed class PatientDbContext(DbContextOptions<PatientDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<Patient> Patients => Set<Patient>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
var patient = modelBuilder.Entity<Patient>();
|
||||
patient.ToTable("patient");
|
||||
patient.HasKey(p => p.PatientId);
|
||||
patient.Property(p => p.PatientId).HasColumnName("patient_id").ValueGeneratedNever();
|
||||
patient.Property(p => p.Name).HasColumnName("name").HasColumnType("character varying").IsRequired();
|
||||
patient.Property(p => p.DateOfBirth).HasColumnName("date_of_birth").HasColumnType("date");
|
||||
patient.Property(p => p.ContactInfo).HasColumnName("contact_info").HasColumnType("character varying");
|
||||
patient.Property(p => p.CreatedAt).HasColumnName("created_at").HasColumnType("timestamp without time zone");
|
||||
patient.Property(p => p.UpdatedAt).HasColumnName("updated_at").HasColumnType("timestamp without time zone");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PatientProfileService.Application;
|
||||
using PatientProfileService.Domain;
|
||||
|
||||
namespace PatientProfileService.Persistence;
|
||||
|
||||
public sealed class SqlPatientRepository(PatientDbContext db) : PatientRepository
|
||||
{
|
||||
public async Task<Patient> SaveAsync(Patient patient, CancellationToken cancellationToken)
|
||||
{
|
||||
db.Patients.Add(patient);
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
return patient;
|
||||
}
|
||||
|
||||
public Task<Patient?> FindByIdAsync(Guid patientId, CancellationToken cancellationToken) =>
|
||||
db.Patients.AsNoTracking().SingleOrDefaultAsync(p => p.PatientId == patientId, cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PatientProfileService.Application;
|
||||
using PatientProfileService.Persistence;
|
||||
|
||||
namespace PatientProfileService;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddProblemDetails();
|
||||
builder.Services.AddDbContext<PatientDbContext>((services, options) =>
|
||||
options.UseNpgsql(services.GetRequiredService<IConfiguration>().GetConnectionString("PatientDatabase")));
|
||||
builder.Services.AddScoped<PatientRepository, SqlPatientRepository>();
|
||||
builder.Services.AddScoped<PatientApplicationService>();
|
||||
|
||||
var app = builder.Build();
|
||||
if (string.IsNullOrWhiteSpace(app.Configuration.GetConnectionString("PatientDatabase")))
|
||||
throw new InvalidOperationException("ConnectionStrings:PatientDatabase must be configured.");
|
||||
|
||||
if (args.Contains("--migrate"))
|
||||
{
|
||||
await using var scope = app.Services.CreateAsyncScope();
|
||||
await scope.ServiceProvider.GetRequiredService<PatientDbContext>().Database.MigrateAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
app.UseExceptionHandler();
|
||||
app.MapControllers();
|
||||
await app.RunAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user