Files
FACULTATE-HEALTHCARE_MANAGER/backend/Application/Endpoints/Chats/GetChats/GetConversationsValidator.cs
T
2024-05-30 15:40:13 +03:00

37 lines
1.5 KiB
C#

using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.Chats.GetChats;
public class GetConversationsValidator : AbstractValidator<GetConversationsCommand>
{
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public GetConversationsValidator(IDoctorRepository doctorRepository, IPatientRepository patientRepository)
{
RuleFor(x => x.IdUser1)
.NotEmpty().WithMessage("IdUser1 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.IdUser2)
.NotEmpty().WithMessage("IdUser2 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x)
.MustAsync(CheckForUsersExistence).WithMessage("One of the users is not existing.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
private async Task<bool> CheckForUsersExistence(GetConversationsCommand request, CancellationToken token)
{
var firstCheck = await _patientRepository.GetByIdAsync(request.IdUser1, token) != null &&
await _doctorRepository.GetByIdAsync(request.IdUser2, token) != null;
var secondCheck = await _patientRepository.GetByIdAsync(request.IdUser2, token) != null &&
await _doctorRepository.GetByIdAsync(request.IdUser1, token) != null;
return firstCheck || secondCheck;
}
}