Files

52 lines
2.1 KiB
C#

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 accountId = Request.Headers["X-Account-Id"].ToString();
if (string.IsNullOrWhiteSpace(accountId)) return BadRequest();
try
{
var patient = await service.CreatePatientAsync(request, accountId, cancellationToken);
return CreatedAtAction(nameof(GetPatient), new { patientId = patient.PatientId }, ToDto(patient));
}
catch (ProfileAlreadyExistsException)
{
return Conflict(new { title = "A profile already exists for this account." });
}
}
[HttpGet("by-account/{accountId}")]
public async Task<ActionResult<PatientDto>> GetByAccount(string accountId, CancellationToken cancellationToken)
{
var patient = await service.GetByAccountAsync(accountId, cancellationToken);
return patient is null ? NotFound() : Ok(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));
}
[HttpPut("{patientId:guid}")]
public async Task<ActionResult<PatientDto>> UpdatePatient(Guid patientId, UpdatePatientRequest request, CancellationToken cancellationToken)
{
var patient = await service.UpdatePatientAsync(patientId, request, cancellationToken);
return patient is null ? NotFound() : Ok(ToDto(patient));
}
private static PatientDto ToDto(Patient patient) =>
new(patient.PatientId, patient.Name, patient.DateOfBirth, patient.ContactInfo);
}