Implement US-03 patient profile updates
This commit is contained in:
1 parent
eb2cf2e54a
commit
a9633e49e1
8 files changed
+123
-5
No files matched your search
@@ -26,4 +26,13 @@ public sealed class PatientApplicationService(PatientRepository repository)
|
||||
|
||||
public Task<Patient?> GetPatientAsync(Guid patientId, CancellationToken cancellationToken) =>
|
||||
repository.FindByIdAsync(patientId, cancellationToken);
|
||||
|
||||
public async Task<Patient?> UpdatePatientAsync(Guid patientId, UpdatePatientRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(request.Name);
|
||||
if (!await repository.UpdateProfileAsync(patientId, request.Name, request.ContactInfo, cancellationToken))
|
||||
return null;
|
||||
|
||||
return await repository.FindByIdAsync(patientId, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,5 @@ public interface PatientRepository
|
||||
{
|
||||
Task<Patient> SaveAsync(Patient patient, CancellationToken cancellationToken);
|
||||
Task<Patient?> FindByIdAsync(Guid patientId, CancellationToken cancellationToken);
|
||||
Task<bool> UpdateProfileAsync(Guid patientId, string name, string? contactInfo, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace PatientProfileService.Contracts;
|
||||
|
||||
public sealed record UpdatePatientRequest
|
||||
{
|
||||
[Required]
|
||||
public string? Name { get; init; }
|
||||
|
||||
public string? ContactInfo { get; init; }
|
||||
}
|
||||
@@ -23,6 +23,13 @@ public sealed class PatientController(PatientApplicationService service) : Contr
|
||||
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);
|
||||
}
|
||||
@@ -15,4 +15,15 @@ public sealed class SqlPatientRepository(PatientDbContext db) : PatientRepositor
|
||||
|
||||
public Task<Patient?> FindByIdAsync(Guid patientId, CancellationToken cancellationToken) =>
|
||||
db.Patients.AsNoTracking().SingleOrDefaultAsync(p => p.PatientId == patientId, cancellationToken);
|
||||
|
||||
public async Task<bool> UpdateProfileAsync(Guid patientId, string name, string? contactInfo, CancellationToken cancellationToken)
|
||||
{
|
||||
var now = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified);
|
||||
var affected = await db.Patients.Where(p => p.PatientId == patientId)
|
||||
.ExecuteUpdateAsync(update => update
|
||||
.SetProperty(p => p.Name, name)
|
||||
.SetProperty(p => p.ContactInfo, contactInfo)
|
||||
.SetProperty(p => p.UpdatedAt, now), cancellationToken);
|
||||
return affected != 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user