Example #1
0
        public async Task CreateArticle(string title, string content, Guid[] categoryIds, Guid userId)
        {
            using (var articleService = new ArticleService())
            {
                var article = new Article()
                {
                    Title   = title,
                    Content = content,
                    UserId  = userId
                };
                await articleService.CreateAsync(article);

                Guid articleId = article.Id;
                using (var articleToCategoryService = new ArticleToCategoryService())
                {
                    foreach (var caregoryId in categoryIds)
                    {
                        await articleToCategoryService.CreateAsync(new ArticleToCategory()
                        {
                            ArticleId      = articleId,
                            BlogCategoryId = caregoryId,
                        }, saved : false);
                    }
                    await articleToCategoryService.Save();
                }
            }
        }
Example #2
0
        public async Task EditArticle(Guid articleId, string title, string content, Guid[] categoryIds)
        {
            using (IArticleService articleService = new ArticleService())
            {
                var article = await articleService.GetOneByIdAsync(articleId);

                article.Title   = title;
                article.Content = content;
                await articleService.EditAsync(article);
            }
            using (IArticleToCategoryService articleToCategoryService = new ArticleToCategoryService())
            {
                //删除原有类别
                foreach (var categoryId in articleToCategoryService.GetAll().Where(m => m.ArticleId == articleId))
                {
                    await articleToCategoryService.RemoveAsync(categoryId, saved : false);
                }
                if (categoryIds != null)
                {
                    foreach (var categoryId in categoryIds)
                    {
                        await articleToCategoryService.CreateAsync(new ArticleToCategory()
                        {
                            ArticleId      = articleId,
                            BlogCategoryId = categoryId
                        }, saved : false);
                    }
                }
                await articleToCategoryService.Save();
            }
        }
Example #3
0
        /// <summary>
        /// 根据用户获取所有文章并分页
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        public async Task <List <ArticleDto> > GetAllArticles(Guid userId, int pageIndex, int pageSize)
        {
            using (var articleService = new ArticleService())
            {
                var list = await articleService.GetAllByPageOrder(pageSize, pageIndex, asc : false).Include(x => x.User).Where(x => x.UserId == userId)
                           .Select(x => new ArticleDto()
                {
                    Title      = x.Title,
                    BadCount   = x.BadCount,
                    GoodCount  = x.GoodCount,
                    Email      = x.User.Email,
                    Content    = x.Content,
                    CreateTime = x.CreateTime,
                    ArticleId  = x.Id,
                    ImagePath  = x.User.ImagePath
                }).ToListAsync();

                using (IArticleToCategoryService articleToCategoryService = new ArticleToCategoryService())
                {
                    foreach (var articleDto in list)
                    {
                        var cates = await articleToCategoryService.GetAll().Include(x => x.Category).Where(x => x.ArticleId == articleDto.ArticleId).ToListAsync();

                        articleDto.CategoryIds   = cates.Select(x => x.CategoryId).ToArray();
                        articleDto.CategoryNames = cates.Select(x => x.Category.CategoryName).ToArray();
                    }

                    return(list);
                }
            }
        }
Example #4
0
        public async Task <List <ArticleDto> > GetAllArticleByEamil(string eamil)
        {
            using (var articleService = new ArticleService())
            {
                var list = await articleService.GetAllByPageOrder().Where(m => m.User.Email == eamil).Select(m => new ArticleDto()
                {
                    Title      = m.Title,
                    BadCount   = m.BadCount,
                    GoodCount  = m.GoodCount,
                    Email      = m.User.Email,
                    Content    = m.Content,
                    CreateTime = m.CreateTime,
                    Id         = m.Id,
                    ImagePath  = m.User.ImagePath
                }).ToListAsync();

                using (IArticleToCategoryService articleToCategoryService = new ArticleToCategoryService())
                {
                    foreach (var articleDto in list)
                    {
                        var cates = await articleToCategoryService.GetAll().Include(m => m.BlogCagetory).Where(m => m.ArticleId == articleDto.Id).ToListAsync();

                        articleDto.CategoryIds   = cates.Select(m => m.BlogCategoryId).ToArray();
                        articleDto.CategoryNames = cates.Select(m => m.BlogCagetory.Cagetory).ToArray();
                    }
                    return(list);
                }
            }
        }
