Exemple #1
0
        private Route GetOfferRoute(OfferModel offer, PointOfInterest pointOfInterest)
        {
            var route = _routeFinder.GetRoute(offer.Coordinates, pointOfInterest.Coordinates);

            if (_routeValidator.IsNotAcceptable(route, pointOfInterest))
            {
                return(null);
            }

            return(route);
        }
        public ActionResult <Drive> Create([FromBody] AddDriveModel addDriveModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var driverUserAccount = _userAccountRepository.GetSingle(x => x.Id == addDriveModel.DriverUserId);

            if (driverUserAccount == null)
            {
                return(BadRequest());
            }

            var passengers = new List <UserAccount>();

            foreach (var userId in addDriveModel.PassengerUserIds)
            {
                var userAccount = _userAccountRepository.GetSingle(x => x.Id == userId);
                if (userAccount == null)
                {
                    return(BadRequest());
                }

                passengers.Add(userAccount);
            }

            var userGroup = _userGroupRepository.GetSingle(x => x.Id == addDriveModel.UserGroupId);

            if (userGroup == null)
            {
                return(BadRequest());
            }

            var route = _routeFinder.GetRoute(addDriveModel.Locations.Select(Location.FromString));

            _routeParser.LoadRoute(route);

            var drive = new Drive
            {
                Driver            = driverUserAccount,
                Passengers        = passengers,
                Group             = userGroup,
                DistanceInKm      = _routeParser.DistanceInKm,
                DurationInSeconds = _routeParser.DurationInSeconds,
                Timestamp         = addDriveModel.Timestamp
            };

            _driveRepository.Add(drive);
            _driveRepository.Commit();
            return(drive);
        }