Ejemplo n.º 1
0
        private Model.Drive CreateNewDrive()
        {
            var drive = new Model.Drive();

            _repository.Add(drive);
            return(drive);
        }
Ejemplo n.º 2
0
        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);
        }