Example #5
0
        /// <summary>
        /// 根据编号获取文章
        /// </summary>
        /// <param name="articleId">文章编号</param>
        /// <returns></returns>
        public async Task <ArticleDto> GetArticleById(Guid articleId)
        {
            using (IArticleService articleService = new ArticleService())
            {
                var data = await articleService.GetAll().Where(x => x.Id == articleId).Select(x => new ArticleDto()
                {
                    ArticleId  = x.Id,
                    BadCount   = x.BadCount,
                    Title      = x.Title,
                    Content    = x.Content,
                    CreateTime = x.CreateTime,
                    Email      = x.User.Email,
                    GoodCount  = x.GoodCount,
                    ImagePath  = x.User.ImagePath
                }).FirstAsync();

                using (IArticleToCategoryService articleToCategoryService = new ArticleToCategoryService())
                {
                    var cates = await articleToCategoryService.GetAll().Include(x => x.Category)
                                .Where(x => x.ArticleId == data.ArticleId).ToListAsync();

                    data.CategoryIds   = cates.Select(x => x.CategoryId).ToArray();
                    data.CategoryNames = cates.Select(x => x.Category.CategoryName).ToArray();

                    return(data);
                }
            }
        }
        public async Task EditArticle(Guid articleId, string title, string content, Guid[] categoryId)
        {
            using (IDAL.IArticleService articleService = new  ArticleService())
            {
                var article = await articleService.GetOneByIdAsync(articleId);

                article.Title   = title;
                article.Content = content;
                await articleService.EditAsync(article);
            }

            using (DAL.ArticleToCategoryService articleToCategoryService = new ArticleToCategoryService())
            {
                foreach (var item in articleToCategoryService.GetAllAsync().Where(m => m.ArticleId == articleId))
                {
                    await articleToCategoryService.RemoveAsync(item, false);
                }
                foreach (var item in categoryId)
                {
                    await articleToCategoryService.CreateAsync(new ArticleToCategory()
                    {
                        ArticleId         = articleId,
                        ArticleCategoryId = item
                    }, false);
                }
                await articleToCategoryService.Save();
            }
        }
        public async Task <ArticleDto> GetOneArticleById(Guid ArticleId)
        {
            using (IArticleService articleService = new ArticleService())
            {
                var list = await articleService.GetAllAsync().Include(m => m.User)
                           .Where(m => m.Id == ArticleId).Select(m => new Dto.ArticleDto()
                {
                    Id         = m.Id,
                    Title      = m.Title,
                    Content    = m.Content,
                    CreateTime = m.CreatTime,
                    BadCount   = m.BadCount,
                    GoodCount  = m.GoodCount,
                    Email      = m.User.Email,
                    ImagePath  = m.User.ImagePath
                }).FirstOrDefaultAsync();

                using (IArticleToCategory service = new ArticleToCategoryService())
                {
                    var categiryList = await service.GetAllAsync().Include(m => m.BlogCategory)
                                       .Where(m => m.ArticleId == list.Id).ToListAsync();

                    list.CategoryIds   = categiryList.Select(m => m.BlogCategory.Id).ToArray();
                    list.CategoryNames = categiryList.Select(m => m.BlogCategory.CategoryName).ToArray();
                    return(list);
                }
            }
        }
Example #8
0
        public async Task <Guid> CreateArticle(string title, string content, string introContent, Guid[] categoryIds, Guid userId)
        {
            using (IArticleService articleSvc = new ArticleService())
            {
                ////处理内容
                //string IntroContent = FilterHTML(content);
                Article article = new Article()
                {
                    Title        = title,
                    Content      = content,
                    IntroContent = introContent,
                    UserId       = userId,
                    IsTop        = categoryIds != null?categoryIds.Contains(Guid.Parse("00000000-0000-0000-0000-000000000001")) : false
                };
                await articleSvc.CreatAsync(article);//添加完会自动有Id值

                Guid articleId = article.Id;
                using (IArticleToCategory articleToCategorySvc = new ArticleToCategoryService())
                {
                    if (categoryIds != null)//当分类不为空时才循环添加
                    {
                        foreach (var categroyId in categoryIds)
                        {
                            await articleToCategorySvc.CreatAsync(new ArticleToCategory()
                            {
                                ArticleId      = articleId,
                                BlogCategoryId = categroyId
                            }, false);
                        }
                    }
                    await articleToCategorySvc.Save();
                }
                return(articleId);
            }
        }
