示例#1
0
        public void AddBooking(NewBookingRequest newBookingRequest)
        {
            var validationResult = _validator.ValidateRequest(newBookingRequest);

            if (!validationResult.PassedValidation)
            {
                throw new ArgumentException(validationResult.Errors.First());
            }

            var bookingId          = new Guid();
            var bookingStartTime   = newBookingRequest.StartTime;
            var bookingEndTime     = newBookingRequest.EndTime;
            var bookingPatientId   = newBookingRequest.PatientId;
            var bookingPatient     = _context.Patient.FirstOrDefault(x => x.Id == newBookingRequest.PatientId);
            var bookingDoctorId    = newBookingRequest.DoctorId;
            var bookingDoctor      = _context.Doctor.FirstOrDefault(x => x.Id == newBookingRequest.DoctorId);
            var bookingSurgeryType = _context.Patient.FirstOrDefault(x => x.Id == bookingPatientId).Clinic.SurgeryType;

            var myBooking = new Order
            {
                Id          = bookingId,
                StartTime   = bookingStartTime,
                EndTime     = bookingEndTime,
                PatientId   = bookingPatientId,
                DoctorId    = bookingDoctorId,
                Patient     = bookingPatient,
                Doctor      = bookingDoctor,
                SurgeryType = (int)bookingSurgeryType
            };

            _context.Order.AddRange(new List <Order> {
                myBooking
            });
            _context.SaveChanges();
        }
示例#2
0
        public bool BookingTimeInvalid(NewBookingRequest request, ref PdrValidationResult result)
        {
            var errors = new List <string>();

            if (request.StartTime < DateTime.Now)
            {
                errors.Add("StartTime cannot be in the past");
            }

            if (request.EndTime < DateTime.Now)
            {
                errors.Add("EndTime cannot be in the past");
            }

            if (request.EndTime < request.StartTime)
            {
                errors.Add("EndTime cannot be before StartTime");
            }

            if (errors.Any())
            {
                result.PassedValidation = false;
                result.Errors.AddRange(errors);
                return(true);
            }

            return(false);
        }
示例#3
0
        public PdrValidationResult ValidateRequest(NewBookingRequest request)
        {
            var result = new PdrValidationResult(true);

            ValidateBookingParameters(request, ref result);
            BookingAlreadyInDB(request, ref result);
            return(result);
        }
示例#4
0
        public IActionResult AddBooking(NewBookingRequest newBooking)
        {
            bool result = _bookingService.AddBooking(newBooking);

            if (true == result)
            {
                return(StatusCode(200));
            }

            return(BadRequest());
        }
示例#5
0
        /// <summary>
        /// validates if booking already exists in db for same patient for same time
        /// </summary>
        /// <param name="request"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private bool BookingAlreadyInDB(NewBookingRequest request, ref PdrValidationResult result)
        {
            if (_context.Order.Any(x => x.PatientId == request.PatientId && x.StartTime >= request.StartTime && x.EndTime <= request.EndTime))
            {
                result.PassedValidation = false;
                result.Errors.Add("Booking already exists for this patient");
                return(true);
            }

            return(false);
        }
示例#6
0
        /// <summary>
        /// Validate request object parameters
        /// </summary>
        /// <param name="request"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private bool ValidateBookingParameters(NewBookingRequest request, ref PdrValidationResult result)
        {
            TimeSpan ts = request.StartTime.Subtract(DateTime.UtcNow);

            if (ts.TotalMinutes < 0)
            {
                result.PassedValidation = false;
                result.Errors.Add("Booking Can not made in Past");
                return(true);
            }
            return(false);
        }
示例#7
0
        public IActionResult AddBooking(NewBookingRequest newBooking)
        {
            try
            {
                _bookingService.AddPatientBooking(newBooking);
                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }

            return(StatusCode(200));
        }
示例#8
0
        public void AddBooking_Delegates_To_Service()
        {
            //arrange
            _bookingController = new Controllers.BookingController(_context, _bookingService.Object);
            var req = new NewBookingRequest()
            {
                PatientId = 1
            };

            //act
            _bookingController.AddBooking(req);

            //assert
            _bookingService.Verify(s => s.AddBooking(req), Times.Once());
        }
