/// <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);
        }
Exemple #2
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();
            }));
        }