Example #9
0
        public async Task <List <ArticleDto> > GetAllArticlesByUserID(Guid userid)
        {
            using (var articleSvc = new ArticleService())
            {
                var list = await articleSvc.GetAllAsync().Include(m => m.User).Where(m => m.UserID == userid)
                           .Select(m => new Dto.ArticleDto()
                {
                    Title      = m.Title,
                    BadCount   = m.BadCount,
                    GoodCount  = m.GoodCount,
                    Email      = m.User.Email,
                    Content    = m.Content,
                    CreateTime = m.CreateTime,
                    ID         = m.ID,
                    ImagePath  = m.User.ImagePath
                })
                           .ToListAsync();

                using (IArticleToCategoryService articleToCategoryService = new ArticleToCategoryService())
                {
                    foreach (var articleDto in list)
                    {
                        var cates = await articleToCategoryService.GetAllAsync().Include(m => m.BlogCategory).Where(m => m.ArticleID == articleDto.ID).ToListAsync();

                        articleDto.CategoryIDs   = cates.Select(m => m.CategoryID).ToArray();
                        articleDto.CategoryNames = cates.Select(m => m.BlogCategory.CategoryName).ToArray();
                    }
                    return(list);
                }
            }
        }
        public async Task <List <ArticleDto> > GetAllArticleByUserId(Guid userId)
        {
            using (var service = new ArticleService())
            {
                var list = await service.GetAllAsync().Include(m => m.User).Where(m => m.UserId == userId).Select(m => new ArticleDto()
                {
                    Id         = m.Id,
                    Email      = m.User.Email,
                    UserId     = m.UserId,
                    CreateTime = m.CreatTime,
                    Title      = m.Title,
                    ImagePath  = m.User.ImagePath,
                    Content    = m.Content,
                    BadCount   = m.BadCount,
                    GoodCount  = m.GoodCount
                }).OrderByDescending(m => m.CreateTime).ToListAsync();

                using (IArticleToCategory articleToCategoryServic = new ArticleToCategoryService())
                {
                    foreach (var item in list)
                    {
                        var newList = await articleToCategoryServic.GetAllAsync().Include(m => m.BlogCategory).Where(m => m.ArticleId == item.Id).ToListAsync();

                        item.CategoryIds   = newList.Select(m => m.ArticleCategoryId).ToArray();
                        item.CategoryNames = newList.Select(m => m.BlogCategory.CategoryName).ToArray();
                    }
                    return(list);
                }
            }
        }
Example #11
0
        public async Task CreateArticle(string title, string content, Guid[] categoryIDs, Guid userID)
        {
            using (var articleSvc = new ArticleService())
            {
                var article = new Article()
                {
                    Title   = title,
                    Content = content,
                    UserID  = userID
                };
                await articleSvc.CreateAsync(article);

                Guid articleID = article.ID;
                using (var articleToCategorySvc = new ArticleToCategoryService())
                {
                    foreach (var categoryID in categoryIDs)
                    {
                        await articleToCategorySvc.CreateAsync(new ArticleToCategory()
                        {
                            ArticleID  = articleID,
                            CategoryID = categoryID
                        }, saved : false);
                    }
                    await articleToCategorySvc.Save();
                }
            }
        }
        public async Task <int> RemoveCategory(Guid categoryId)  //用户删除类别
        {
            using (IBlogCategoryService blogCategoryService = new BlogCategoryService())
            {
                var data = await blogCategoryService.GetAllAsync().FirstOrDefaultAsync(m => m.Id == categoryId);

                using (IArticleToCategoryService articleToCategorySvc = new ArticleToCategoryService())
                {
                    //var cate = await articleToCategorySvc.GetAllAsync()
                    //    .Include(m => m.BlogCategory)
                    //    .Where(m => m.BlogCategoryId == data.Id).ToListAsync();
                    //if (cate.Count==0)  //如果文章类别没有相关的类别就执行删除,否则提示用户先删除文章对应的类别
                    //{
                    //    await blogCategoryService.RemoveAsync(data);
                    //    return 1;
                    //}
                    //else
                    //{
                    //    return 0;
                    //}
                    await blogCategoryService.RemoveAsync(data);

                    return(1);
                }
            }
        }
        public async Task <int> RemoveArticle(Guid articleId)   //根据文章Id删除
        {
            using (IArticleService articleSvc = new ArticleService())
            {
                var data = await articleSvc.GetAllAsync().FirstOrDefaultAsync(m => m.Id == articleId);

                var acid = data.Id;
                using (IArticleToCategoryService articleToCategoryService = new ArticleToCategoryService())
                {
                    //var article=await articleToCategoryService.GetAllAsync().Include(m => m.Article).Where(m => m.ArticleId == acid).ToListAsync();
                    //if (article.Count==0)   //说明该文章未被分类可以进行删除操作
                    //{
                    //    await articleSvc.RemoveAsync(data);
                    //    return 1;
                    //}
                    //else
                    //{
                    //    return 0;
                    //}

                    await articleSvc.RemoveAsync(data);

                    return(1);
                }
            }
        }
