using Application.Services.Database; using FluentValidation; namespace Application.Endpoints.Patients.Profile; public class PatientProfileValidation : AbstractValidator { private readonly IPatientRepository _patientRepository; public PatientProfileValidation(IPatientRepository patientRepository) { _patientRepository = patientRepository; RuleFor(x => x.Id) .NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString()) .MustAsync(IsPatientRegistered).WithMessage("Patient is not registered in system") .WithErrorCode(HttpStatusCodes.NotFound.ToString()); RuleFor(x => x.Email) .NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString()) .EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString()) .MustAsync(BeUniqueEmail).WithMessage("Email in use by another patient.") .WithErrorCode(HttpStatusCodes.Conflict.ToString()); RuleFor(x => x.Password) .NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString()) .MinimumLength(8).WithMessage("Password must be at least 8 characters long.") .WithErrorCode(HttpStatusCodes.BadRequest.ToString()); RuleFor(x => x.Name) .NotEmpty().WithMessage("Name is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString()) .MinimumLength(3).WithMessage("Name must be at least 3 characters long.") .WithErrorCode(HttpStatusCodes.BadRequest.ToString()); } private async Task IsPatientRegistered(Guid id, CancellationToken cancellationToken) { var patient = await _patientRepository.GetByIdAsync(id); return patient != null; } private async Task BeUniqueEmail(string email, CancellationToken cancellationToken) { var patient = await _patientRepository.FindByEmailAsync(email); return patient == null; } }