public CuratedPackage CreatedCuratedPackage(
            CuratedFeed curatedFeed,
            PackageRegistration packageRegistration,
            bool included             = false,
            bool automaticallyCurated = false,
            string notes       = null,
            bool commitChanges = true)
        {
            if (curatedFeed == null)
            {
                throw new ArgumentNullException(nameof(curatedFeed));
            }

            if (packageRegistration == null)
            {
                throw new ArgumentNullException(nameof(packageRegistration));
            }

            var curatedPackage = curatedFeed.Packages
                                 .SingleOrDefault(cp => cp.PackageRegistrationKey == packageRegistration.Key);

            if (curatedPackage == null)
            {
                curatedPackage = new CuratedPackage
                {
                    PackageRegistration  = packageRegistration,
                    Included             = included,
                    AutomaticallyCurated = automaticallyCurated,
                    Notes = notes,
                };

                curatedFeed.Packages.Add(curatedPackage);
            }

            if (commitChanges)
            {
                CuratedFeedRepository.CommitChanges();
            }

            return(curatedPackage);
        }
        public void ModifyCuratedPackage(
            int curatedFeedKey,
            int curatedPackageKey,
            bool included)
        {
            var curatedFeed = GetFeedByKey(curatedFeedKey, includePackages: true);

            if (curatedFeed == null)
            {
                throw new InvalidOperationException("The curated feed does not exist.");
            }

            var curatedPackage = curatedFeed.Packages.SingleOrDefault(cp => cp.Key == curatedPackageKey);

            if (curatedPackage == null)
            {
                throw new InvalidOperationException("The curated package does not exist.");
            }

            curatedPackage.Included = included;
            CuratedFeedRepository.CommitChanges();
        }
        public CuratedPackage CreatedCuratedPackage(
            CuratedFeed curatedFeed,
            Package package,
            bool included             = false,
            bool automaticallyCurated = false,
            string notes       = null,
            bool commitChanges = true)
        {
            if (curatedFeed == null)
            {
                throw new ArgumentNullException("curatedFeed");
            }

            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            var curatedPackageRegistration = curatedFeed.Packages
                                             .SingleOrDefault(cp => cp.PackageRegistrationKey == package.PackageRegistration.Key);

            var isFirstPackageInRegistration = false;

            if (curatedPackageRegistration == null)
            {
                curatedPackageRegistration = new CuratedPackage
                {
                    PackageRegistration    = package.PackageRegistration,
                    PackageRegistrationKey = package.PackageRegistrationKey,
                    Included             = included,
                    AutomaticallyCurated = automaticallyCurated,
                    Notes = notes,
                };

                curatedFeed.Packages.Add(curatedPackageRegistration);
                isFirstPackageInRegistration = true;
            }

            if (!curatedPackageRegistration.CuratedPackageVersions.Any(p => p.PackageKey == package.Key))
            {
                var curatedPackageVersion = new CuratedPackageVersion
                {
                    CuratedFeed            = curatedFeed,
                    CuratedFeedKey         = curatedFeed.Key,
                    PackageRegistration    = package.PackageRegistration,
                    PackageRegistrationKey = package.PackageRegistrationKey,
                    Package    = package,
                    PackageKey = package.Key,
                };

                // Make sure we set IsLatest + IsLatestStable for the first package, because
                // UpdateIsLatest won't be able to see this registration if we don't commit.
                // If it's the first package in the registration, then it's definitely the
                // latest. It's the latest stable package only if it's not a pre-release.
                if (isFirstPackageInRegistration)
                {
                    curatedPackageVersion.IsLatest       = true;
                    curatedPackageVersion.IsLatestStable = !package.IsPrerelease;
                }

                curatedPackageRegistration.CuratedPackageVersions.Add(curatedPackageVersion);
            }

            if (commitChanges)
            {
                CuratedFeedRepository.CommitChanges();
            }

            return(curatedPackageRegistration);
        }