// create data center public bool AddDataCenter(DataCenterVM dataCenter, out string msg) { DataCenterLocation d = _context.DataCenterLocation.Where(a => a.Location == dataCenter.Location) .FirstOrDefault(); if (d != null) { msg = "Data Center name already exist."; return(false); } try { DataCenterLocation newDataCenter = new DataCenterLocation(); newDataCenter.Location = dataCenter.Location; _context.DataCenterLocation.Add(newDataCenter); _context.SaveChanges(); msg = "Data Center successfully added"; return(true); } catch { msg = "Failed to add data center."; return(false); } }
// update data center public bool EditDataCenter(DataCenterVM dataCenter, out string msg) { DataCenterLocation d = _context.DataCenterLocation.Where(a => a.Location == dataCenter.Location).FirstOrDefault(); if (d != null) { if (d.LocationID != dataCenter.LocationID) { msg = "Data Center name already exist."; return(false); } } DataCenterLocation original = _context.DataCenterLocation.Where(a => a.LocationID == dataCenter.LocationID).FirstOrDefault(); bool changed = original.Location != dataCenter.Location; // check if any data center info changed if (changed) { try { DataCenterLocation dataCenterUpdated = _context.DataCenterLocation .Where(a => a.LocationID == dataCenter.LocationID) .FirstOrDefault(); dataCenterUpdated.Location = dataCenter.Location; dataCenterUpdated.LocationID = dataCenter.LocationID; _context.SaveChanges(); msg = "Data Center information succesfully updated."; return(true); } catch { msg = "Failed to update data center."; return(false); } } else { msg = "Information is identical, no update performed."; return(false); } }
// delete data center public bool DeleteDataCenter(int referenceID, out string msg) { DataCenterLocation dataCenterToBeDeleted = _context.DataCenterLocation .Where(a => a.LocationID == referenceID) .FirstOrDefault(); // check servers associated with the data center var dataCenterServers = _context.Server .Where(a => a.LocationID == referenceID) .FirstOrDefault(); IEnumerable <Server> allDataCenterServer = dataCenterToBeDeleted.Servers; if (dataCenterToBeDeleted == null) { msg = "Data Center could not be deleted."; return(false); } if (allDataCenterServer.Count() > 0) { msg = "Data Center has Server(s) associated, cannot be deleted"; return(false); } try { _context.DataCenterLocation.Remove(dataCenterToBeDeleted); _context.SaveChanges(); msg = "Data Center Successfully Deleted"; return(true); } catch { msg = "Failed to update Data Center."; return(false); } }