コード例 #1
0
        /**
         * Method responsible for converting a ClosestRequestModel to a ClosetDomainModel for
         * domain logic in the Services.
         * As this project is small scale, ClosestDomainModel is similar to ClosestRequestModel.
         */
        private ScooterClosestDomainModel ToScooterClosestDomainModel(
            ScooterClosestRequestModel scooterClosestRequestModel)
        {
            ScooterClosestDomainModel scooterClosestDomainModel = new ScooterClosestDomainModel(
                scooterClosestRequestModel.NearestNumberOfScooters,
                scooterClosestRequestModel.Radius,
                scooterClosestRequestModel.ChosenLatitude,
                scooterClosestRequestModel.ChosenLongitude);

            return(scooterClosestDomainModel);
        }
コード例 #2
0
        public ActionResult <IEnumerable <ScooterClosestDomainModel> > GetClosest([FromBody] ScooterClosestRequestModel scooterClosestRequestModel)
        {
            ScooterClosestDomainModel scooterClosestDomainModel = ToScooterClosestDomainModel(scooterClosestRequestModel);
            var result = _scooterService.GetClosest(scooterClosestDomainModel);

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

            return(Ok(result.Select(ToScooterResponse)));
        }
コード例 #3
0
        /**
         * Domain logic responsible for calculating distance and finding the closest few scooters to a given coordinate.
         * First, we get all Scooters that's available in the database at the current moment.
         * Next, we iterate through all the Scooters available and calculate their distance.
         * If the distance is smaller than the specified distance (termed radius),
         * the Scooter will be added to a List holding all nearest scooters.
         * At the same time, their distance and UUID will be saved in a List of Tuples, which will be sorted.
         * We will then find the UUID of scooters from the shortest distance first and add these Scooters to another
         * list (called constrainNearestScooters). constrainNearestScooters will then hold a
         * specific number of closest scooters to the given point and will be returned by this function.
         */
        public IEnumerable <ScooterDomainModel> GetClosest(ScooterClosestDomainModel scooterClosestDomainModel)
        {
            Coordinates centreCoordinate        = scooterClosestDomainModel.CentreCoordinate;
            int         nearestNumberOfScooters = scooterClosestDomainModel.NearestNumberOfScooters;
            int         radius = scooterClosestDomainModel.Radius;

            IEnumerable <ScooterDomainModel> allScooters                  = _repository.GetAll().Result;
            List <ScooterDomainModel>        allNearestScooters           = new List <ScooterDomainModel>();
            List <ScooterDomainModel>        constrainNearestScooters     = new List <ScooterDomainModel>();
            List <Tuple <double, Guid> >     distanceOfAllNearestScooters = new List <Tuple <double, Guid> >();

            foreach (ScooterDomainModel scooter in allScooters)
            {
                double distance = CoordinatesDistanceExtensions.GetDistance(
                    scooter.Longitude,
                    scooter.Latitude,
                    centreCoordinate.Longitude,
                    centreCoordinate.Latitude);
                if (!(distance <= radius))
                {
                    continue;
                }
                allNearestScooters.Add(scooter);
                distanceOfAllNearestScooters.Add(new Tuple <double, Guid>(distance, scooter.Id));
            }

            distanceOfAllNearestScooters.Sort((x, y)
                                              => x.Item1.CompareTo(y.Item1));

            while (nearestNumberOfScooters > 0 && distanceOfAllNearestScooters.Any())
            {
                var initialElement = distanceOfAllNearestScooters.First();
                ScooterDomainModel desiredScooter = allNearestScooters.Find(model =>
                                                                            model.Id.Equals(initialElement.Item2));
                constrainNearestScooters.Add(desiredScooter);
                nearestNumberOfScooters--;
                distanceOfAllNearestScooters.Remove(initialElement);
            }
            return(constrainNearestScooters);
        }