Example #14
0
        /// <summary>
        /// 查找当前账户最新的文章(置顶或不置顶)
        /// </summary>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public async Task <List <ArticleDto> > GetCurrentUserLatestArticle(int count, Guid userId, bool isTop)
        {
            using (IArticleService articleService = new ArticleService())
            {
                var list = await articleService.GetAll().Where(m => m.User.Id == userId && m.IsTop == isTop).OrderByDescending(m => m.CreatTime).Include(m => m.User).Select(m => new ArticleDto()
                {
                    Id           = m.Id,
                    Title        = m.Title,
                    IntroContent = m.IntroContent,
                    CreateTime   = m.CreatTime,
                    Email        = m.User.Email,
                    imagePath    = m.User.ImagePath,
                    GoodCount    = m.GoodCount,
                    BadCount     = m.BadCount,
                    userId       = m.UserId
                }).Take(count).ToListAsync();

                using (IArticleToCategory articleToCategorySvc = new ArticleToCategoryService())
                {
                    foreach (var item in list)
                    {
                        var cate = await articleToCategorySvc.GetAll().Include(m => m.BlogCategory).Where(m => m.ArticleId == item.Id).ToListAsync();

                        item.CategoryIds   = cate.Select(m => m.BlogCategoryId).ToArray();
                        item.CategoryNames = cate.Select(m => m.BlogCategory.CategoryName).ToArray();
                    }
                }
                return(list);
            }
        }
Example #15
0
        /// <summary>
        /// 从所有文章中获取对应数量的文章
        /// </summary>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">数量</param>
        /// <returns></returns>
        public async Task <List <ArticleDto> > GetAllArticles(int pageIndex, int pageSize)
        {
            using (IArticleService articleSvc = new ArticleService())
            {
                var list = await articleSvc.GetAllByPageOrder(pageSize, pageIndex, m => m.Id == m.Id, false).Include(m => m.User).Select(m => new ArticleDto()
                {
                    Title        = m.Title,
                    GoodCount    = m.GoodCount,
                    BadCount     = m.BadCount,
                    Email        = m.User.Email,
                    IntroContent = m.IntroContent,
                    CreateTime   = m.CreatTime,
                    Id           = m.Id,
                    imagePath    = m.User.ImagePath
                }).ToListAsync();

                using (IArticleToCategory articleToCategorySvc = new ArticleToCategoryService())
                {
                    foreach (var articleDto in list)
                    {
                        var cates = await articleToCategorySvc.GetAll().Include(m => m.BlogCategory).Where(m => m.ArticleId == articleDto.Id).ToListAsync();

                        articleDto.CategoryIds   = cates.Select(m => m.BlogCategoryId).ToArray();
                        articleDto.CategoryNames = cates.Select(m => m.BlogCategory.CategoryName).ToArray();
                    }
                    return(list);
                }
            }
        }
        public async Task CreateArticle(string title, string content, Guid userId, Guid[] categoryIds)
        {
            using (var articleSvc = new ArticleService())
            {
                Article article = new Article()
                {
                    Title   = title,
                    Content = content,
                    UserId  = userId
                };
                await articleSvc.CreateAsync(article);

                Guid id = article.Id;
                using (var articleTocategory = new ArticleToCategoryService())
                {
                    foreach (var item in categoryIds)
                    {
                        await articleTocategory.CreateAsync(new ArticleToCategory()
                        {
                            ArticleCategoryId = item,
                            ArticleId         = id
                        }, autoSave : false);
                    }
                    await articleTocategory.Save();
                }
            }
        }
