/// <summary>
        /// Creates and saves a new napack.
        /// </summary>
        /// <param name="napackName">The name of the napack</param>
        /// <param name="newNapack">The new napack to create/save</param>
        /// <param name="napackSpec">The napack specification</param>
        public void SaveNewNapack(string napackName, NewNapack newNapack, NapackSpec napackSpec)
        {
            NapackVersionIdentifier version        = new NapackVersionIdentifier(napackName, 1, 0, 0);
            NapackMetadata          metadata       = NapackMetadata.CreateFromNewNapack(napackName, newNapack);
            NapackVersion           packageVersion = NapackVersion.CreateFromNewNapack(newNapack.NewNapackVersion);

            foreach (string author in newNapack.NewNapackVersion.Authors)
            {
                AddAuthorConsumption(author, version);
            }

            foreach (string userId in newNapack.metadata.AuthorizedUserIds)
            {
                AddUserAuthorization(userId, napackName);
            }

            foreach (NapackMajorVersion consumedPackage in newNapack.NewNapackVersion.Dependencies)
            {
                AddConsumingPackage(consumedPackage, version);
            }

            // Add the new napack to all the various stores.
            NapackStats stats = new NapackStats();

            stats.AddVersion(newNapack.NewNapackVersion);

            searchIndices.Add(version.NapackName, NapackSearchIndex.CreateFromMetadataAndStats(metadata, stats));

            statsStore.Add(version.NapackName, stats);
            packageMetadataStore.Add(version.NapackName, metadata);
            packageStore.Add(version.GetFullName(), packageVersion);
            specStore.Add(version.GetFullName(), napackSpec);
        }
예제 #2
0
        public void SaveNewNapack(string napackName, NewNapack newNapack, NapackSpec napackSpec)
        {
            NapackVersionIdentifier version        = new NapackVersionIdentifier(napackName, 1, 0, 0);
            NapackMetadata          metadata       = NapackMetadata.CreateFromNewNapack(napackName, newNapack);
            NapackVersion           packageVersion = NapackVersion.CreateFromNewNapack(newNapack.NewNapackVersion);

            foreach (string author in newNapack.NewNapackVersion.Authors)
            {
                AddItem(author.ToUpperInvariant(), version, AuthorPackageTable, "authorName", "packageVersionList", false);
            }

            foreach (string userId in newNapack.metadata.AuthorizedUserIds)
            {
                AddItem(userId.ToUpperInvariant(), napackName, UserPackageTable, "userId", "packageNameList", true);
            }

            foreach (NapackMajorVersion consumedPackage in newNapack.NewNapackVersion.Dependencies)
            {
                AddItem(consumedPackage.ToString(), version, PackageConsumersTable, "packageMajorVersionId", "consumingPackages", false);
            }

            // Add the new napack to all the various stores.
            NapackStats stats = new NapackStats();

            stats.AddVersion(newNapack.NewNapackVersion);

            ExecuteTransactionCommand((command) =>
            {
                string statsEncoded = Serializer.Serialize(stats);
                command.Parameters.Add(napackName);
                command.Parameters.Add(statsEncoded);
                command.CommandText = $"INSERT INTO {PackageStatsTable} VALUES ($1, $2)";
                command.ExecuteNonQuery();
                command.Parameters.Clear();

                string metadataEncoded        = Serializer.Serialize(metadata);
                string safeDescriptionAndTags = GetSafeDescriptionAndTags(metadata);
                command.Parameters.Add(napackName);
                command.Parameters.Add(safeDescriptionAndTags);
                command.Parameters.Add(metadataEncoded);
                command.CommandText = $"INSERT INTO {PackageMetadataTable} VALUES ($1, $2, $3)";
                command.ExecuteNonQuery();
                command.Parameters.Clear();

                string napackSpecEncoded = Serializer.Serialize(napackSpec);
                command.Parameters.Add(version.GetFullName());
                command.Parameters.Add(napackSpecEncoded);
                command.CommandText = $"INSERT INTO {PackageSpecsTable} VALUES ($1, $2)";
                command.ExecuteNonQuery();
                command.Parameters.Clear();

                string packageVersionEncoded = Serializer.Serialize(packageVersion);
                command.Parameters.Add(version.GetFullName());
                command.Parameters.Add(packageVersionEncoded);
                command.CommandText = $"INSERT INTO {PackageStoreTable} VALUES ($1, $2)";
                command.ExecuteNonQuery();
                command.Parameters.Clear();
            });
        }
