Implement owner-scoped Observation lifecycle with serialized patches

This commit is contained in:
Steffen committed 2026-10-14 11:46:03 +02:00
1 parent bfe6ebf5af
commit 3a70e3e296
37 files changed
+31249 -9

No files matched your search

@@ -0,0 +1,33 @@
using HealthApp.Contracts;
using HealthApp.Infrastructure;
using HealthApp.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace HealthApp.Controllers;
[ApiController,Route("api")]
public sealed class ObservationController(ObservationQueries queries,ObservationCommands commands,AccessPolicy access,RequestTransaction tx) : ControllerBase
{
private IActionResult Unavailable(){Response.StatusCode=404;return new EmptyResult();}
private async Task<AuthorizedPatientContext?> Owner(Guid id,bool write,CancellationToken ct){var owner=await queries.ResolveOwnerAsync(id,ct);return owner is null?null:await access.AuthorizeAsync(User,owner.Value,write,ct);}
[HttpPost("patients/{patientId:guid}/observations"),Authorize(Policy="Patient")]
public async Task<IActionResult> Record(Guid patientId,RecordObservationRequest r,CancellationToken ct){var scope=await access.AuthorizeAsync(User,patientId,true,ct);if(scope is null)return Unavailable();var o=await commands.RecordAsync(scope,r,ct);await tx.FinishAsync(ct);return o is null?NotFound():Created($"/api/observations/{o.ObservationId}",o);}
[HttpGet("observations/{observationId:guid}")]
public async Task<IActionResult> Get(Guid observationId,CancellationToken ct){var scope=await Owner(observationId,false,ct);if(scope is null)return Unavailable();var o=await queries.GetAsync(scope,observationId,ct);await tx.FinishAsync(ct);return o is null?NotFound():Ok(o);}
[HttpPut("observations/{observationId:guid}"),Authorize(Policy="Patient")]
public Task<IActionResult> Put(Guid observationId,UpdateObservationRequest r,CancellationToken ct)=>Patch(observationId,new PatchObservationRequest{Systolic=r.Systolic,Diastolic=r.Diastolic,Pulse=r.Pulse},ct);
[HttpPatch("observations/{observationId:guid}"),Authorize(Policy="Patient")]
public async Task<IActionResult> Patch(Guid observationId,PatchObservationRequest r,CancellationToken ct){var scope=await Owner(observationId,true,ct);if(scope is null)return Unavailable();var o=await commands.PatchAsync(scope,observationId,r,ct);await tx.FinishAsync(ct);return o is null?NotFound():Ok(o);}
[HttpDelete("observations/{observationId:guid}"),Authorize(Policy="Patient")]
public async Task<IActionResult> Delete(Guid observationId,CancellationToken ct){var scope=await Owner(observationId,true,ct);if(scope is null)return Unavailable();var found=await commands.DeleteAsync(scope,observationId,ct);await tx.FinishAsync(ct);return found?NoContent():NotFound();}
[HttpGet("patients/{patientId:guid}/observations/latest")]
public async Task<IActionResult> Latest(Guid patientId,CancellationToken ct){var scope=await access.AuthorizeAsync(User,patientId,false,ct);if(scope is null)return Unavailable();var o=await queries.LatestAsync(scope,ct);await tx.FinishAsync(ct);return o is null?NotFound():Ok(o);}
[HttpGet("patients/{patientId:guid}/observations")]
public async Task<IActionResult> History(Guid patientId,[FromQuery,BindRequired]DateTime from,[FromQuery,BindRequired]DateTime to,[FromQuery]ContextSelection selection,CancellationToken ct)
{
var scope=await access.AuthorizeAsync(User,patientId,false,ct);if(scope is null)return Unavailable();
try{var series=await queries.SelectedAsync(scope,from,to,selection,ct);await tx.FinishAsync(ct);Response.Headers["X-Measurement-Setting"]=selection.Setting??"All";Response.Headers["X-Activity-Condition"]=selection.ActivityCondition??"All";return Ok(series);}
catch(ArgumentException){ModelState.AddModelError(nameof(to),"The end time must not be earlier than the start time.");return ValidationProblem(ModelState);}
}
}