Exemple #1
0
        public UserModel GetRentingClient(int rentId)
        {
            RentModel currentRent   = new RentModel(DB.Rents.SingleOrDefault(p => p.RentId == rentId));
            UserModel rentingClient = new UserModel(DB.Users.SingleOrDefault(p => p.UserId == currentRent.UserId));

            return(rentingClient);
        }
        public RentModel RentFirstCarAvailable(int carTypeId, RentModel rentModel)
        {
            CarModel rentedCar = new CarModel();
            Car      car       = (DB.Cars.FirstOrDefault(p => p.CarTypeId == carTypeId && !DB.Rents.Any(r => r.CarId == p.CarId)));

            if (car != null)
            {
                rentedCar = new CarModel(car);
            }

            else
            {
                rentedCar = new CarModel(DB.Cars.FirstOrDefault(
                                             p => p.CarTypeId == carTypeId && !(DB.Rents.Any(r =>
                                                                                             ((r.ReturnDate >= rentModel.PickupDate && r.ReturnDate <= rentModel.ReturnDate) ||
                                                                                              (r.PickupDate >= rentModel.PickupDate && r.PickupDate <= rentModel.ReturnDate) ||
                                                                                              (r.PickupDate <= rentModel.PickupDate && r.ReturnDate >= rentModel.ReturnDate)) && r.CarId == p.CarId))));
            }

            if (rentedCar == null)
            {
                return(null);
            }

            rentModel.CarId = rentedCar.CarId;
            rentModel.PracticalReturnDate = null;

            DB.Rents.Add(rentModel.convertToRent());
            DB.SaveChanges();


            return(rentModel);
        }
        public RentModel GetCurrentRentOfCarId(string carId)
        {
            RentModel currentRent = new RentModel(DB.Rents
                                                  .SingleOrDefault(p => p.CarId == carId &&
                                                                   p.PickupDate < DateTime.Now &&
                                                                   p.PracticalReturnDate == null));

            return(currentRent);
        }