예제 #3
0
        public List <NapackSearchIndex> FindPackages(string searchPhrase, int skip, int top)
        {
            // Sanitize and return if the user didn't search for anything!
            List <string> searchKeys = searchPhrase.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Take(maxSearchTerms).Select(item => item.Replace("'", "''")).ToList();

            if (searchKeys.Count == 0)
            {
                return(new List <NapackSearchIndex>());
            }

            // Build the SQL query
            StringBuilder searchString = new StringBuilder();

            searchString.Append($"SELECT metadata FROM {PackageMetadataTable} WHERE ");
            for (int i = 0; i < searchKeys.Count; i++)
            {
                searchString.Append($"descriptionAndTags LIKE '%{searchKeys[i]}%'");
                if (i != searchKeys.Count - 1)
                {
                    searchString.Append(" AND ");
                }
            }

            searchString.Append($" LIMIT {top} OFFSET {skip}");
            logger.Info($"Searching for {searchString.ToString()}");

            return(ExecuteCommand((command) =>
            {
                List <NapackMetadata> metadatas = new List <NapackMetadata>();
                command.CommandText = searchString.ToString();
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        metadatas.Add(Serializer.Deserialize <NapackMetadata>(reader.GetString(0)));
                    }
                }

                logger.Info($"Search returned {metadatas.Count} items.");

                return metadatas.Select(metadata =>
                {
                    NapackStats stats = GetItem <NapackStats>(command, PackageStatsTable, "packageName", metadata.Name, "packageStat",
                                                              () => { throw new NapackStatsNotFoundException(metadata.Name); });
                    return NapackSearchIndex.CreateFromMetadataAndStats(metadata, stats);
                }).ToList();
            }));
        }
예제 #4
0
        public void IncrementPackageDownload(string packageName)
        {
            ExecuteTransactionCommand((command) =>
            {
                NapackStats stats = GetItem <NapackStats>(command, PackageStatsTable, "packageName", packageName, "packageStat",
                                                          () => { throw new NapackStatsNotFoundException(packageName); });
                stats.Downloads++;

                string packageStatsEncoded = Serializer.Serialize(stats);
                command.Parameters.Add(packageStatsEncoded);
                command.Parameters.Add(packageName);
                command.CommandText = $"UPDATE {PackageStatsTable} SET packageStat = $1 WHERE packageName = $2";
                command.ExecuteNonQuery();
                command.Parameters.Clear();
            });
        }
예제 #5
0
        private void UpdatePackageMetadataStore(NapackMetadata package, NapackVersionIdentifier nextVersion, NapackAnalyst.UpversionType upversionType, NewNapackVersion newNapackVersion)
        {
            if (upversionType == NapackAnalyst.UpversionType.Major)
            {
                NapackMajorVersionMetadata newMajorVersionMetadata = new NapackMajorVersionMetadata()
                {
                    Recalled = false,
                    Versions = new Dictionary <int, List <int> >
                    {
                        [0] = new List <int> {
                            0
                        }
                    },
                    License = newNapackVersion.License
                };

                package.Versions.Add(nextVersion.Major, newMajorVersionMetadata);
            }
            else if (upversionType == NapackAnalyst.UpversionType.Minor)
            {
                package.Versions[nextVersion.Major].Versions.Add(nextVersion.Minor, new List <int> {
                    0
                });
            }
            else
            {
                package.Versions[nextVersion.Major].Versions[nextVersion.Minor].Add(nextVersion.Patch);
            }

            ExecuteTransactionCommand((command) =>
            {
                NapackStats stats = GetItem <NapackStats>(command, PackageStatsTable, "packageName", package.Name, "packageStat",
                                                          () => { throw new NapackStatsNotFoundException(package.Name); });
                stats.AddVersion(newNapackVersion);

                string statsEncoded = Serializer.Serialize(stats);
                command.Parameters.Add(statsEncoded);
                command.Parameters.Add(package.Name);
                command.CommandText = $"UPDATE {PackageStatsTable} SET packageStat = $1 WHERE packageName = $2";
                command.ExecuteNonQuery();
                command.Parameters.Clear();
            });

            UpdatePackageMetadata(package);
        }
예제 #6
0
        public SearchModule()
            : base("/search")
        {
            // Retrieves and displays the basic search page.
            Get["/"] = parameters =>
            {
                return(View["Search"]);
            };

            // Retrieves a sequence of search results.
            Get["/query"] = parameters =>
            {
                string query = (string)this.Request.Query["search"];
                int    skip  = int.Parse(this.Request.Query["$skip"]);
                int    top   = Math.Min(int.Parse(this.Request.Query["$top"]), SearchModule.MaxResultsPerPage);

                List <NapackSearchIndex> packagesFound = Global.NapackStorageManager.FindPackages(query, skip, top);
                return(this.Response.AsJson(new
                {
                    Results = packagesFound.Select(package => package.ToAnonymousType()),
                    CanContinue = packagesFound.Count == top
                }));
            };

            // Retrives the detailed information about a search result.
            Get["/result/{packageName}"] = parameters =>
            {
                string         packageName = parameters.packageName;
                NapackMetadata metadata    = Global.NapackStorageManager.GetPackageMetadata(packageName, false);
                NapackStats    stats       = Global.NapackStorageManager.GetPackageStatistics(packageName);

                return(this.Response.AsJson(new
                {
                    Authors = stats.AllAuthors,
                    AuthorizedUsers = metadata.AuthorizedUserIds,
                    MoreInformation = metadata.MoreInformation,
                    Tags = metadata.Tags
                }));
            };
        }
예제 #7
0
 public static NapackSearchIndex CreateFromMetadataAndStats(NapackMetadata metadata, NapackStats stats)
 {
     return(new NapackSearchIndex()
     {
         Name = metadata.Name,
         Description = metadata.Description,
         LastUsedLicense = metadata.Versions[metadata.Versions.Keys.Max()].License,
         Downloads = stats.Downloads,
         Views = stats.Views,
         LastUpdateTime = stats.LastUpdateTime,
     });
 }