public void IsCarCurrentlyRented_any_account()
        {
            Rental rental = new Rental() {
                CarId = 1
            };

            Mock<IRentalRepository> mockRentalRepository = new Mock<IRentalRepository>();
            mockRentalRepository.Setup(obj => obj.GetCurrentRentalByCar(1)).Returns(rental);

            Mock<IDataRepositoryFactory> mockRepositoryFactory = new Mock<IDataRepositoryFactory>();
            mockRepositoryFactory.Setup(obj => obj.GetDataRepository<IRentalRepository>()).Returns(mockRentalRepository.Object);

            CarRentalEngine engine = new CarRentalEngine(mockRepositoryFactory.Object);

            bool try1 = engine.IsCarCurrentlyRented(2);
            bool try2 = engine.IsCarCurrentlyRented(1);

            Assert.IsFalse(try1);
            Assert.IsTrue(try2);
        }
Ejemplo n.º 2
0
        public Rental RentCarToCustomer(string loginEmail, int carId, DateTime rentalDate, DateTime dateDueBack)
        {
            if (rentalDate > DateTime.Now)
                throw new UnableToRentForDateException(string.Format("Cannot rent for date {0} yet", rentalDate.ToShortDateString()));

            IAccountRepository accountRepository = _DataRepositoryFactory.GetDataRepository<IAccountRepository>();
            IRentalRepository rentalRepository = _DataRepositoryFactory.GetDataRepository<IRentalRepository>();

            bool carIsRented = IsCarCurrentlyRented(carId);
            if (carIsRented)
                throw new CarCurrentlyRentedException(string.Format("Car {0} is already rented", carId));

            Account account = accountRepository.GetByLogin(loginEmail);
            if (account == null)
                throw new NotFoundException(string.Format("No account found for login {0}", loginEmail));

            Rental rental = new Rental()
            {
                AccountId = account.AccountId,
                CarId = carId,
                DateRented = rentalDate,
                DateDue = dateDueBack
            };

            Rental savedEntity = rentalRepository.Add(rental);

            return savedEntity;
        }
Ejemplo n.º 3
0
        public Rental RentCarToCustomer(string loginEmail, int carId, DateTime rentalDate, DateTime dateDueBack)
        {
            return ExecuteFaultHandledOperation(() =>
            {
                if (rentalDate > DateTime.Now)
                {
                    UnableToRentForDateException ex = new UnableToRentForDateException(string.Format("Cannot rent for date {0} yet.", rentalDate.ToShortDateString()));
                    throw new FaultException<UnableToRentForDateException>(ex, ex.Message);
                }

                IAccountRepository accountRepository = _DataRepositoryFactory.GetDataRepository<IAccountRepository>();
                IRentalRepository rentalRepository = _DataRepositoryFactory.GetDataRepository<IRentalRepository>();
                ICarRentalEngine carRentalEngine = _BusinessEngineFactory.GetBusinessEngine<ICarRentalEngine>();

                bool carIsRented = carRentalEngine.IsCarCurrentlyRented(carId);
                if (carIsRented)
                {
                    CarCurrentlyRentedException ex = new CarCurrentlyRentedException(string.Format("Car {0} is already rented.", carId));
                    throw new FaultException<CarCurrentlyRentedException>(ex, ex.Message);
                }

                Account account = accountRepository.GetByLogin(loginEmail);
                if (account == null)
                {
                    NotFoundException ex = new NotFoundException(string.Format("No account found for login '{0}'.", loginEmail));
                    throw new FaultException<NotFoundException>(ex, ex.Message);
                }

                Rental rental = new Rental()
                {
                    AccountId = account.AccountId,
                    CarId = carId,
                    DateRented = rentalDate,
                    DateDue = dateDueBack
                };

                Rental savedEntity = rentalRepository.Add(rental);

                return savedEntity;
            });
        }
Ejemplo n.º 4
0
        public Rental RentCarToCustomer(string loginEmail, int carId, DateTime rentalDate, DateTime dateDueBack)
        {
            if (rentalDate > DateTime.Now)
            {
                throw new UnableToRentForDateException(string.Format("Cannot rent for date {0}", rentalDate));
            }

            var accountRepository = _DataRepositoryFactory.GetDataRepository<IAccountRepository>();
            var rentalRepository = _DataRepositoryFactory.GetDataRepository<IRentalRepository>();

            var carIsRented = IsCarCurrentlyRented(carId);

            var account = accountRepository.GetByLogin(loginEmail);
            if (account == null)
            {
                throw new NotFoundException(string.Format("no account found for login email {0}", loginEmail));
            }

            var rental = new Rental()
            {
                AccountId = account.AccountId,
                CarId = carId,
                DateRented = rentalDate,
                DateDue = dateDueBack
            };

            var savedEntity = rentalRepository.Add(rental);

            return savedEntity;
        }