finalizare 1.0
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Chats.GetChats;
|
||||
|
||||
public class GetConversationsValidator : AbstractValidator<GetConversationsCommand>
|
||||
{
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user