示例#1
0
        public static async Task Initialize(WebKantoraDbContext context, IApplicationBuilder app)
        {
            await context.Database.EnsureCreatedAsync();

            if (context.Articles.Any() && context.Keywords.Any())
            {
                return;
            }

            var user = await context.Users.FirstOrDefaultAsync();

            if (user == null)
            {
                return;
            }

            using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var userManager = serviceScope.ServiceProvider.GetService <UserManager <User> >();

                var keywords = new List <Keyword>();
                for (int i = 0; i < 5; i++)
                {
                    var keyWord = new Keyword()
                    {
                        Content = $"keyword{i}"
                    };
                    keywords.Add(keyWord);
                }

                await context.AddRangeAsync(keywords);

                var articles = new List <Article>();

                for (int i = 0; i < 5; i++)
                {
                    var article = new Article()
                    {
                        Title   = $"Article{i}",
                        Author  = user,
                        Content = @"Lorem ipsum dolor sit amet, 
                                consectetur adipiscing elit, sed do eiusmod 
                                tempor incididunt ut labore et dolore magna aliqua. 
                                Ut enim ad minim veniam, quis nostrud exercitation 
                                ullamco laboris nisi ut aliquip ex ea commodo consequat. 
                                Duis aute irure dolor in reprehenderit in voluptate velit 
                                esse cillum dolore eu fugiat nulla pariatur. 
                                Excepteur sint occaecat cupidatat non proident, 
                                sunt in culpa qui officia deserunt mollit anim id est laborum.",
                        Date    = DateTime.Now
                    };
                    articles.Add(article);
                }

                await context.AddRangeAsync(articles);

                await context.SaveChangesAsync();

                var kas    = new List <KeywordArticle>();
                var random = new Random();

                foreach (var article in articles)
                {
                    var ka = new KeywordArticle()
                    {
                        Article = article,
                        Keyword = keywords[random.Next(0, 4)]
                    };
                    kas.Add(ka);
                }

                await context.AddRangeAsync(kas);

                await context.SaveChangesAsync();
            }
        }
示例#2
0
        public async Task <ActionResult> CreateArticle(CreateArticleViewModel model)
        {
            try
            {
                var text = await FileHelpers.ProcessFormFile(model.ArticleContent, ModelState);

                if (ModelState.IsValid)
                {
                    //TODO: try catch ?
                    var article = this.mapper.Map <Article>(model);
                    var user    = await this.usersService.GetByUserName(User.Identity.Name);

                    article.Content   = text;
                    article.Author    = user;
                    article.Date      = DateTime.UtcNow;
                    article.IsDeleted = false;

                    var keywords            = model.Keywords;
                    var dbKeywords          = this.keywordsService.GetAll();
                    var articleKeywordsList = new HashSet <Keyword>();
                    var keywordsArticles    = new HashSet <KeywordArticle>();

                    foreach (var keyword in keywords)
                    {
                        var newKeyword = await dbKeywords.Where(x => x.Content.ToLower() == keyword.ToLower()).FirstOrDefaultAsync();

                        if (newKeyword == null)
                        {
                            newKeyword = new Keyword()
                            {
                                Content = keyword
                            };
                            await this.keywordsService.Add(newKeyword);
                        }

                        //articleKeywordsList.Add(newKeyword);

                        var newKeywordArticle = new KeywordArticle()
                        {
                            KeywordId = newKeyword.Id,
                            //                         Keyword = newKeyword,
                            ArticleId = article.Id,
                            //                           Article = article
                        };

                        keywordsArticles.Add(newKeywordArticle);
                        // newKeyword.KeywordArticles.Add(newKeywordArticle);
                        // await this.keywordsService.Update(newKeyword.Id, newKeyword);
                        article.KeywordArticles.Add(newKeywordArticle);
                    }

                    this.cache.Remove("ArticlesCache");

                    await this.articlesService.Add(article);

                    /// Adds collection of keywordsArticles
                    //await this.keywordArticlesService.Add(keywordsArticles);

                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                return(View(model));
            }
        }