public Point[] GetAllPoints() { List <Point> allPoints = new List <Point>(); try { #region Cache Storage if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE") { var ctx = HttpContext.Current; if (ctx != null) { return((Point[])ctx.Cache[CacheKey]); } return(new Point[] { new Point { Id = 0, Name = "Placeholder" } }); } #endregion #region DB Storage MySQl else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL") { var dbCon = DBConnection.Instance(); if (dbCon.IsConnect()) { //suppose col0 and col1 are defined as VARCHAR in the DB string query = "SELECT Id, Name FROM POINTS"; var cmd = new MySqlCommand(query, dbCon.Connection); var reader = cmd.ExecuteReader(); while (reader.Read()) { Point point = new Point(); point.Id = reader.GetInt32(0); point.Name = reader.GetString(1); allPoints.Add(point); } dbCon.Close(); } } #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 <Point>("POINTS"); allPoints = collection.Find(_ => true).ToList(); } #endregion DB Storage Mongo } catch (Exception ex) { new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message); } return(allPoints.ToArray()); }
public bool DeletePoint(Point point) { try { #region Validations List <Point> points = new PointRepository().GetAllPoints().ToList(); if (!points.Exists(p => p.Id == point.Id)) { return(false); } #endregion Validations #region Cache Storage if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE") { var ctx = HttpContext.Current; if (ctx != null) { try { var currentData = ((Point[])ctx.Cache[CacheKey]).ToList(); if (currentData.Exists(p => p.Id == point.Id)) { currentData.RemoveAt(currentData.FindIndex(p => p.Id == point.Id)); } ctx.Cache[CacheKey] = currentData.ToArray(); return(true); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return(false); } } } #endregion #region DB Storage MySQl else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL") { try { var dbCon = DBConnection.Instance(); if (dbCon.IsConnect()) { string query = "DELETE FROM `ROUTES` WHERE IdSource=" + point.Id + " OR IdDestination = " + point.Id; var cmd = new MySqlCommand(query, dbCon.Connection); var reader = cmd.ExecuteNonQuery(); query = "DELETE FROM `POINTS` WHERE Id=" + point.Id; cmd = new MySqlCommand(query, dbCon.Connection); reader = cmd.ExecuteNonQuery(); dbCon.Close(); if (reader == 1) { return(true); } else { return(false); } } } 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 collectionR = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Route>("ROUTES"); var Deleteall = collectionR.DeleteMany(Builders <Route> .Filter.Or( Builders <Route> .Filter.Eq("IdSource", point.Id), Builders <Route> .Filter.Eq("IdDestination", point.Id))); var collection = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Point>("POINTS"); var Deleteone = collection.DeleteOneAsync(Builders <Point> .Filter.Eq("Id", point.Id)); 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 UpdatePoint(Point point) { try { #region Validations List <Point> points = new PointRepository().GetAllPoints().ToList(); if (!points.Exists(p => p.Id == point.Id)) { return(false); } #endregion Validations #region Cache Storage if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE") { var ctx = HttpContext.Current; if (ctx != null) { try { var currentData = ((Point[])ctx.Cache[CacheKey]).ToList(); if (currentData.Exists(p => p.Id == point.Id)) { currentData[currentData.FindIndex(x => x.Id == point.Id)].Name = point.Name; } 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 = "UPDATE `POINTS` SET `Name`='" + point.Name + "' WHERE Id=" + point.Id; 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 <Point>("POINTS"); var updoneresult = collection.UpdateOneAsync(Builders <Point> .Filter.Eq("Id", point.Id), Builders <Point> .Update.Set("Name", point.Name)); 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 SavePoint(Point point) { try { #region Validations List <Point> points = new PointRepository().GetAllPoints().ToList(); if (points.Exists(p => p.Name == point.Name)) { return(false); } #endregion Validations #region Cache Storage if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE") { var ctx = HttpContext.Current; if (ctx != null) { try { var currentData = ((Point[])ctx.Cache[CacheKey]).ToList(); if (point.Id == -1) { var maxItemID = currentData.Max(x => x.Id); point.Id = maxItemID + 1; } currentData.Add(point); 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 `POINTS`(`Name`) VALUES ('" + point.Name + "')"; 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(); point.Id = Convert.ToInt32(Sequence.GetNextSequenceValue("pointid", client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]))); var collection = client.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"]).GetCollection <Point>("POINTS"); collection.InsertOne(point); return(true); } #endregion DB Storage Mongo } catch (Exception ex) { new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message); } return(false); }
public Route[] GetAllRoutes() { List <Route> allRoutes = new List <Route>(); try { #region Cache Storage if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "CACHE") { var ctx = HttpContext.Current; if (ctx != null) { return((Route[])ctx.Cache[CacheKey]); } return(new Route[] { new Route { IdSource = 0, IdDestination = 0, RouteCost = 0, RouteTime = 0 } }); } #endregion #region DB Storage MySQl else if (ConfigurationManager.AppSettings["dbType"].ToUpper() == "DB_MYSQL") { var dbCon = DBConnection.Instance(); if (dbCon.IsConnect()) { //suppose col0 and col1 are defined as VARCHAR in the DB string query = "SELECT IdSource,IdDestination,RouteCost,RouteTime Name FROM ROUTES"; var cmd = new MySqlCommand(query, dbCon.Connection); var reader = cmd.ExecuteReader(); while (reader.Read()) { Route route = new Route(); route.IdSource = reader.GetInt32(0); route.IdDestination = reader.GetInt32(1); route.RouteCost = reader.GetInt32(2); route.RouteTime = reader.GetInt32(3); allRoutes.Add(route); } dbCon.Close(); } } #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"); allRoutes = collection.Find(_ => true).ToList(); } #endregion DB Storage Mongo } catch (Exception ex) { new DeliveryWService.Services.LogService(true).Error(DeliveryWService.Services.LogServiceAux.GetCurrentMethod() + " - " + ex.Message); } return(allRoutes.ToArray()); }
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 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 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); }