Example #1
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 #2
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);
                }
            }
        }
Example #3
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 #4
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 #5
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));
     }
 }
Example #6
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 #7
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 #8
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);
                }
            }
        }
Example #9
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 #10
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);
            }
        }
Example #11
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 #12
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);
                }
            }
        }