public CloseBookingResponse Close(CloseBookingRequest request)
        {
            var userPrincipal = new UserPrincipal(ClaimsPrincipal.Current);

            if (userPrincipal.Id.HasValue)
            {
                return(BookingService.CloseBooking(
                           request, userPrincipal.Id.Value));
            }
            return(new CloseBookingResponse
            {
                Success = false,
                Message = "No user logged on"
            });
        }
Exemple #2
0
        public CloseBookingResponse CloseBooking(CloseBookingRequest request,
                                                 int accountId)
        {
            //get booking by id and ensure it exists
            var openBooking = BookingRepository.Find(request.BookingId);
            var car         = CarRepository.Find(openBooking.VehicleID);
            var user        = UserRepository.Find(accountId);

            if (openBooking == null || openBooking.BookingStatus !=
                Constants.BookingOpenStatus || car == null || car.Status !=
                Constants.CarBookedStatus || user == null)
            {
                return new CloseBookingResponse
                       {
                           Message = ValidateClosedBooking(openBooking, car, user),
                           Success = false
                       }
            }
            ;

            //look through cities and ensure one is close enough for check in
            var selectedCity =
                ValidateCity(request.Latitude, request.Longitude);

            //if city doesn't exist throw error
            if (selectedCity == null)
            {
                return new CloseBookingResponse
                       {
                           Message =
                               "No cities are within a " +
                               $"{Constants.BookingMaxRangeFromCityCentre}m radius",
                           Success = false
                       }
            }
            ;

            //set parameters to return car and calculate billing amount
            var returnDate  = DateTime.Now;
            var ts          = returnDate - openBooking.CheckOut;
            var totalHours  = (int)Math.Ceiling(ts.TotalHours);
            var totalAmount = totalHours * openBooking.BillingRate;


            //update cars status and its new location
            car.Status  = Constants.CarAvailableStatus;
            car.Suburb  = selectedCity.CityName;
            car.LatPos  = request.Latitude;
            car.LongPos = request.Longitude;

            CarRepository.Update(car);

            //update booking record to show this booking is closed
            openBooking.CheckIn       = returnDate;
            openBooking.BookingStatus = Constants.BookingClosedStatus;
            openBooking.TimeBilled    = totalHours;
            openBooking.AmountBilled  = totalAmount;
            openBooking.CityDropOff   = selectedCity.CityName;

            BookingRepository.Update(openBooking);

            //return a successful message that booking was closed
            return(new CloseBookingResponse
            {
                //return message to show booking and billing details
                City = selectedCity.CityName,
                HourlyRate = openBooking.BillingRate.ToString("C"),
                Message =
                    $"{car.Make} {car.Model} has been returned at " +
                    $"a cost of {totalAmount:C}",
                Success = true,
                TotalHours = totalHours.ToString(),
                TotalAmount = totalAmount.ToString("C")
            });
        }