Example #17
0
        public async Task <ArticleDto> GetOneArticleById(Guid articleId)
        {
            using (IArticleService articleSvc = new ArticleService())
            {
                var data = await articleSvc.GetAll().Where(m => m.Id == articleId).Include(m => m.User).Select(m => new ArticleDto()
                {
                    Id           = m.Id,
                    Title        = m.Title,
                    IntroContent = m.IntroContent,
                    Content      = m.Content,
                    CreateTime   = m.CreatTime,
                    Email        = m.User.Email,
                    userId       = m.UserId,
                    imagePath    = m.User.ImagePath,
                    GoodCount    = m.GoodCount,
                    BadCount     = m.BadCount
                }).FirstAsync();

                using (IArticleToCategory articleToCategorySvc = new ArticleToCategoryService())
                {
                    var cates = await articleToCategorySvc.GetAll().Include(m => m.BlogCategory).Where(m => m.ArticleId == data.Id).ToListAsync();

                    data.CategoryIds   = cates.Select(m => m.BlogCategoryId).ToArray();
                    data.CategoryNames = cates.Select(m => m.BlogCategory.CategoryName).ToArray();
                    return(data);
                }
            }
        }
Example #18
0
 public async Task <int> GetArticleDataCount(Guid userId, Guid categoryId)
 {
     using (IArticleToCategory articleToCategorySvc = new ArticleToCategoryService())
     {
         return(await articleToCategorySvc.GetAll().CountAsync(m => m.Article.UserId == userId && m.BlogCategoryId == categoryId));
     }
 }
        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(); //循环完再保存
                }
            }
        }
 public async Task <List <ArticleToBlogcateDto> > GetAllArticleTocate()
 {
     using (IArticleToCategoryService articleToCategory = new ArticleToCategoryService())
     {
         return(await articleToCategory.GetAllAsync()
                .Where(m => m.IsRemoved == false)
                .Where(m => m.Article.State == true) //已发布的文章
                .Include(m => m.BlogCategory)
                .Select(m => new ArticleToBlogcateDto()
         {
             CateName = m.BlogCategory.CategoryName,
             BlogCategoryId = m.BlogCategoryId,
             ArticleId = m.ArticleId
         }).ToListAsync());
     }
 }
 public async Task <List <ArticleToBlogcateDto> > GetAllArticleTocateByUserId(Guid userid)  // 根据用户找到对应的类别下的文章并且统计数量
 {
     using (IArticleToCategoryService articleToCategory = new ArticleToCategoryService())
     {
         return(await articleToCategory.GetAllAsync()
                .Include(m => m.BlogCategory)
                .Include(m => m.Article)
                .Where(m => m.IsRemoved == false)
                .Where(m => m.Article.State == true)     //文章已发布
                .Where(m => m.Article.UserId == userid)
                .Select(m => new ArticleToBlogcateDto()
         {
             BlogCategoryId = m.BlogCategoryId,
             CateName = m.BlogCategory.CategoryName,
             ArticleId = m.ArticleId
         }).ToListAsync());
     }
 }
Example #22
0
        public async Task RemoveArticle(Guid articleId)
        {
            using (IArticleService articleSvc = new ArticleService())
            {
                Article article = await articleSvc.GetOneByIdAsync(articleId);

                await articleSvc.DeleteAsync(article);
            }
            using (IArticleToCategory articleToCategorySvc = new ArticleToCategoryService())
            {
                List <ArticleToCategory> list = await articleToCategorySvc.GetAll().Where(m => m.ArticleId == articleId).ToListAsync();

                foreach (var item in list)
                {
                    await articleToCategorySvc.DeleteAsync(item, false);
                }
                await articleToCategorySvc.Save();
            }
        }
