Beispiel #1
0
        public async Task <List <Driver> > GetInRange(double latitude, double longitude, int km)
        {
            var path    = $"{_baseUri}/GetInRange";
            var postDto = new DriverInRangeDto()
            {
                Latitude        = latitude,
                Longitude       = longitude,
                MaxDistanceInKm = km
            };

            try
            {
                var driverDtos = await WebApiClient.PostCallApi <List <DriverDto>, DriverInRangeDto>(path, postDto);

                if (driverDtos == null)
                {
                    return(null);
                }

                var drivers = _mapper.ProjectTo <Driver>(driverDtos.AsQueryable()).ToList();

                return(drivers);
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }
        }
Beispiel #2
0
        public async void Post_GetInRange_InvalidModel_ReturnsBadRequest()
        {
            var path = $"{BaseUri}/GetInRange";
            var dto  = new DriverInRangeDto()
            {
                Latitude        = 51.0862,
                DriverId        = "00000000-0000-0000-0000-000000000002",
                MaxDistanceInKm = 10
            };
            var json = JsonConvert.SerializeObject(dto);
            var body = new StringContent(json, Encoding.UTF8, "application/json");

            var response = await _client.PostAsync(path, body);

            var content = await response.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            Assert.Equal("Invalid model", content);
        }
Beispiel #3
0
        public async void Post_GetInRange_ValidModel_ReturnsDrivers()
        {
            var path = $"{BaseUri}/GetInRange";
            var dto  = new DriverInRangeDto()
            {
                Latitude        = 51.0862,
                Longitude       = 2.9764,
                DriverId        = "00000000-0000-0000-0000-000000000002",
                MaxDistanceInKm = 10
            };
            var json = JsonConvert.SerializeObject(dto);
            var body = new StringContent(json, Encoding.UTF8, "application/json");

            var response = await _client.PostAsync(path, body);

            var content = await response.Content.ReadAsStringAsync();

            var drivers = JsonConvert.DeserializeObject <List <DriverDto> >(content);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(2, drivers.Count);
        }
        public async Task <IActionResult> GetInRange([FromBody] DriverInRangeDto model)
        {
            var availableDrivers = await _mappingRepository
                                   .GetFiltered(d => d.IsActive)
                                   .ToListAsync();

            if (availableDrivers == null)
            {
                return(BadRequest("No drivers found"));
            }

            if (model.Latitude == 0 || model.Longitude == 0)
            {
                return(BadRequest("Invalid model"));
            }

            var customerLocation = new Location()
            {
                Latitude  = model.Latitude,
                Longitude = model.Longitude
            };

            var driverDtos = availableDrivers.Where(d =>
            {
                var driverLocation = new Location()
                {
                    Latitude  = d.Latitude,
                    Longitude = d.Longitude
                };

                var distance = customerLocation.CalculateDistance(driverLocation, DistanceUnits.Kilometers);

                return(distance <= model.MaxDistanceInKm);
            }).ToList();

            return(Ok(driverDtos));
        }
Beispiel #5
0
 public async Task SendLocation(DriverInRangeDto dto)
 {
     await Clients.All.SendAsync("ReceiveLocation", dto);
 }