Example #1
0
        /// <summary>
        /// Function takes the rental/booking number,
        /// day the car returns
        /// and what the meter stands on when returning the car
        /// </summary>
        /// <param name="id"></param>
        /// <param name="endOfRent"></param>
        /// <param name="endOfCurrentMeter"></param>
        /// <returns>The rent object</returns>
        public async Task <Rent> ReturnAsync(int id, DateTime endOfRent, int endOfCurrentMeter)
        {
            Rent rent = await _rentRepository.GetByIdAsync(id);

            if (DateTime.Compare(rent.StartOfRent, endOfRent) > 0)
            {
                throw new ArgumentException("RentService::ReturnAsync Invalid date, end date is before start date");
            }

            if (rent.StartOfCurrentMeter > endOfCurrentMeter)
            {
                throw new ArgumentException("RentService::ReturnAsync Current meter is below starting meter");
            }

            rent.EndOfRent         = endOfRent;
            rent.EndofCurrentMeter = endOfCurrentMeter;
            Price price = await _priceRepository.GetPriceByCarCategoryAsync(rent.CarCategory);

            rent.Price = rent.CalculateRentalPrice(price.PerDay, price.PerKm);
            await _rentRepository.UpdateAsync(rent);

            return(rent);
        }