示例#9
0
 public IActionResult AddBooking(NewBookingRequest newBookingRequest)
 {
     try
     {
         _bookingService.AddBooking(newBookingRequest);
         return(StatusCode(200));
     }
     catch (ArgumentException ex)
     {
         return(BadRequest(ex.Message));
     }
     catch (Exception ex)
     {
         return(StatusCode(500, ex));
     }
 }
        public PdrValidationResult ValidateRequest(NewBookingRequest request)
        {
            var result = new PdrValidationResult(true);

            if (BookingInThePast(request, ref result))
            {
                return(result);
            }

            if (DoctorAlreadyBooked(request, ref result))
            {
                return(result);
            }

            return(result);
        }
示例#11
0
        public PdrValidationResult ValidateRequest(NewBookingRequest request)
        {
            var result = new PdrValidationResult(true);

            if (BookingTimeInvalid(request, ref result))
            {
                return(result);
            }

            if (DoctorIsNotAvailable(request, ref result))
            {
                return(result);
            }

            return(result);
        }
        private bool BookingInThePast(NewBookingRequest request, ref PdrValidationResult result)
        {
            var errors = new List <string>();

            if (request.StartTime < DateTime.Now)
            {
                errors.Add("Bookings can not be in the past");
            }

            if (errors.Any())
            {
                result.PassedValidation = false;
                result.Errors.AddRange(errors);
                return(true);
            }

            return(false);
        }
示例#13
0
        public bool DoctorIsNotAvailable(NewBookingRequest request, ref PdrValidationResult result)
        {
            var doctorsBookings = _context.Order.Where(o => o.DoctorId == request.DoctorId);

            if (doctorsBookings.Any())
            {
                var bookedTime = doctorsBookings.Where(b => !(b.StartTime >= request.EndTime || request.StartTime >= b.EndTime));

                if (bookedTime.Any())
                {
                    result.PassedValidation = false;
                    result.Errors.Add("The Doctor is already booked in this time slot");
                    return(true);
                }
            }

            return(false);
        }
        private bool DoctorAlreadyBooked(NewBookingRequest request, ref PdrValidationResult result)
        {
            var errors = new List <string>();


            if (_context.Order.Any(x => x.DoctorId == request.DoctorId && x.StartTime <= request.EndTime && request.StartTime <= x.EndTime))
            {
                errors.Add("Doctor is already booked for the requested time.");
            }


            if (errors.Any())
            {
                result.PassedValidation = false;
                result.Errors.AddRange(errors);
                return(true);
            }

            return(false);
        }
示例#15
0
        public bool AddBooking(NewBookingRequest newBooking)
        {
            var bookingId          = new Guid();
            var bookingStartTime   = newBooking.StartTime;
            var bookingEndTime     = newBooking.EndTime;
            var bookingPatientId   = newBooking.PatientId;
            var bookingPatient     = _context.Patient.FirstOrDefault(x => x.Id == newBooking.PatientId);
            var bookingDoctorId    = newBooking.DoctorId;
            var bookingDoctor      = _context.Doctor.FirstOrDefault(x => x.Id == newBooking.DoctorId);
            var bookingSurgeryType = _context.Patient.FirstOrDefault(x => x.Id == bookingPatientId).Clinic.SurgeryType;

            var validationResult = _newBookingRequestValidator.ValidateRequest(newBooking);

            if (validationResult.PassedValidation != true)
            {
                return(false);
            }

            var myBooking = new Order
            {
                Id          = bookingId,
                StartTime   = bookingStartTime,
                EndTime     = bookingEndTime,
                PatientId   = bookingPatientId,
                DoctorId    = bookingDoctorId,
                Patient     = bookingPatient,
                Doctor      = bookingDoctor,
                SurgeryType = (int)bookingSurgeryType
            };

            _context.Order.AddRange(new List <Order> {
                myBooking
            });
            _context.SaveChanges();

            return(true);
        }