using Application.Services.Database.PostgreSQL; using FluentValidation; namespace Application.Endpoints.MedicalHistories; public class MedicalHistoryCreateValidation : AbstractValidator { private readonly IPatientRepository _patientRepository; public MedicalHistoryCreateValidation(IPatientRepository patientRepository) { _patientRepository = patientRepository; RuleFor(x => x.UserId) .NotEmpty().WithMessage("Patient is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString()) .MustAsync(BeExistingUser).WithMessage("Specified patient doesn't exist.") .WithErrorCode(HttpStatusCodes.NotFound.ToString()); RuleFor(x => x.Content) .NotEmpty().WithMessage("Description is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString()); } private async Task BeExistingUser(Guid userId, CancellationToken cancellationToken) { var patient = await _patientRepository.GetByIdAsync(userId); return patient != null; } }