Example #23
0
        public async Task EditArticle(Guid articleId, string title, string content, string introContent, Guid[] categoryIds)
        {
            using (IArticleService articleSvc = new ArticleService())
            {
                ////处理内容
                //string IntroContent = FilterHTML(content);
                //获取当前文章的实体,修改标题和内容
                var article = await articleSvc.GetOneByIdAsync(articleId);

                article.Title        = title;
                article.Content      = content;
                article.IntroContent = introContent;
                article.IsTop        = categoryIds != null?categoryIds.Contains(Guid.Parse("00000000-0000-0000-0000-000000000001")) : false;

                await articleSvc.EditAsync(article);

                using (IArticleToCategory articleToCategorySvc = new ArticleToCategoryService())
                {
                    //循环删除一篇文章的多个分类
                    foreach (var articleToCategory in articleToCategorySvc.GetAll().Where(m => m.ArticleId == articleId))
                    {
                        await articleToCategorySvc.DeleteAsync(articleToCategory, false);
                    }
                    //循环创建一篇文章多个分类
                    if (categoryIds != null)//当分类不为空时才循环添加
                    {
                        foreach (var categoryId in categoryIds)
                        {
                            await articleToCategorySvc.CreatAsync(new ArticleToCategory()
                            {
                                ArticleId      = articleId,
                                BlogCategoryId = categoryId
                            }, false);
                        }
                    }
                    await articleToCategorySvc.Save();
                }
            }
        }
Example #24
0
        public async Task <List <BlogCategoryDto> > GetAllCategoriesByUserId(Guid userId, int pageIndex, int pageSize)
        {
            using (IBlogCategory blogCategorySvc = new BlogCategoryService())
            {
                Guid allId = Guid.Parse("00000000-0000-0000-0000-000000000001");
                var  data  = await blogCategorySvc.GetAllByPageOrder(pageSize, pageIndex, m => m.UserId == userId || m.Id == allId, false).Select(m => new BlogCategoryDto()
                {
                    Id = m.Id,
                    BlogCategoryName = m.CategoryName,
                    CreateTime       = m.CreatTime
                }).ToListAsync();

                using (IArticleToCategory articleToCategorySvc = new ArticleToCategoryService())
                {
                    foreach (var item in data)
                    {
                        item.ArticleCount = await articleToCategorySvc.GetAll().Where(m => m.BlogCategoryId == item.Id).CountAsync();
                    }
                }
                return(data);
            }
        }
        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();  //循环完后保存
                }
            }
        }
        public async Task <ArticleDto> GetOneArticleById(Guid articleId)  //根据文章Id查询
        {
            using (IArticleService articleSvc = new ArticleService())
            {
                var data = await articleSvc.GetAllAsync()
                           .Include(m => m.User)
                           .Where(m => m.Id == articleId)
                           .Select(m => new ArticleDto()
                {
                    Id                = m.Id,
                    Title             = m.Title,
                    Content           = m.Content,
                    CreateTime        = m.CreateTime,
                    NickName          = m.User.NickName,
                    GoodCount         = m.GoodCounnt,
                    BadCount          = m.BadCount,
                    UserId            = m.UserId,
                    ImagePath         = m.User.ImagePath,
                    State             = m.State,
                    IsClosingComments = m.IsClosingComments
                }).FirstAsync();

                using (IArticleToCategoryService articleToCategorySvc = new ArticleToCategoryService())
                {
                    var cate = await articleToCategorySvc.GetAllAsync()
                               .Include(m => m.BlogCategory)
                               .Where(m => m.ArticleId == data.Id)
                               .Where(m => m.BlogCategory.IsRemoved == false)
                               .ToListAsync();

                    data.CategoryIds   = cate.Select(m => m.BlogCategoryId).ToArray();  //转化为数组Id
                    data.CategoryNames = cate.Select(m => m.BlogCategory.CategoryName).ToArray();
                    return(data);
                }
            }
        }
        public async Task <List <ArticleToBlogcateDto> > GetAllArticlesByCategoryId(Guid blogcateId)
        {
            using (IArticleToCategoryService articleToCategory = new ArticleToCategoryService())
            {
                var data = await articleToCategory.GetAllAsync()
                           .Include(m => m.Article)
                           .Include(m => m.Article.User)
                           .Where(m => m.BlogCategoryId == blogcateId)
                           .Select(m => new ArticleToBlogcateDto()
                {
                    ArticleId  = m.ArticleId,
                    Title      = m.Article.Title,
                    NickName   = m.Article.User.NickName,
                    CateName   = m.BlogCategory.CategoryName,
                    CreateTime = m.Article.CreateTime
                }).ToListAsync();

                return(data);
            }
            //using (IBlogCategoryService blogCategory = new BlogCategoryService())
            //{
            //    var data = await blogCategory.GetAllAsync().Where(m => m.UserId == userid).ToListAsync();
            //    foreach (var item in data)
            //    {
            //        var bid = item.Id;
            //        using (IArticleToCategoryService articleToCategory = new ArticleToCategoryService())
            //        {
            //            return await articleToCategory.GetAllAsync().Where(m => m.BlogCategoryId == bid).Include(m => m.BlogCategory)
            //                .Select(m => new BlogCategoryDto()
            //            {
            //                CategoryName=m.BlogCategory.CategoryName
            //            }).ToListAsync();
            //        }
            //    }
            //}
        }
