public RegisterMedicalAppointmentResponse Ejecute(RegisterMedicalAppointmentRequest request)
        {
            MedicalAppointment newMedicalAppointment = null;
            var medicalAppointment =
                _unitOfWork.MedicalAppointmentRepository.FindFirstOrDefault(m => m.Id == request.Identification);

            var turn = CalculateTurn(request);

            if (!ValidatePatient(request.Patient))
            {
                return new RegisterMedicalAppointmentResponse {
                           Mensaje = "Este paciente ya tiene una cita medica programada"
                }
            }
            ;

            if (!ValidateDate(DateTime.Now, request.Date))
            {
                return new RegisterMedicalAppointmentResponse {
                           Mensaje = "Por favor, ingrese una fecha valida"
                }
            }
            ;

            if (!ValidateTurn(turn))
            {
                return new RegisterMedicalAppointmentResponse {
                           Mensaje = "Ya no hay mas cupos para esta jornada"
                }
            }
            ;

            if (medicalAppointment == null)
            {
                newMedicalAppointment = new MedicalAppointment();

                newMedicalAppointment.Doctor = new SearchDoctorService(_unitOfWork)
                                               .Ejecute(new SearchDoctorRequest {
                    Identification = request.Doctor.Id
                }).Doctor;                                                                         //request.Doctor;
                newMedicalAppointment.Patient = new SearchPatientService(_unitOfWork)
                                                .Ejecute(new SearchPatientRequest {
                    Identification = request.Patient.Id
                }).Patient;                                                                          //request.Patient;
                newMedicalAppointment.Date         = request.Date;
                newMedicalAppointment.Time         = newMedicalAppointment.Doctor.Workday;
                newMedicalAppointment.Turn         = turn;
                newMedicalAppointment.Prescription = null;
                newMedicalAppointment.GenerateCost();
                _unitOfWork.MedicalAppointmentRepository.Add(newMedicalAppointment);
                _unitOfWork.Commit();
                return(new RegisterMedicalAppointmentResponse {
                    Mensaje = "Cita medica creada satisfactoriamente", Turno = turn
                });
            }

            return(new RegisterMedicalAppointmentResponse {
                Mensaje = "Error al crear la cita medica"
            });
        }
        private int CalculateTurn(RegisterMedicalAppointmentRequest request)
        {
            var turn = 0;
            var medicalAppointments =
                _unitOfWork.MedicalAppointmentRepository.FindBy(m => m.Date == request.Date && m.Doctor == request.Doctor);

            foreach (var appointment in medicalAppointments)
            {
                if (appointment.State.Equals("Programada") || appointment.State.Equals("Aplazada"))
                {
                    turn++;
                }
            }

            if (turn == 0)
            {
                return(1);
            }

            return(turn + 1);
        }