예제 #1
0
        private RentRequestResponse RentCars(IEnumerable <Car> cars, RentRequest request)
        {
            RentRequestResponse response = new RentRequestResponse();

            foreach (Car car in cars)
            {
                RentCar(car, request.CompanyId.Value, request.Days);

                request.Company.BonusPoints += car.BonusPointsPerRental;

                response.CarResults.Add(new RentCarResponse()
                {
                    Status    = RentCarStatus.Succeded,
                    Message   = $"CarId {car.Id} rented successfully by companyId {request.CompanyId}.",
                    RentPrice = car.PricePerDay * request.Days
                });
            }

            response.Status             = RentCarResponseStatus.Succeded;
            request.Status              = RentCarResponseStatus.Succeded;
            request.StatusMessage       = "Rental request Succeded!";
            request.Company.UpdatedDate = DateTime.Now;
            _unitOfWork.Companies.Update(request.Company);
            return(response);
        }
예제 #2
0
        public async Task <RentRequestResponse> CreateRentAsync(RentRequest request)
        {
            RentRequestResponse response = new RentRequestResponse();

            IEnumerable <Car> cars = await _fleetService.GetAsync(x => request.CarIds.Contains(x.Id));

            IEnumerable <Car> alreadyRentedCars = cars.Where(x => x.Rented);

            request.Company = await _unitOfWork.Companies.GetByIDAsync(request.CompanyId);

            if (request.Company == null)
            {
                response.Status  = RentCarResponseStatus.Failed;
                response.Message = $"Company with Id {request.CompanyId} does not exists.";
                return(response);
            }
            try
            {
                if (alreadyRentedCars.Any())
                {
                    request.Status        = RentCarResponseStatus.Failed;
                    request.StatusMessage = $"Some of the cars are already rented. {Environment.NewLine} {string.Join(',', alreadyRentedCars.Select(x => x.Id + Environment.NewLine))}";

                    response.Status  = RentCarResponseStatus.Failed;
                    response.Message = $"Cars provided cannot be rented. The following cars are already rented, remove them from the request: {Environment.NewLine} {string.Join(Environment.NewLine, alreadyRentedCars.Select(x => x.Id + Environment.NewLine))}";
                }
                else if (cars.Any())
                {
                    response = RentCars(cars, request);
                }
                else
                {
                    request.Status        = RentCarResponseStatus.Failed;
                    request.StatusMessage = $"0 carIds provided.";
                    response.Status       = RentCarResponseStatus.Failed;
                    response.Message      = $"Please provide atleast one(1) carId in the rentalRequest.";
                }
            }
            catch (Exception ex)
            {
                request.Status        = RentCarResponseStatus.Failed;
                request.StatusMessage = $"Some error ocurred while processing the rental request.Rollback executed {Environment.NewLine}{ex.ToString()}";
                response.Status       = RentCarResponseStatus.Failed;
                response.Message      = $"Some error ocurred while processing the rental request {ex.Message}";
                await _unitOfWork.RentalRequests.InsertAsync(request);

                _unitOfWork.Rollback();
            }
            finally
            {
                await _unitOfWork.RentalRequests.InsertAsync(request);

                _unitOfWork.Commit();
            }
            return(response);
        }
예제 #3
0
        public async Task <IActionResult> CreateAsync([FromBody] IEnumerable <WebAPI.Contracts.v1.Requests.RentRequest> rentRequests)
        {
            List <RentRequestResponse> responses = new List <RentRequestResponse>();

            if (!rentRequests.Any())
            {
                return(BadRequest(new BadRequestObjectResult($"Please provide atleast one RentalRequest.")));
            }

            foreach (WebAPI.Contracts.v1.Requests.RentRequest request in rentRequests)
            {
                RentRequestResponse response = _mapper.Map <RentRequestResponse>(await _rentalService.CreateRentAsync(_mapper.Map <Domain.Requests.RentRequest>(request)));
                responses.Add(response);
            }

            return(Ok(new Response <List <RentRequestResponse> >(responses)));
        }