Example #28
0
        /// <summary>
        /// 从所有文章中查找符合查找内容和对应数量的文章
        /// </summary>
        /// <param name="searchWord">关键字</param>
        /// <param name="searchType">查询类型(0-查用户名和标题 1-查标题 2-查用户名)</param>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">数量</param>
        /// <returns></returns>
        public async Task <List <ArticleDto> > GetAllSearchArticles(string searchWord, int searchType, int pageIndex, int pageSize)
        {//searchType:0-查用户名和标题 1-查标题 2-查用户名
            using (IArticleService articleSvc = new ArticleService())
            {
                List <ArticleDto> list = null;
                if (searchType == 0)//查用户名和标题
                {
                    list = await articleSvc.GetAllByPageOrder(pageSize, pageIndex, m => m.Title.Contains(searchWord) || m.User.Email.Contains(searchWord), false).Include(m => m.User).Select(m => new ArticleDto()
                    {
                        Title      = m.Title,
                        GoodCount  = m.GoodCount,
                        BadCount   = m.BadCount,
                        Email      = m.User.Email,
                        Content    = m.IntroContent,
                        CreateTime = m.CreatTime,
                        Id         = m.Id,
                        imagePath  = m.User.ImagePath,
                        userId     = m.User.Id
                    }).ToListAsync();
                }
                else if (searchType == 1)//查标题
                {
                    list = await articleSvc.GetAllByPageOrder(pageSize, pageIndex, m => m.Title.Contains(searchWord), false).Include(m => m.User).Select(m => new ArticleDto()
                    {
                        Title      = m.Title,
                        GoodCount  = m.GoodCount,
                        BadCount   = m.BadCount,
                        Email      = m.User.Email,
                        Content    = m.IntroContent,
                        CreateTime = m.CreatTime,
                        Id         = m.Id,
                        imagePath  = m.User.ImagePath,
                        userId     = m.User.Id
                    }).ToListAsync();
                }
                else if (searchType == 2)//查用户名
                {
                    list = await articleSvc.GetAllByPageOrder(pageSize, pageIndex, m => m.User.Email.Contains(searchWord), false).Include(m => m.User).Select(m => new ArticleDto()
                    {
                        Title      = m.Title,
                        GoodCount  = m.GoodCount,
                        BadCount   = m.BadCount,
                        Email      = m.User.Email,
                        Content    = m.IntroContent,
                        CreateTime = m.CreatTime,
                        Id         = m.Id,
                        imagePath  = m.User.ImagePath,
                        userId     = m.User.Id
                    }).ToListAsync();
                }

                using (IArticleToCategory articleToCategorySvc = new ArticleToCategoryService())
                {
                    foreach (var articleDto in list)
                    {
                        var cates = await articleToCategorySvc.GetAll().Include(m => m.BlogCategory).Where(m => m.ArticleId == articleDto.Id).ToListAsync();

                        articleDto.CategoryIds   = cates.Select(m => m.BlogCategoryId).ToArray();
                        articleDto.CategoryNames = cates.Select(m => m.BlogCategory.CategoryName).ToArray();
                    }
                    return(list);
                }
            }
        }