Example #1
0
        public string GetPath([FromBody] string array)
        {
            var strArray   = array.Split(','); // create string array
            var numberList = new List <int>();
            var path       = new List <int>();
            var results    = "";

            try
            {
                strArray.ToList().ForEach(x => numberList.Add(Convert.ToInt32(x))); // convert array into int
                var pathFromCache = _cacheService.GetCacheByArray(numberList.ToArray());

                if (pathFromCache == null)
                {
                    path = _arrayService.StorePath(_arrayService.GetReach(numberList.ToArray()));
                    if (!_arrayService.Failure) // don't save to cache if it's unreachable
                    {
                        _cacheService.AddCacheToRepository(numberList.ToArray(), path);
                    }
                }

                else
                {
                    path = pathFromCache;
                }

                results = _outputService.PrepareOutput(numberList.ToArray(), path, _arrayService.Failure);
                _arrayService.Failure = false;
            }

            catch
            {
                Console.WriteLine("Error");
            }

            return(results);
        }