Exemplo n.º 1
0
        public Task <bool> Handle(ReserveDoctorCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                NotifyValidationErrors(request);
                return(Task.FromResult(false));
            }

            var reservationDay = request.ReservationDay;
            var existingDoctor = _doctorRepository.GetById(request.Id);

            if (_doctorRepository.IsDoctorReservedByHour(existingDoctor.Id, reservationDay))
            {
                _mediator.RaiseEvent(new DomainNotification(request.MessageType, "Doctor's timetable has already been taken."));
                return(Task.FromResult(false));
            }

            existingDoctor.Reservations.Add(reservationDay.ToString());

            _doctorRepository.Update(existingDoctor);

            if (Commit())
            {
                _mediator.RaiseEvent(new DoctorReservedEvent(request.Id, request.ReservationDay, request.ReferenceId));
            }

            return(Task.FromResult(true));
        }
        private void PublishBookDoctorCommand(DoctorId doctorId, DateTime reservationDay)
        {
            var command = new ReserveDoctorCommand(doctorId, reservationDay, Id.Value);

            this.Publish(command);
        }