public bool UpdateRoute(Route route)
        {
            try
            {
                #region Validations
                List <Route> routes = new RouteRepository().GetAllRoutes().ToList();
                if (!routes.Exists(r => r.IdSource == route.IdSource && r.IdDestination == route.IdDestination))
                {
                    return(false);
                }
                #endregion Validations

                #region Cache Storage
                if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE")
                {
                    var ctx = HttpContext.Current;
                    if (ctx != null)
                    {
                        try
                        {
                            var currentData = ((Route[])ctx.Cache[CacheKey]).ToList();
                            if (currentData.Exists(p => p.IdSource == route.IdSource && p.IdDestination == route.IdDestination))
                            {
                                if (currentData[currentData.FindIndex(p => p.IdSource == route.IdSource && p.IdDestination == route.IdDestination)].RouteCost != route.RouteCost && route.RouteCost > 0)
                                {
                                    currentData[currentData.FindIndex(p => p.IdSource == route.IdSource && p.IdDestination == route.IdDestination)].RouteCost = route.RouteCost;
                                }
                                if (currentData[currentData.FindIndex(p => p.IdSource == route.IdSource && p.IdDestination == route.IdDestination)].RouteTime != route.RouteTime && route.RouteTime > 0)
                                {
                                    currentData[currentData.FindIndex(p => p.IdSource == route.IdSource && p.IdDestination == route.IdDestination)].RouteTime = route.RouteTime;
                                }
                            }
                            ctx.Cache[CacheKey] = currentData.ToArray();

                            return(true);
                        }
                        catch (Exception ex)
                        {
                            new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                            return(false);
                        }
                    }
                }
                #endregion
                #region DB Storage MySQl
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL")
                {
                    try
                    {
                        var dbCon = DBConnection.Instance();
                        if (dbCon.IsConnect())
                        {
                            if (!string.IsNullOrEmpty(route.RouteCost.ToString()))
                            {
                                if (route.RouteCost > 0)
                                {
                                    string query  = "UPDATE `ROUTES` SET `RouteCost`=" + route.RouteCost + "ss WHERE IdSource=" + route.IdSource + " AND IdDestiny = " + route.IdDestination;
                                    var    cmd    = new MySqlCommand(query, dbCon.Connection);
                                    var    reader = cmd.ExecuteNonQuery();
                                }
                            }
                            if (!string.IsNullOrEmpty(route.RouteTime.ToString()))
                            {
                                if (route.RouteTime > 0)
                                {
                                    string query  = "UPDATE `ROUTES` SET `RouteTime`=" + route.RouteTime + " WHERE IdSource=" + route.IdSource + " AND IdDestiny = " + route.IdDestination;
                                    var    cmd    = new MySqlCommand(query, dbCon.Connection);
                                    var    reader = cmd.ExecuteNonQuery();
                                }
                            }
                            dbCon.Close();
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                        return(false);
                    }
                }
                #endregion DB Storage MySQl
                #region DB Storage Mongo
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MONGO")
                {
                    var dbCon = DBConnectionMongo.Instance();
                    MongoDB.Driver.MongoClient client = dbCon.GetClient();
                    client.StartSession();
                    var collection = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Route>("ROUTES");

                    if (!string.IsNullOrEmpty(route.RouteCost.ToString()))
                    {
                        if (route.RouteCost > 0)
                        {
                            var updoneresult = collection.UpdateOneAsync(Builders <Route> .Filter.And(
                                                                             Builders <Route> .Filter.Eq("IdSource", route.IdSource),
                                                                             Builders <Route> .Filter.Eq("IdDestination", route.IdDestination)), Builders <Route> .Update.Set("RouteCost", route.RouteCost));
                        }
                    }
                    if (!string.IsNullOrEmpty(route.RouteTime.ToString()))
                    {
                        if (route.RouteTime > 0)
                        {
                            var updoneresult = collection.UpdateOneAsync(Builders <Route> .Filter.And(
                                                                             Builders <Route> .Filter.Eq("IdSource", route.IdSource),
                                                                             Builders <Route> .Filter.Eq("IdDestination", route.IdDestination)), Builders <Route> .Update.Set("RouteTime", route.RouteTime));
                        }
                    }
                    return(true);
                }
                #endregion DB Storage Mongo
            }
            catch (Exception ex)
            {
                new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
            }
            return(false);
        }
        public bool SaveRoute(Route route)
        {
            try
            {
                #region Validations
                List <Point> points = new PointRepository().GetAllPoints().ToList();
                if (!points.Exists(p => p.Id == route.IdSource))
                {
                    return(false);
                }
                else if (!points.Exists(p => p.Id == route.IdDestination))
                {
                    return(false);
                }
                List <Route> routes = new RouteRepository().GetAllRoutes().ToList();
                if (routes.Exists(r => r.IdSource == route.IdSource && r.IdDestination == route.IdDestination))
                {
                    return(false);
                }
                #endregion Validations

                #region Cache Storage
                if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE")
                {
                    var ctx = HttpContext.Current;
                    if (ctx != null)
                    {
                        try
                        {
                            var currentData = ((Route[])ctx.Cache[CacheKey]).ToList();
                            currentData.Add(route);
                            ctx.Cache[CacheKey] = currentData.ToArray();

                            return(true);
                        }
                        catch (Exception ex)
                        {
                            new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                            return(false);
                        }
                    }
                }
                #endregion
                #region DB Storage MySQl
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL")
                {
                    try
                    {
                        var dbCon = DBConnection.Instance();
                        if (dbCon.IsConnect())
                        {
                            //suppose col0 and col1 are defined as VARCHAR in the DB
                            string query  = "INSERT INTO `ROUTES`(`IdSource`, `IdDestination`, `RouteCost`, `RouteTime`) VALUES  (" + route.IdSource + "," + route.IdDestination + "," + route.RouteCost + "," + route.RouteTime + ")";
                            var    cmd    = new MySqlCommand(query, dbCon.Connection);
                            var    reader = cmd.ExecuteNonQuery();
                            dbCon.Close();
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                        return(false);
                    }
                }
                #endregion DB Storage MySQl
                #region  DB Storage Mongo
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MONGO")
                {
                    var dbCon = DBConnectionMongo.Instance();
                    MongoDB.Driver.MongoClient client = dbCon.GetClient();
                    client.StartSession();
                    var collection = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Route>("ROUTES");
                    collection.InsertOne(route);
                    return(true);
                }
                #endregion  DB Storage Mongo
            }
            catch (Exception ex)
            {
                new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
            }
            return(false);
        }
        public bool DeleteRoute(Route route)
        {
            try
            {
                #region Validations
                List <Route> routes = new RouteRepository().GetAllRoutes().ToList();
                if (!routes.Exists(r => r.IdSource == route.IdSource && r.IdDestination == route.IdDestination))
                {
                    return(false);
                }
                #endregion Validations

                #region Cache Storage
                if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE")
                {
                    var ctx = HttpContext.Current;
                    if (ctx != null)
                    {
                        try
                        {
                            var currentData = ((Route[])ctx.Cache[CacheKey]).ToList();
                            if (currentData.Exists(p => p.IdSource == route.IdSource && p.IdDestination == route.IdDestination))
                            {
                                currentData.RemoveAt(currentData.FindIndex(p => p.IdSource == route.IdSource && p.IdDestination == route.IdDestination));
                            }
                            ctx.Cache[CacheKey] = currentData.ToArray();

                            return(true);
                        }
                        catch (Exception ex)
                        {
                            new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                            return(false);
                        }
                    }
                }
                #endregion
                #region DB Storage MySQl
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL")
                {
                    try
                    {
                        var dbCon = DBConnection.Instance();
                        if (dbCon.IsConnect())
                        {
                            //suppose col0 and col1 are defined as VARCHAR in the DB
                            string query  = "DELETE FROM `ROUTES` WHERE IdSource = " + route.IdSource + " AND IdDestiny = " + route.IdDestination;
                            var    cmd    = new MySqlCommand(query, dbCon.Connection);
                            var    reader = cmd.ExecuteNonQuery();
                            dbCon.Close();
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                        return(false);
                    }
                }
                #endregion DB Storage MySQl
                #region DB Storage Mongo
                else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MONGO")
                {
                    var dbCon = DBConnectionMongo.Instance();
                    MongoDB.Driver.MongoClient client = dbCon.GetClient();
                    client.StartSession();
                    var collection = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Route>("ROUTES");
                    var Deleteone  = collection.DeleteOneAsync(Builders <Route> .Filter.And(
                                                                   Builders <Route> .Filter.Eq("IdSource", route.IdSource),
                                                                   Builders <Route> .Filter.Eq("IdDestination", route.IdDestination)));
                    return(true);
                }
                #endregion DB Storage Mongo
            }
            catch (Exception ex)
            {
                new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
            }
            return(false);
        }
        public Delivery GetDelivery(Delivery delivery)
        {
            try
            {
                List <Route> currentDataRoute = new RouteRepository().GetAllRoutes().ToList();
                List <Point> currentDataPoint = new PointRepository().GetAllPoints().ToList();

                #region Validations
                if (!currentDataPoint.Exists(p => p.Id == delivery.IdSource))
                {
                    delivery.BestRoute = "No source point found in points repository.";
                    return(delivery);
                }
                else if (!currentDataPoint.Exists(p => p.Id == delivery.IdDestination))
                {
                    delivery.BestRoute = "No destimatiom point found in points repository.";
                    return(delivery);
                }
                else if (!currentDataPoint.Exists(p => p.Id == delivery.IdDestination))
                {
                    delivery.BestRoute = "No destimatiom point found in points repository.";
                    return(delivery);
                }

                if (string.IsNullOrEmpty(delivery.Type))
                {
                    delivery.Type = ConfigurationManager.AppSettings["dbDefaultSearchType"].ToUpper();
                }
                else if (!ConfigurationManager.AppSettings["dbSearchTypes"].Split(';').ToList().Exists(a => a.ToUpper() == delivery.Type.ToUpper()))
                {
                    delivery.Type = ConfigurationManager.AppSettings["dbDefaultSearchType"].ToUpper();
                }
                #endregion Validations

                #region Build Graph
                currentDataRoute.RemoveAll(r => r.IdSource == delivery.IdSource && r.IdDestination == delivery.IdDestination);

                int[,] graph = new int[currentDataPoint.Count(), currentDataPoint.Count()];
                int x = 0;
                foreach (Point pointX in currentDataPoint)
                {
                    int y = 0;
                    foreach (Point pointY in currentDataPoint)
                    {
                        if (currentDataRoute.Exists(r => r.IdSource == pointX.Id && r.IdDestination == pointY.Id))
                        {
                            switch (delivery.Type.ToUpper())
                            {
                            case "COST":
                                graph[x, y] = currentDataRoute.FirstOrDefault(r => r.IdSource == pointX.Id && r.IdDestination == pointY.Id).RouteCost;
                                break;

                            case "TIME":
                                graph[x, y] = currentDataRoute.FirstOrDefault(r => r.IdSource == pointX.Id && r.IdDestination == pointY.Id).RouteTime;
                                break;
                            }
                        }
                        else
                        {
                            graph[x, y] = 0;
                        }
                        y++;
                    }
                    x++;
                }
                #endregion Build Graph

                #region Get Short Path

                var path = Dijkstra.DijkstraAlgorithm(graph,
                                                      currentDataPoint.FindIndex(r => r.Id == delivery.IdSource),
                                                      currentDataPoint.FindIndex(r => r.Id == delivery.IdDestination)
                                                      );

                string formattedPath = "";
                if (path == null)
                {
                    formattedPath = "No path";
                }
                else
                {
                    int pathLength = 0;
                    for (int i = 0; i < path.Count - 1; i++)
                    {
                        pathLength += graph[path[i], path[i + 1]];
                    }

                    for (int i = 0; i < path.Count; i++)
                    {
                        formattedPath += "(" + currentDataPoint[path[i]].Id + " - " + currentDataPoint[path[i]].Name + ")";
                        if (i < path.Count - 1)
                        {
                            formattedPath += " -> ";
                        }
                    }
                    formattedPath += " - Total cost in " + delivery.Type + ": " + pathLength;
                }
                #endregion

                delivery.BestRoute = formattedPath;

                return(delivery);
            }
            catch (Exception ex)
            {
                new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message);
                return(null);
            }
            return(delivery);
        }