Exemplo n.º 1
0
        public async Task <IActionResult> GetBuildingEmployees([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = new GenericResult <GoogleDistanceMatrixApi.Response>();

            try
            {
                var Url = _serviceSettings.Value.GMUrl;
                var Key = _serviceSettings.Value.GMApiKey;
                var inputSourceAddresses = buildingService.GetAsynById(id);
                var source = inputSourceAddresses.Result;
                var inputDestinationAddresses      = employeeService.GetAll();
                var inputDestinationAddressesArray = new List <string>();
                var inputSourceAddressesArray      = new List <string>();
                inputSourceAddressesArray.Add(string.Format("{0}, {1}, {2}, {3}, {4}", source.Address, source.City, source.State, source.Country, source.Zip));

                foreach (var destination in inputDestinationAddresses)
                {
                    inputDestinationAddressesArray.Add(string.Format("{0}, {1}, {2}, {3}, {4}", destination.Address, destination.City, destination.State, destination.Country, destination.Zip));
                }

                GoogleDistanceMatrixApi api = new GoogleDistanceMatrixApi(Url, Key, inputSourceAddressesArray.ToArray(), inputDestinationAddressesArray.ToArray());
                var response = await api.GetResponse();

                var originAdresses      = response.OriginAddresses.ToList();
                var destinationAdresses = response.DestinationAddresses.ToList();
                var rows = response.Rows.ToList();
                foreach (var row in rows)
                {
                    int i        = rows.IndexOf(row);
                    var elements = row.Elements.ToList();

                    foreach (var element in elements)
                    {
                        int j = elements.IndexOf(element);
                        element.OriginAddress      = originAdresses[i];
                        element.DestinationAddress = destinationAdresses[j];
                    }
                }
                response.Rows  = rows.ToArray();
                result.Result  = response;
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Errors = new string[] { ex.Message };
            }

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

            return(Ok(result));
        }
        public async Task <ActionResult> Get()
        {
            var Url = _serviceSettings.Value.GMUrl;
            var Key = _serviceSettings.Value.GMApiKey;
            var inputSourceAddresses           = buildingService.GetAll();
            var inputDestinationAddresses      = employeeService.GetAll();
            var inputDestinationAddressesArray = new List <string>();
            var inputSourceAddressesArray      = new List <string>();

            foreach (var source in inputSourceAddresses)
            {
                inputSourceAddressesArray.Add(string.Format("{0}, {1}, {2}, {3}, {4}", source.Address, source.City, source.State, source.Country, source.Zip));
            }

            foreach (var destination in inputDestinationAddresses)
            {
                inputDestinationAddressesArray.Add(string.Format("{0}, {1}, {2}, {3}, {4}", destination.Address, destination.City, destination.State, destination.Country, destination.Zip));
            }

            GoogleDistanceMatrixApi api = new GoogleDistanceMatrixApi(Url, Key, inputSourceAddressesArray.ToArray(), inputDestinationAddressesArray.ToArray());
            var response = await api.GetResponse();

            var originAdresses      = response.OriginAddresses.ToList();
            var destinationAdresses = response.DestinationAddresses.ToList();
            var rows = response.Rows.ToList();

            foreach (var row in rows)
            {
                int i        = rows.IndexOf(row);
                var elements = row.Elements.ToList();

                foreach (var element in elements)
                {
                    int j = elements.IndexOf(element);
                    element.OriginAddress      = originAdresses[i];
                    element.DestinationAddress = destinationAdresses[j];
                }
            }
            response.Rows = rows.ToArray();
            return(Json(response));
        }