Esempio n. 1
0
        /// <summary>
        /// Save a pending ReadMe if changes are detected.
        /// </summary>
        /// <param name="package">Package entity associated with the ReadMe.</param>
        /// <param name="edit">Package version edit readme request.</param>
        /// <param name="encoding">The encoding used when reading the existing readme.</param>
        /// <param name="commitChanges">Whether or not to commit the pending changes to the database.</param>
        /// <returns>True if the package readme changed, otherwise false.</returns>
        public async Task <bool> SaveReadMeMdIfChanged(
            Package package,
            EditPackageVersionReadMeRequest edit,
            Encoding encoding,
            bool commitChanges)
        {
            var activeReadMe = package.HasReadMe ?
                               NormalizeNewLines(await GetReadMeMdAsync(package)) :
                               null;

            var newReadMe = HasReadMeSource(edit?.ReadMe) ?
                            NormalizeNewLines(await GetReadMeMdAsync(edit.ReadMe, encoding)) :
                            null;

            var hasReadMe = !string.IsNullOrWhiteSpace(newReadMe);

            if (hasReadMe && !newReadMe.Equals(activeReadMe))
            {
                await _packageFileService.SaveReadMeMdFileAsync(package, newReadMe);

                edit.ReadMeState = PackageEditReadMeState.Changed;

                // Save entity to db.
                package.HasReadMe = true;

                if (commitChanges)
                {
                    await _entitiesContext.SaveChangesAsync();
                }
            }
            else if (!hasReadMe && !string.IsNullOrEmpty(activeReadMe))
            {
                await _packageFileService.DeleteReadMeMdFileAsync(package);

                edit.ReadMeState = PackageEditReadMeState.Deleted;

                // Save entity to db.
                package.HasReadMe = false;

                if (commitChanges)
                {
                    await _entitiesContext.SaveChangesAsync();
                }
            }
            else
            {
                edit.ReadMeState = PackageEditReadMeState.Unchanged;
            }

            return(edit.ReadMeState != PackageEditReadMeState.Unchanged);
        }