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> 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> 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> 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> 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); }