Example #1
0
        public IHttpActionResult CreateJourney([FromBody] JourneyForCreationDto JourneyFromBody, int userId, int carId)
        {
            try
            {
                if (JourneyFromBody == null)
                {
                    return(BadRequest());
                }

                var car = _carRepository.GetCarForUser(userId, carId, false);

                if (car == null)
                {
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var routes = Mapper.Map <IEnumerable <PassengerRoute> >(JourneyFromBody.Routes);

                if (!_passengerRouteRepository.UsersExistForRoutes(routes))
                {
                    return(BadRequest());
                }

                if (_passengerRouteService.SameUserForRoutes(routes))
                {
                    return(BadRequest());
                }

                _passengerRouteRepository.SetUsersForRoutes(routes);

                var journey = Mapper.Map <Journey>(JourneyFromBody);

                journey.PassengerRoutes.AddRange(routes);

                _journeyService.GiveTotalPrices(journey, (double)JourneyFromBody.PriceForLiter, car.FuelConsumption100km);

                var payments = _paymentService.GeneratePayments(journey, userId);

                _userService.EditUsersPaymentStatistics(payments, userId);

                //notification service albo repozytorium wysle notyfikacje

                _journeyRepository.AddJourneyForUserAndCar(userId, carId, journey);

                _paymentRepository.AddPayments(payments);

                if (!_appRepository.Commit())
                {
                    return(InternalServerError());
                }

                var journeyToReturn = Mapper.Map <JourneyDto>(journey);

                return(CreatedAtRoute("GetJourneyForUserAndCar", new { userId = userId, id = journey.Id, passengerRoutes = true, carId = carId }, journeyToReturn));
            }
            catch (Exception e)
            {
                return(InternalServerError());
            }
        }