Files
HealthAppRepositoryB/src/HealthApp/Controllers/HealthOverviewController.cs
T

18 lines
1.0 KiB
C#

using HealthApp.Contracts;
using HealthApp.Infrastructure;
using HealthApp.Security;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace HealthApp.Controllers;
[ApiController]
public sealed class HealthOverviewController(OverviewQueries overview,AccessPolicy access,RequestTransaction tx) : ControllerBase
{
[HttpGet("api/patients/{patientId:guid}/overview")]
public async Task<IActionResult> Get(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){Response.StatusCode=404;return new EmptyResult();}
try{var o=await overview.GetAsync(scope,from,to,selection,ct);await tx.FinishAsync(ct);return o is null?Problem(statusCode:404,title:"Patient does not exist."):Ok(o);}
catch(ArgumentException){ModelState.AddModelError(nameof(to),"The end time must not be earlier than the start time.");return ValidationProblem(ModelState);}
}
}