コード例 #1
0
 /**
  * Method responsible for converting a Domain model to a Response model for the View layer
  * As this project is a small scale one, ScooterResponseModel is the same as ScooterDomainModel.
  * In larger scale projects, the Response model may be very different compared to the Domain model.
  */
 private ScooterResponseModel ToScooterResponse(ScooterDomainModel scooterDomainModel)
 {
     return(new ScooterResponseModel(
                scooterDomainModel.Id,
                scooterDomainModel.Latitude,
                scooterDomainModel.Longitude));
 }
コード例 #2
0
 /**
  * Method responsible for converting a Domain model to a Data model.
  * Data models are used for communication between the Repository and the Databases (through ORM)
  * Domain models are used for communication between the Repository and the Domain layer.
  */
 private ScooterDataModel ToScooterDataModel(ScooterDomainModel scooterDomainModel)
 {
     return(new ScooterDataModel
     {
         Id = scooterDomainModel.Id,
         Latitude = scooterDomainModel.Latitude,
         Longitude = scooterDomainModel.Longitude
     });
 }
コード例 #3
0
        /**
         * Create operation
         * One of the 4 main operations under CRUD
         * Adds a new Scooter object to the database
         */
        public async Task <ScooterDomainModel> Add(ScooterDomainModel scooterDomainModel)
        {
            ScooterDataModel scooterDataModel = ToScooterDataModel(scooterDomainModel);

            await _databaseContext.AddAsync(scooterDataModel);

            await _databaseContext.SaveChangesAsync();

            return(await Get(scooterDomainModel.Id));
        }
コード例 #4
0
        public async Task <ActionResult <ScooterDomainModel> > Post([FromBody] ScooterRequestModel requestModel)
        {
            ScooterDomainModel scooterDomainModel = new ScooterDomainModel(Guid.NewGuid(), requestModel.Latitude, requestModel.Longitude);
            var result = await _scooterService.Add(scooterDomainModel);

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

            return(Created(Request.Path.Value + "/" + result.Id, result));
        }
コード例 #5
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);
        }
コード例 #6
0
 public Task <ScooterDomainModel> Add(ScooterDomainModel scooterDomainModel)
 {
     return(_repository.Add(scooterDomainModel));
 }