Esempio n. 1
0
        public bool CreateDefaults()
        {
            //bool AnswerRepositoryIsEmpty = AnswerRepository.Select().Count() == 0;
            //bool AnswerRepositoryIsEmpty = AnswerRepository.FindBy(item=>item.Description=="zzzz").Count() == 0;
            bool AnswerRepositoryIsEmpty = AnswerRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;

            if (AnswerRepositoryIsEmpty == false)
            {
                return(false);
            }
            bool CategoryRepositoryIsEmpty = CategoryRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;
            bool EarningRepositoryIsEmpty  = EarningRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;
            bool QuestionRepositoryIsEmpty = QuestionRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;
            bool TagRepositoryIsEmpty      = TagRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;
            //bool ApplicationUserRepositoryIsEmpty = ApplicationUserRepository.All().Count() == 0;
            bool ApplicationUserRepositoryIsEmpty = ApplicationUserRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;

            ////aboutIsEmpty = false;
            //if (CategoryRepositoryIsEmpty == false) return false;
            if (CategoryRepositoryIsEmpty)
            {
                try
                {
                    string[] categoryNames = new string[] { "Categoryname1", "Categoryname2" };
                    foreach (var item in categoryNames)
                    {
                        var newItem = new Category
                        {
                            CreatedDate = DateTime.Now,
                            UpdatedDate = DateTime.Now,
                            Name        = item,
                            ImageOrIcon = "https://img.icons8.com/nolan/64/bug.png"
                        };
                        CategoryRepository.AddAsync(newItem).GetAwaiter().GetResult();
                    }
                    Commit();
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (TagRepositoryIsEmpty)
            {
                try
                {
                    string[] tagNames = new string[] { "tag1", "tag2", "tag3", "tag4" };
                    foreach (var item in tagNames)
                    {
                        var newItem = new Tag
                        {
                            CreatedDate = DateTime.Now,
                            UpdatedDate = DateTime.Now,
                            Name        = item
                        };
                        TagRepository.AddAsync(newItem).GetAwaiter().GetResult();
                    }
                    Commit();
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (ApplicationUserRepositoryIsEmpty)
            {
                try
                {
                    string[] userNames = new string[] { "User1", "User2", "User3", "User4" };
                    foreach (var item in userNames)
                    {
                        var newItem = new ApplicationUser
                        {
                            Id                 = Guid.NewGuid().ToString(),
                            CreatedDate        = DateTime.Now,
                            UpdatedDate        = DateTime.Now,
                            FacebookName       = item,
                            ProfilePicture     = "SeedPicture",
                            ProfilePictureWeb  = "https://img.icons8.com/nolan/64/user.png",
                            ConcurrencyStamp   = Guid.NewGuid().ToString(),
                            SecurityStamp      = Guid.NewGuid().ToString("N").ToUpper(),
                            NormalizedEmail    = item.ToLower() + "@asqme.com",
                            Email              = item.ToLower() + "@asqme.com",
                            NormalizedUserName = item.ToLower() + "@asqme.com",
                            UserName           = item.ToLower() + "@asqme.com",
                        };
                        //var newItem2 = new ApplicationUser
                        //{
                        //    CreatedDate = DateTime.Now,
                        //    UpdatedDate = DateTime.Now,
                        //    FacebookName = "FacebookName",
                        //    ProfilePicture = "SeedPicture",
                        //    ProfilePictureWeb = "https://img.icons8.com/nolan/64/user.png",
                        //    ConcurrencyStamp = Guid.NewGuid().ToString(),
                        //    SecurityStamp = Guid.NewGuid().ToString(),
                        //    NormalizedEmail = AppSettings.Value.AdminEmail,
                        //    Email = AppSettings.Value.AdminEmail,
                        //    NormalizedUserName = AppSettings.Value.AdminEmail,
                        //    UserName = AppSettings.Value.AdminEmail,
                        //};
                        ApplicationUserRepository.AddAsync(newItem).GetAwaiter().GetResult();
                        //UserManager.CreateAsync(newItem2).GetAwaiter().GetResult();
                    }
                    Commit();
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (QuestionRepositoryIsEmpty)
            {
                try
                {
                    var users      = ApplicationUserRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                    var tags       = TagRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                    var categories = CategoryRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                    foreach (var item in tags)
                    {
                        var newItem = new Question
                        {
                            CreatedDate       = DateTime.Now,
                            UpdatedDate       = DateTime.Now,
                            Title             = "Question Title",
                            Description       = "Question Description",
                            ApplicationUserId = users[new Random().Next(0, users.Count() - 1)].Id,
                            SolverAnswerId    = int.MinValue,
                            CategoryId        = categories[new Random().Next(0, categories.Count() - 1)].Id
                        };
                        QuestionRepository.AddAsync(newItem).GetAwaiter().GetResult();
                        //Commit();
                        //Id = QuestionTagRepository.All().Count() + 1,
                        //CreatedDate = DateTime.Now,
                        //UpdatedDate = DateTime.Now,
                        var newItem2 = new QuestionTag
                        {
                            QuestionId = newItem.Id,
                            TagId      = tags[new Random().Next(0, tags.Count() - 1)].Id
                        };
                        QuestionTagRepository.AddAsync(newItem2).GetAwaiter().GetResult();
                    }
                    Commit();
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (AnswerRepositoryIsEmpty)
            {
                var users     = ApplicationUserRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                var questions = QuestionRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                try
                {
                    foreach (var item in questions)
                    {
                        var newItem = new Answer
                        {
                            CreatedDate       = DateTime.Now,
                            UpdatedDate       = DateTime.Now,
                            QuestionId        = item.Id,
                            ApplicationUserId = users[new Random().Next(0, users.Count() - 1)].Id,
                            Description       = "Answer Description"
                        };
                        AnswerRepository.AddAsync(newItem).GetAwaiter().GetResult();
                        Commit();
                        item.SolverAnswerId = newItem.Id;
                        QuestionRepository.UpdateAsync(item).GetAwaiter().GetResult();
                        Commit();
                    }
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (EarningRepositoryIsEmpty)
            {
                try
                {
                    var users = ApplicationUserRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                    //var earningsTypes = new string[] { "Received", "Sent" };
                    var earningsTypes = Enum.GetValues(typeof(Earning.EarningType));
                    foreach (var item in users)
                    {
                        foreach (var item2 in users)
                        {
                            var newItem = new Earning
                            {
                                CreatedDate  = DateTime.Now,
                                UpdatedDate  = DateTime.Now,
                                EarningsType = (Earning.EarningType)earningsTypes.GetValue(new Random().Next(earningsTypes.Length)),
                                Amount       = 1m,
                                SenderId     = users.Where(u => u.Id != item.Id).ToList()
                                               [new Random().Next(0, users.Count() - 2)].Id,
                                ApplicationUserId = item.Id
                            };
                            EarningRepository.AddAsync(newItem).GetAwaiter().GetResult();
                        }
                        Commit();
                    }
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            //Commit();
            if (CategoryRepositoryIsEmpty || TagRepositoryIsEmpty || ApplicationUserRepositoryIsEmpty ||
                QuestionRepositoryIsEmpty || AnswerRepositoryIsEmpty || EarningRepositoryIsEmpty)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }