public async Task <JsonResult> LinkEpmToSystem(ProjectServerSystemLinkDTO syncObjectLinkViewModel)
 {
     {
         try
         {
             await ProjectServerSystemLinkBusinessService.LinkEpmToSystem(syncObjectLinkViewModel);
         }
         catch (Exception exception)
         {
             JsonResult result = HandleException(exception);
             return(result);
         }
     }
     return(Json(new ProxyResponse
     {
         Result = "ok",
     }, JsonRequestBehavior.AllowGet));
 }
        public async Task LinkEpmToSystem(ProjectServerSystemLinkDTO syncObjectLinkViewModel)
        {
            List <ProjectServerSystemLinkDTO> linksToAddDto = new List <ProjectServerSystemLinkDTO>();

            List <ProjectServerSystemLinkDTO> links =
                GetList(syncObjectLinkViewModel.ProjectUid, syncObjectLinkViewModel.SystemId);

            if (syncObjectLinkViewModel.IsHomeProject)
            {
                ProjectServerSystemLinkDTO homeLink = links.FirstOrDefault(x => x.IsHomeProject);
                if (homeLink == null)
                {
                    homeLink = new ProjectServerSystemLinkDTO();
                    linksToAddDto.Add(homeLink);
                }
                if (homeLink.ProjectId != syncObjectLinkViewModel.ProjectId)
                {
                    homeLink.ProjectUid    = syncObjectLinkViewModel.ProjectUid;
                    homeLink.SystemId      = syncObjectLinkViewModel.SystemId;
                    homeLink.ProjectKey    = syncObjectLinkViewModel.ProjectKey;
                    homeLink.ProjectName   = syncObjectLinkViewModel.ProjectName;
                    homeLink.ProjectId     = syncObjectLinkViewModel.ProjectId;
                    homeLink.IsHomeProject = syncObjectLinkViewModel.IsHomeProject;
                    homeLink.LastExecuted  = syncObjectLinkViewModel.LastExecuted;
                    homeLink.ExecuteStatus = syncObjectLinkViewModel.ExecuteStatus;
                    homeLink.DateCreated   = DateTime.Now;
                }
            }
            else
            {
                RemoveRange(x => x.ProjectUid == syncObjectLinkViewModel.ProjectUid &&
                            x.SystemId == syncObjectLinkViewModel.SystemId &&
                            x.IsHomeProject);
            }
            if (syncObjectLinkViewModel.Epics != null)
            {
                List <string> epicKeys = syncObjectLinkViewModel.Epics.Select(x => x.EpicKey).ToList();
                RemoveRange(x => x.ProjectUid == syncObjectLinkViewModel.ProjectUid &&
                            x.SystemId == syncObjectLinkViewModel.SystemId &&
                            !epicKeys.Contains(x.EpicKey) && !x.IsHomeProject);

                foreach (ProjectServerSystemLinkDTO epic in syncObjectLinkViewModel.Epics)
                {
                    ProjectServerSystemLinkDTO epicLink = links.FirstOrDefault(x => x.EpicKey == epic.EpicKey);
                    if (epicLink == null)
                    {
                        epicLink = new ProjectServerSystemLinkDTO();
                        linksToAddDto.Add(epicLink);
                    }
                    if (epicLink.EpicKey != epic.EpicKey)
                    {
                        epicLink.ProjectUid    = syncObjectLinkViewModel.ProjectUid;
                        epicLink.SystemId      = syncObjectLinkViewModel.SystemId;
                        epicLink.IsHomeProject = false;
                        epicLink.ProjectKey    = epic.ProjectKey;
                        epicLink.ProjectName   = epic.ProjectName;
                        epicLink.ProjectId     = epic.ProjectId;
                        epicLink.EpicId        = epic.EpicId;
                        epicLink.EpicName      = epic.EpicName;
                        epicLink.EpicKey       = epic.EpicKey;
                        epicLink.DateCreated   = DateTime.Now;
                    }
                }
            }
            if (syncObjectLinkViewModel.Epics == null || syncObjectLinkViewModel.Epics.Count == 0)
            {
                RemoveRange(x => x.ProjectUid == syncObjectLinkViewModel.ProjectUid &&
                            x.SystemId == syncObjectLinkViewModel.SystemId &&
                            !x.IsHomeProject);
            }

            List <ProjectServerSystemLink> linksToAdd = linksToAddDto.Select(x => new ProjectServerSystemLink()
            {
                IsHomeProject             = x.IsHomeProject,
                DateCreated               = x.DateCreated,
                EpicId                    = x.EpicId,
                EpicKey                   = x.EpicKey,
                EpicName                  = x.EpicName,
                ExecuteStatus             = x.ExecuteStatus,
                LastExecuted              = x.LastExecuted,
                ProjectId                 = x.ProjectId,
                ProjectKey                = x.ProjectKey,
                ProjectName               = x.ProjectName,
                ProjectServerSystemLinkId = x.ProjectServerSystemLinkId,
                ProjectUid                = x.ProjectUid,
                SystemId                  = x.SystemId
            })
                                                        .ToList();

            UnitOfWork.ProjectServerSystemLinkRepository.AddRange(linksToAdd);
            await UnitOfWork.SaveChangesAsync();
        }