Exemple #4
0
        //public List<CarTypeModel> UpdateManyCarTypes(List<CarTypeModel> carTypesToUpdate)
        //{
        //    for (int i = 0; i < carTypesToUpdate.Count; i++)
        //    {
        //        carTypesToUpdate[i] = this.UpdateCarType(carTypesToUpdate[i]);
        //    }

        //    return carTypesToUpdate;
        //}

        public List <CarTypeModel> GetAllAvailableCarTypes(RentModel rentModel)
        {
            List <string> unabailableCarsIds = DB.Rents
                                               .Where(rent =>
                                                      (rent.ReturnDate >= rentModel.PickupDate && rent.ReturnDate <= rentModel.ReturnDate) ||
                                                      (rent.PickupDate >= rentModel.PickupDate && rent.PickupDate <= rentModel.ReturnDate) ||
                                                      (rent.PickupDate <= rentModel.PickupDate && rent.ReturnDate >= rentModel.ReturnDate))
                                               .ToList()
                                               .GroupBy(p => p.CarId)
                                               .Select(p => p.First().CarId)
                                               .ToList();

            List <CarType> availableCarTypes = DB.Cars
                                               .Where(p => !unabailableCarsIds.Contains(p.CarId))
                                               .Join(DB.CarTypes, car => car.CarTypeId, carType => carType.CarTypeId, (car, carType) => carType)
                                               .Select(carType => carType)
                                               .Distinct()
                                               .ToList();

            List <CarTypeModel> carTypeModels = new List <CarTypeModel>();

            foreach (var item in availableCarTypes)
            {
                carTypeModels.Add(new CarTypeModel(item));
            }

            List <Car> carsWithNoAvail = new List <Car>();
            bool       carHasNoAvail;

            List <Car> cars = DB.Cars.ToList();

            foreach (var car in cars)
            {
                carHasNoAvail = true;
                foreach (var rent in DB.Rents)
                {
                    if (rent.CarId == car.CarId)
                    {
                        carHasNoAvail = false;
                        break;
                    }
                }
                if (carHasNoAvail == true)
                {
                    carsWithNoAvail.Add(car);
                }
            }
            foreach (var item in carsWithNoAvail)
            {
                CarTypeModel carTypeToCheck = carTypeModels.SingleOrDefault(p => p.CarTypeId == item.CarTypeId);
                if (carTypeToCheck == null)
                {
                    carTypeModels.Add(new CarTypeModel(DB.CarTypes.SingleOrDefault(p => p.CarTypeId == item.CarTypeId)));
                }
            }
            return(carTypeModels);
        }
        public RentModel UpdateRent(int rentId, RentModel rentModel)
        {
            Rent rentToUpdate = DB.Rents.SingleOrDefault(p => p.RentId == rentId);

            rentToUpdate.PickupDate          = rentModel.PickupDate;
            rentToUpdate.ReturnDate          = rentModel.ReturnDate;
            rentToUpdate.PracticalReturnDate = rentModel.PracticalReturnDate;
            rentToUpdate.FinalPayment        = rentModel.FinalPayment;
            rentToUpdate.UserId = rentModel.UserId;
            DB.SaveChanges();
            return(new RentModel(rentToUpdate));
        }
        public RentModel ReturnCar(int rentId, RentModel rentModel)
        {
            Rent rentToUpdate = DB.Rents.SingleOrDefault(p => p.RentId == rentId);

            if (rentToUpdate == null)
            {
                return(null);
            }
            rentToUpdate.PracticalReturnDate = rentModel.PracticalReturnDate;
            rentToUpdate.FinalPayment        = rentModel.FinalPayment;
            DB.SaveChanges();

            return(rentModel);
        }
 public RentModel RentCar(RentModel rentModel)
 {
     DB.Rents.Add(rentModel.convertToRent());
     DB.SaveChanges();
     return(rentModel);
 }
        public List <CarModel> GetAllAvailableCars(RentModel rentModel)
        {
            //List<Rent> query = DB.Rents
            //    .Where(p =>
            //        !(p.ReturnDate >= rentModel.PickupDate && p.ReturnDate <= rentModel.ReturnDate) ||
            //        !(p.PickupDate >= rentModel.PickupDate && p.PickupDate <= rentModel.ReturnDate) ||
            //        !(p.PickupDate <= rentModel.PickupDate && p.ReturnDate >= rentModel.ReturnDate))
            //    .ToList()
            //    .GroupBy(p => p.CarId)
            //    .Select(p => p.First())
            //    .ToList();

            //List<Car> query2 = query
            //    .Join(DB.Cars, rent => rent.CarId, car => car.CarId, (rent, car) => car)
            //    .Select(car => car)
            //    .Distinct()
            //    .ToList();


            List <Car>  cars          = DB.Cars.ToList();
            List <Rent> rents         = DB.Rents.ToList();
            List <Car>  availableCars = new List <Car>();

            foreach (var car in cars)
            {
                if (!rents.Any(p => p.CarId == car.CarId))
                {
                    availableCars.Add(car);
                    continue;
                }

                if (rents.Any(rent =>
                              rent.CarId == car.CarId &&
                              ((rent.ReturnDate >= rentModel.PickupDate && rent.ReturnDate <= rentModel.ReturnDate) ||
                               (rent.PickupDate >= rentModel.PickupDate && rent.PickupDate <= rentModel.ReturnDate) ||
                               (rent.PickupDate <= rentModel.PickupDate && rent.ReturnDate >= rentModel.ReturnDate))))
                {
                    continue;
                }
                availableCars.Add(car);



                //foreach (var rent in rents)
                //{

                //    if(car.CarId == rent.CarId && car.IsFixed == 1)
                //    {
                //        if (!(rent.ReturnDate >= rentModel.PickupDate && rent.ReturnDate <= rentModel.ReturnDate) ||
                //            !(rent.PickupDate >= rentModel.PickupDate && rent.PickupDate <= rentModel.ReturnDate) ||
                //            !(rent.PickupDate <= rentModel.PickupDate && rent.ReturnDate >= rentModel.ReturnDate))
                //        {
                //            availableCars.Add(car);
                //            break;
                //        }
                //    }
                //}
            }

            List <CarModel> availableCarModels = availableCars.Select(p => new CarModel(p)).ToList();

            foreach (var item in availableCarModels)
            {
                item.Branch = DB.Branches.SingleOrDefault(p => p.BranchId == DB.CarsBranches.SingleOrDefault(p => p.CarId == item.CarId).BranchId).Name;
            }
            return(availableCarModels);
        }