46 lines
2.0 KiB
C#
46 lines
2.0 KiB
C#
using PatientProfileService.Contracts;
|
|
using PatientProfileService.Domain;
|
|
|
|
namespace PatientProfileService.Application;
|
|
|
|
public sealed class PatientApplicationService(PatientRepository repository)
|
|
{
|
|
public async Task<Patient> CreatePatientAsync(CreatePatientRequest request, string accountId, CancellationToken cancellationToken)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(request.Name);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(accountId);
|
|
if (await repository.FindByAccountIdAsync(accountId, cancellationToken) is not null)
|
|
throw new ProfileAlreadyExistsException();
|
|
if (request.DateOfBirth is null)
|
|
throw new ArgumentException("A date of birth is required.", nameof(request));
|
|
|
|
// The documented TIMESTAMP columns store UTC without a time-zone component.
|
|
var now = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified);
|
|
return await repository.SaveAsync(new Patient
|
|
{
|
|
PatientId = Guid.NewGuid(),
|
|
AccountId = accountId,
|
|
Name = request.Name,
|
|
DateOfBirth = request.DateOfBirth.Value,
|
|
ContactInfo = request.ContactInfo,
|
|
CreatedAt = now,
|
|
UpdatedAt = now
|
|
}, cancellationToken);
|
|
}
|
|
|
|
public Task<Patient?> GetByAccountAsync(string accountId, CancellationToken cancellationToken) =>
|
|
repository.FindByAccountIdAsync(accountId, cancellationToken);
|
|
|
|
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);
|
|
}
|
|
}
|