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,58 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ApiGateway.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/patients")]
|
||||
public sealed class PatientController(IHttpClientFactory clients) : ControllerBase
|
||||
{
|
||||
[HttpPost]
|
||||
public Task CreatePatient(CancellationToken cancellationToken) =>
|
||||
ForwardAsync(HttpMethod.Post, "api/patients", cancellationToken);
|
||||
|
||||
[HttpGet("{patientId:guid}")]
|
||||
public Task GetPatient(Guid patientId, CancellationToken cancellationToken) =>
|
||||
ForwardAsync(HttpMethod.Get, $"api/patients/{patientId}", cancellationToken);
|
||||
|
||||
private async Task ForwardAsync(HttpMethod method, string path, CancellationToken cancellationToken)
|
||||
{
|
||||
using var request = new HttpRequestMessage(method, path);
|
||||
if (method == HttpMethod.Post)
|
||||
{
|
||||
request.Content = new StreamContent(Request.Body);
|
||||
if (Request.ContentType is not null)
|
||||
request.Content.Headers.TryAddWithoutValidation("Content-Type", Request.ContentType);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var upstream = await clients.CreateClient("PatientProfile")
|
||||
.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
||||
Response.StatusCode = (int)upstream.StatusCode;
|
||||
Response.ContentType = upstream.Content.Headers.ContentType?.ToString();
|
||||
if (upstream.Headers.Location is { } location)
|
||||
Response.Headers.Location = location.IsAbsoluteUri ? location.PathAndQuery : location.ToString();
|
||||
await upstream.Content.CopyToAsync(Response.Body, cancellationToken);
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
await WriteFailureAsync(StatusCodes.Status502BadGateway);
|
||||
}
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await WriteFailureAsync(StatusCodes.Status504GatewayTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
private Task WriteFailureAsync(int status)
|
||||
{
|
||||
if (Response.HasStarted)
|
||||
{
|
||||
HttpContext.Abort();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
Response.Clear();
|
||||
return Results.Problem(statusCode: status, title: "Patient/Profile service is unavailable.")
|
||||
.ExecuteAsync(HttpContext);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user