using PatientProfileService.Contracts; using PatientProfileService.Domain; namespace PatientProfileService.Application; public sealed class PatientApplicationService(PatientRepository repository) { public Task CreatePatientAsync(CreatePatientRequest request, CancellationToken cancellationToken) { ArgumentException.ThrowIfNullOrWhiteSpace(request.Name); 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 repository.SaveAsync(new Patient { PatientId = Guid.NewGuid(), Name = request.Name, DateOfBirth = request.DateOfBirth.Value, ContactInfo = request.ContactInfo, CreatedAt = now, UpdatedAt = now }, cancellationToken); } public Task GetPatientAsync(Guid patientId, CancellationToken cancellationToken) => repository.FindByIdAsync(patientId, cancellationToken); public async Task 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); } }