Esempio n. 1
0
 public void Dispose()
 {
     BookingRepository?.Dispose();
     CarRepository?.Dispose();
     CarCategoryRepository?.Dispose();
     UserRepository?.Dispose();
     CityRepository?.Dispose();
     TransactionHistoryRepository?.Dispose();
 }
Esempio n. 2
0
        public UnitOfWork(AutoniverseContext context)
        {
            this.context  = context;
            Cars          = new CarRepository(context);
            CarCategories = new CarCategoryRepository(context);

            Motorcycles          = new MotorcycleRepository(context);
            MotorcycleCategories = new MotorcycleCategoryRepository(context);
        }
Esempio n. 3
0
        public OpenBookingResponse OpenBooking(int vehicleId, int accountId)
        {
            //this method allows a car to be checked ou
            //check car exists and is correct status
            var car = CarRepository.Find(vehicleId);

            if (car == null)
            {
                return new OpenBookingResponse
                       {
                           Message = $"Vehicle {vehicleId} does not exist",
                           Success = false
                       }
            }
            ;
            if (car.Status != Constants.CarAvailableStatus)
            {
                return new OpenBookingResponse
                       {
                           Message =
                               $"{car.Make} {car.Model} is not available to " +
                               "be booked",
                           Success = false
                       }
            }
            ;

            //check user exists and correct status
            var user = UserRepository.Find(accountId);

            if (user == null)
            {
                return new OpenBookingResponse
                       {
                           Message = $"Account {accountId} does not exist",
                           Success = false
                       }
            }
            ;
            if (user.Status != Constants.UserActiveStatus)
            {
                return new OpenBookingResponse
                       {
                           Message = "Only activated users can book cars",
                           Success = false
                       }
            }
            ;

            //check is payment method exists
            var payment = PaymentMethodRepository.Find(accountId);

            if (payment == null)
            {
                return new OpenBookingResponse
                       {
                           Message = "You enter a payment method before booking",
                           Success = false
                       }
            }
            ;

            //sanity check to ensure the vehicle has no other bookings
            var hasOpenVehicleBookings = BookingRepository
                                         .FindByVehicleId(vehicleId)
                                         .Any(x => x.BookingStatus == Constants.BookingOpenStatus);

            if (hasOpenVehicleBookings)
            {
                //update the status of the car to be booked
                car.Status = Constants.CarBookedStatus;

                CarRepository.Update(car);

                return(new OpenBookingResponse
                {
                    Message =
                        $"{car.Make} {car.Model} is not available " +
                        $"to be booked",
                    Success = false
                });
            }

            //sanity check to ensure the account has no other bookings
            var hasOpenAccountBookings = BookingRepository
                                         .FindByAccountId(accountId)
                                         .Any(x => x.BookingStatus == Constants.BookingOpenStatus);

            if (hasOpenAccountBookings)
            {
                return new OpenBookingResponse
                       {
                           Message = "User already has an open vehicle booking",
                           Success = false
                       }
            }
            ;

            var category = car.CarCategory1;

            if (category == null)
            {
                category = CarCategoryRepository.Find(car.CarCategory);

                if (category == null)
                {
                    return new OpenBookingResponse
                           {
                               Message =
                                   "Car has an invalid category and can not " +
                                   "be checked out",
                               Success = false
                           }
                }
                ;
            }

            //create the booking and save
            var booking = new Booking
            {
                VehicleID     = vehicleId,
                AccountID     = accountId,
                BookingStatus = Constants.BookingOpenStatus,
                BillingRate   = category.BillingRate,
                CheckOut      = DateTime.Now,
                CityPickUp    = car.Suburb
            };

            BookingRepository.Add(booking);

            //update the status of the car to be booked
            car.Status = Constants.CarBookedStatus;
            CarRepository.Update(car);

            return(new OpenBookingResponse
            {
                BookingId = booking.BookingID,
                CheckOutTime = booking.CheckOut.ToString(),
                Success = true,
                Message =
                    $"{car.Make} {car.Model} has been booked out " +
                    $"at ${category.BillingRate} per hour"
            });
        }