public Build Update(Build reqBuild)
        {
            var build = Context.Builds.Select(x => x).FirstOrDefault(x => x.Id == reqBuild.Id);

            if (build != null)
            {
                Context.Entry(build).CurrentValues.SetValues(reqBuild);
                Context.Entry(build).State = EntityState.Modified;
                Context.SaveChanges();
            }

            return(build);
        }
Example #2
0
        /// <summary>
        /// Автоматически сопоставить файлы и записать их в БД
        /// </summary>
        public void AutoMapFiles(Build build, bool isStartMapping = false)
        {
            var buildDirectory = Path.Combine(ScriptsPath, Configuration.GetSection("AppSettings")["BuildDirectory"]);

            if (!Directory.Exists(buildDirectory))
            {
                throw new DirectoryNotFoundException("Не найдена директория со скриптами!.");
            }

            var fileNames = Directory.GetFiles(buildDirectory, "*.js", SearchOption.AllDirectories);

            var savedWebResources = WebResourceContext.WebResourceMaps.Select(x => x).ToList();

            foreach (var fileName in fileNames)
            {
                var webResourceHash = CryptographyHelper.GetMd5Hash(fileName);

                var index       = fileName.IndexOf("new_", StringComparison.Ordinal);
                var crmFileName = fileName.Substring(index, fileName.Length - index).Replace("\\", "/")
                                  .Replace(".bundle", "");

                var savedWebResource = savedWebResources.FirstOrDefault(x => x.CrmFileName == crmFileName);

                if (savedWebResource?.LocalFileMd5Hash == webResourceHash)
                {
                    continue;
                }

                savedWebResource = savedWebResource ?? new WebResourceMap();
                savedWebResource.IsAutoUpdate     = crmFileName.Contains("/ribbon") || crmFileName.Contains("/form");
                savedWebResource.CrmFileName      = crmFileName;
                savedWebResource.CreatedOn        = DateTime.Now;
                savedWebResource.LocalFileMd5Hash = webResourceHash;
                savedWebResource.FileSystemPath   = fileName;

                var crmWebResource = WebResourceRepository.RetrieveWebresource(crmFileName);
                if (crmWebResource != null)
                {
                    savedWebResource.CrmWebResourceId = crmWebResource.Id;
                }

                if (savedWebResource.Id == Guid.Empty)
                {
                    WebResourceContext.WebResourceMaps.Add(savedWebResource);
                }
                else
                {
                    WebResourceContext.Entry(savedWebResource).State = EntityState.Modified;
                }

                if (!isStartMapping)
                {
                    WebResourceContext.ChangedWebResources.Add(new ChangedWebResource
                    {
                        Build          = build,
                        WebResourceMap = savedWebResource,
                        ChangedDate    = DateTime.Now
                    });
                }
            }

            WebResourceContext.SaveChanges();
        }