66 lines
2.6 KiB
C#
66 lines
2.6 KiB
C#
using Application.Services.Database.MongoDB;
|
|
using Application.Services.Database.PostgreSQL;
|
|
using FluentValidation;
|
|
|
|
namespace Application.Endpoints.Appointments;
|
|
|
|
public class DeleteAppointmentValidator : AbstractValidator<AppointmentManagementDto>
|
|
{
|
|
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
|
|
private readonly IDoctorRepository _doctorRepository;
|
|
private readonly IPatientRepository _patientRepository;
|
|
|
|
public DeleteAppointmentValidator(IAppointmentsMongoDbService appointmentsMongoDbService,
|
|
IDoctorRepository doctorRepository, IPatientRepository patientRepository)
|
|
{
|
|
RuleFor(x => x.DoctorId)
|
|
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
|
.MustAsync(IsDoctorRegistered).WithMessage("Doctor is not registered in system")
|
|
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
|
|
|
|
RuleFor(x => x.PatientId)
|
|
.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.Appointment)
|
|
.Must(IsAppointmentValidFormat).WithMessage("Appointment is not valid")
|
|
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
|
|
|
RuleFor(x => x)
|
|
.MustAsync(DoesAppointmentExists).WithMessage("Appointment not found in system")
|
|
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
|
|
|
|
_appointmentsMongoDbService = appointmentsMongoDbService;
|
|
_doctorRepository = doctorRepository;
|
|
_patientRepository = patientRepository;
|
|
}
|
|
|
|
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
var doctor = await _doctorRepository.GetByIdAsync(id);
|
|
return doctor != null;
|
|
}
|
|
|
|
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
var patient = await _patientRepository.GetByIdAsync(id);
|
|
return patient != null;
|
|
}
|
|
|
|
private bool IsAppointmentValidFormat(DateTime appointment)
|
|
{
|
|
if (appointment == default)
|
|
return false;
|
|
|
|
if (appointment.Date < DateTime.UtcNow.Date)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
private async Task<bool> DoesAppointmentExists(AppointmentManagementDto dto, CancellationToken cancellationToken)
|
|
{
|
|
return await _appointmentsMongoDbService.DoesAppointmentExists(dto);
|
|
}
|
|
} |