コード例 #1
0
        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);
        }
コード例 #2
0
 public RentModel RentCar(RentModel rentModel)
 {
     DB.Rents.Add(rentModel.convertToRent());
     DB.SaveChanges();
     return(rentModel);
 }