コード例 #1
0
        public async Task CreateArticle(string title, string content, Guid[] categoryIds, Guid UserId, bool IsClosingComments, bool state)  //添加用户选择了分类的博客
        {
            using (var articleSvc = new ArticleService())
            {
                var article = new Article()
                {
                    Title             = title,
                    Content           = content,
                    State             = state,
                    UserId            = UserId,
                    IsClosingComments = IsClosingComments
                };
                await articleSvc.CreateAsync(article);

                Guid articleId = article.Id;//拿到新增的文章id
                using (var articleToCategory = new ArticleToCategoryService())
                {
                    foreach (var item in categoryIds)
                    {
                        await articleToCategory.CreateAsync(new ArticleToCategory()
                        {
                            BlogCategoryId = item,
                            ArticleId      = articleId
                        }, false);                   //由于这里可能不止有一个类别id所有等全部添加完一起保存
                    }
                    await articleToCategory.Saved(); //循环完再保存
                }
            }
        }
コード例 #2
0
        public async Task EditArticle(Guid articleId, string title, string content, Guid[] categoryIds, bool IsClosingComments)  //修改文章
        {
            using (IArticleService articleSvc = new ArticleService())
            {
                var article = await articleSvc.GetOneByIdAsync(articleId);

                article.Title             = title;
                article.Content           = content;
                article.State             = true;         //保存修改发布状态改为true
                article.CreateTime        = DateTime.Now; //将发布时间设置为当前时间
                article.IsClosingComments = IsClosingComments;
                await articleSvc.EditAsync(article);

                using (IArticleToCategoryService articleToCategorySvc = new ArticleToCategoryService())
                {
                    //根据文章Id先删除原来所有的类别
                    foreach (var item in articleToCategorySvc.GetAllAsync().Where(m => m.ArticleId == articleId))
                    {
                        await articleToCategorySvc.RemoveAsync(item, false);
                    }
                    await articleToCategorySvc.Saved();  //循环完后保存

                    if (categoryIds != null)
                    {
                        foreach (var item in categoryIds)
                        {
                            await articleToCategorySvc.CreateAsync(new ArticleToCategory()
                            {
                                ArticleId      = articleId,
                                BlogCategoryId = item
                            }, false);
                        }
                    }
                    await articleToCategorySvc.Saved();  //循环完后保存
                }
            }
        }