예제 #1
0
        /// <summary>
        /// Seed database with materials and comment for testing purposes
        /// </summary>
        public void SeedAddTestData(IList <string> catTokens, bool titleAppendCategoryName = false)
        {
            if (catTokens.Contains("seed"))
            {
                catTokens[catTokens.IndexOf("seed")] = "seed:Root";
            }
            catTokens = catTokens.Select(x => x.Substring("seed:".Length)).ToList();

            using (DataBaseConnection db = new DataBaseConnection(providerName, connectionString))
            {
                DataContainer dataContainer = new DataContainer
                {
                    Categories        = db.Categories.ToList(),
                    Users             = db.Users.ToList(),
                    currentMaterialId = db.Materials.Any() ? db.Materials.Max(x => x.Id) + 1 : 1,
                    currentCommentId  = db.Comments.Any() ? db.Comments.Max(x => x.Id) + 1 : 1
                };

                MaterialsSeeder materialsSeeder = new MaterialsSeeder(dataContainer);

                foreach (var catToken in catTokens)
                {
                    var parts          = catToken.Split(":");
                    var categoryName   = parts[0];
                    int?materialsCount = null;
                    if (parts.Length > 1)
                    {
                        materialsCount = int.Parse(parts[1]);
                    }
                    int?commentsCount = null;
                    if (parts.Length > 2)
                    {
                        commentsCount = int.Parse(parts[2]);
                    }

                    if (materialsCount.HasValue)
                    {
                        materialsSeeder.MinMaterialCount = materialsSeeder.MaxMaterialCount = materialsCount.Value;
                    }

                    if (commentsCount.HasValue)
                    {
                        materialsSeeder.CommentsCount = commentsCount.Value;
                    }

                    materialsSeeder.TitleAppendCategoryName = titleAppendCategoryName;

                    materialsSeeder.SeedCategoryAndSub(categoryName);
                }

                new DataBaseSeeder(db, dataContainer).SeedMaterials().PostSeedMaterials();
            }
        }
예제 #2
0
        /// <summary>
        /// Seed database with materials and comment for testing purposes
        /// </summary>
        public void SeedAddTestData(IList <string> catTokens, bool titleAppendCategoryName = false)
        {
            CheckDbConnectionAndExitIfFailed();

            const string seedCommandDots = SeedCommand + ":";

            if (catTokens.Contains(SeedCommand))
            {
                catTokens[catTokens.IndexOf(SeedCommand)] = seedCommandDots + Category.RootCategoryName;
            }
            catTokens = catTokens.Select(x => x.Substring(seedCommandDots.Length)).ToList();

            SeedTestData();


            void SeedTestData()
            {
                using var db = new DataBaseConnection(providerName, connectionString);

                var dataContainer = new DataContainer
                {
                    Categories        = db.Categories.ToList(),
                    Users             = db.Users.ToList(),
                    currentMaterialId = db.Materials.Any() ? db.Materials.Max(x => x.Id) + 1 : 1,
                    currentCommentId  = db.Comments.Any() ? db.Comments.Max(x => x.Id) + 1 : 1
                };

                var titlesPath     = Path.Combine(this.configDirectoryPath, SeederPathsNames.InitDir, SeederPathsNames.SeedTemplatesDir, SeederPathsNames.FishTitlesFile);
                var paragraphsPath = Path.Combine(this.configDirectoryPath, SeederPathsNames.InitDir, SeederPathsNames.SeedTemplatesDir,
                                                  SeederPathsNames.FishParagraphsFile);
                var titles = Regex.Matches(File.ReadAllText(titlesPath), "<h1>(.*?)</h1>", RegexOptions.Singleline)
                             .Select(x => x.Groups[1].Value).ToList();
                var paragraphs = Regex
                                 .Matches(File.ReadAllText(paragraphsPath), "<p>(.*?)</p>", RegexOptions.Singleline)
                                 .Select(x => x.Groups[1].Value).ToList();


                var materialsSeeder = new MaterialsSeeder(dataContainer, titles, paragraphs);

                foreach (var catToken in catTokens)
                {
                    var parts          = catToken.Split(":");
                    var categoryName   = parts[0];
                    int?materialsCount = null;
                    if (parts.Length > 1)
                    {
                        materialsCount = int.Parse(parts[1]);
                    }
                    int?commentsCount = null;
                    if (parts.Length > 2)
                    {
                        commentsCount = int.Parse(parts[2]);
                    }

                    if (materialsCount.HasValue)
                    {
                        materialsSeeder.MinMaterialCount = materialsSeeder.MaxMaterialCount = materialsCount.Value;
                    }

                    if (commentsCount.HasValue)
                    {
                        materialsSeeder.CommentsCount = commentsCount.Value;
                    }

                    materialsSeeder.TitleAppendCategoryName = titleAppendCategoryName;

                    materialsSeeder.SeedCategoryAndSub(categoryName);
                }

                new DataBaseSeeder(db, dataContainer).SeedMaterials().PostSeedMaterials();
            }
        }
예제 #3
0
        /// <summary>
        /// Seed database with materials and comment for testing purposes
        /// </summary>
        public void SeedAddTestData(IList <string> catTokens, bool titleAppendCategoryName = false)
        {
            const string seedCommandDots = SeedCommand + ":";

            if (catTokens.Contains(SeedCommand))
            {
                catTokens[catTokens.IndexOf(SeedCommand)] = seedCommandDots + Category.RootName;
            }
            catTokens = catTokens.Select(x => x.Substring(seedCommandDots.Length)).ToList();

            using (DataBaseConnection db = new DataBaseConnection(providerName, connectionString))
            {
                try
                {
                    SeedTestData(db);
                }
                catch (DbException e)
                {
                    throw new SunDataBaseException(
                              "Exception happened in data seed process. " +
                              "Check that last migrations were done('migrate' argument) and system initialized ('init' argument).",
                              e);
                }
                catch (SocketException e)
                {
                    throw new SunDataBaseException("The connection could not be made. " +
                                                   "Check the database you are trying to connect exists.", e);
                }
            }


            void SeedTestData(DataBaseConnection db)
            {
                var dataContainer = new DataContainer
                {
                    Categories        = db.Categories.ToList(),
                    Users             = db.Users.ToList(),
                    currentMaterialId = db.Materials.Any() ? db.Materials.Max(x => x.Id) + 1 : 1,
                    currentCommentId  = db.Comments.Any() ? db.Comments.Max(x => x.Id) + 1 : 1
                };

                var materialsSeeder = new MaterialsSeeder(dataContainer);

                foreach (var catToken in catTokens)
                {
                    var parts          = catToken.Split(":");
                    var categoryName   = parts[0];
                    int?materialsCount = null;
                    if (parts.Length > 1)
                    {
                        materialsCount = int.Parse(parts[1]);
                    }
                    int?commentsCount = null;
                    if (parts.Length > 2)
                    {
                        commentsCount = int.Parse(parts[2]);
                    }

                    if (materialsCount.HasValue)
                    {
                        materialsSeeder.MinMaterialCount = materialsSeeder.MaxMaterialCount = materialsCount.Value;
                    }

                    if (commentsCount.HasValue)
                    {
                        materialsSeeder.CommentsCount = commentsCount.Value;
                    }

                    materialsSeeder.TitleAppendCategoryName = titleAppendCategoryName;

                    materialsSeeder.SeedCategoryAndSub(categoryName);
                }

                new DataBaseSeeder(db, dataContainer).SeedMaterials().PostSeedMaterials();
            }
        }