//Edit Server
        public bool EditServer(ServerDetailVM model, out string msg)
        {
            try
            {
                Server s = _context.Server.Where(a => a.ServerName == model.ServerName).FirstOrDefault();

                if (s != null)
                {
                    if (s.ReferenceID != s.ReferenceID)
                    {
                        msg = "Server name already exist.";
                        return(false);
                    }
                }

                Server serverUpdated = _context.Server.Where(a => a.ReferenceID == model.ReferenceID).FirstOrDefault();

                serverUpdated.ServerName   = model.ServerName;
                serverUpdated.StatusID     = model.StatusID;
                serverUpdated.LocationID   = model.LocationID;
                serverUpdated.Description  = model.Description;
                serverUpdated.ReferenceID  = model.ReferenceID;
                serverUpdated.ServerTypeID = model.ServerTypeID;

                if (model.ApplicationReferenceIDs == null)
                {
                    model.ApplicationReferenceIDs = new string[0];
                }

                var apps = _context.Application.Where(a => model.ApplicationReferenceIDs.Contains(a.ReferenceID));

                serverUpdated.Applications.Clear();

                serverUpdated.Applications = apps.ToList();
                _context.SaveChanges();

                msg = "Server information succesfully updated.";
                return(true);
            }
            catch
            {
                msg = "Failed to update server.";
                return(false);
            }
        }
        public ActionResult Edit(ServerDetailVM model)
        {
            if (ModelState.IsValid)
            {
                if (_serverRepo.EditServer(model, out string msg))
                {
                    TempData["SuccessMsg"] = msg;
                    return(RedirectToAction("Index"));
                }

                TempData["ErrorMsg"] = msg;
            }

            model.StatusList      = _serverRepo.GetStatusList();
            model.LocationList    = _serverRepo.GetLocationList();
            model.ServerTypeList  = _serverRepo.GetServerTypeList();
            model.ApplicationList = _selectRepo.GetApplicationList();

            return(View(model));
        }
        //Gets all the detials involved with the server takes referenceID
        public ServerDetailVM GetServerDetails(string referenceID)
        {
            try
            {
                Server server = _context.Server.Where(a => a.ReferenceID == referenceID).FirstOrDefault();

                IEnumerable <Notification> allServerNotifications = server.Notifications;

                IEnumerable <ServerThreadVM> serverThreads = allServerNotifications.Select(thread => new ServerThreadVM()
                {
                    ReferenceID   = thread.ReferenceID,
                    ThreadID      = thread.IncidentNumber,
                    ThreadHeading = thread.NotificationHeading,
                    SentDateTime  = thread.SentDateTime,
                    ThreadType    = thread.NotificationType.NotificationTypeName,
                    LevelOfImpact = thread.LevelOfImpact.LevelName,
                    ThreadStatus  = thread.Status.StatusName
                });

                IEnumerable <Application> allServerApplications = server.Applications;

                //.GroupBy(n => n.ClientID)
                //.Select(t => t.OrderBy(i => i.FirstName))
                IEnumerable <ServerApplicationVM> serverApplication = allServerApplications.Select(app => new ServerApplicationVM()
                {
                    ApplicationReferenceID = app.ReferenceID,
                    ApplicationName        = app.ApplicationName,
                    Status      = app.Status.StatusName,
                    ClientID    = app.Client.ClientName,
                    Description = app.Description,
                    URL         = app.URL
                });

                var getServerApplications = server.Applications.Select(app => app.ReferenceID).ToArray();

                ServerDetailVM model = new ServerDetailVM
                {
                    ServerName              = server.ServerName,
                    ReferenceID             = server.ReferenceID,
                    Description             = server.Description,
                    StatusID                = server.Status.StatusID,
                    Status                  = server.Status.StatusName,
                    LocationID              = server.DataCenterLocation.LocationID,
                    Location                = server.DataCenterLocation.Location,
                    ServerTypeID            = server.ServerType.ServerTypeID,
                    ServerType              = server.ServerType.ServerTypeName,
                    Threads                 = serverThreads,
                    Applications            = serverApplication,
                    ApplicationReferenceIDs = getServerApplications
                };

                model.StatusList      = GetStatusList();
                model.ServerTypeList  = GetServerTypeList();
                model.LocationList    = GetLocationList();
                model.ApplicationList = _selectRepo.GetApplicationList();

                return(model);
            }
            catch (Exception e)
            {
                if (e is SqlException)
                {
                }

                return(null);
            }
        }