예제 #1
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();
            }
        }
예제 #2
0
        public async Task EditStickyPostsByArticle(Guid id)  //设置文章置顶
        {
            using (IArticleService article = new ArticleService())
            {
                var data = await article.GetAllAsync().FirstAsync(m => m.Id == id);

                if (data != null)
                {
                    if (data.IsStick == 0)  //设置置顶
                    {
                        data.IsStick = 1;
                    }
                    else
                    {
                        data.IsStick = 0;   //取消置顶
                    }
                    await article.EditAsync(data);
                }
            }
        }
예제 #3
0
        public async Task <List <ArticleDto> > GetAllArticlesByNickName(string nickName, string title, bool state)   //后台模糊查询(根据作者查询)
        {
            using (IArticleService articleService = new ArticleService())
            {
                var data = await articleService.GetAllAsync()
                           .Include(m => m.User)
                           .Where(m => m.State == state)
                           .Where(m => string.IsNullOrEmpty(nickName) & string.IsNullOrEmpty(title) || m.User.NickName.Contains(nickName) & m.Title.Contains(title))
                           .OrderByDescending(m => m.CreateTime)
                           .Select(m => new ArticleDto()
                {
                    Id         = m.Id,
                    Title      = m.Title,
                    NickName   = m.User.NickName,
                    State      = m.State,
                    CreateTime = m.CreateTime,
                }).ToListAsync();

                return(data);
            }
        }
예제 #4
0
 public async Task <List <ArticleDto> > GetAllArticleIsFreeze(string search) //查询所有文章显示在主页(用户也可模糊查询)
 {
     using (IArticleService articleSvc = new ArticleService())
     {
         return(await articleSvc.GetAllAsync().Include(m => m.User)
                .Where(m => m.State == true & m.User.IsFreeze == false)
                .Where(m => string.IsNullOrEmpty(search) || m.Title.Contains(search) || m.User.NickName.Contains(search))
                .Select(m => new ArticleDto()
         {
             Id = m.Id,
             Title = m.Title,
             Content = m.Content,
             CreateTime = m.CreateTime,
             GoodCount = m.GoodCounnt,
             BadCount = m.BadCount,
             ImagePath = m.User.ImagePath,
             State = true,
             NickName = m.User.NickName,
             UserId = m.UserId
         }).ToListAsync());
     }
 }
예제 #5
0
 public async Task <int> GetSearchArticleDataCount(string searchWord, int searchType)
 {//searchType:0-查用户名和标题 1-查标题 2-查用户名
     using (IArticleService articleSvc = new ArticleService())
     {
         if (searchType == 0)//查用户名和标题
         {
             return(await articleSvc.GetAll().CountAsync(m => m.Title.Contains(searchWord) || m.User.Email.Contains(searchWord)));
         }
         else if (searchType == 1)//查标题
         {
             return(await articleSvc.GetAll().CountAsync(m => m.Title.Contains(searchWord)));
         }
         else if (searchType == 2)//查用户名
         {
             return(await articleSvc.GetAll().CountAsync(m => m.User.Email.Contains(searchWord)));
         }
         else
         {
             return(0);
         }
     }
 }
예제 #6
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();
                }
            }
        }
예제 #7
0
        /// <summary>
        /// 点赞
        /// </summary>
        /// <param name="articleId">文章id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public async Task GoodCountAdd(Guid articleId, Guid userId)
        {
            int likeCount;
            int hateCount;

            using (ILikeHateService likeHateSvc = new LikeHateService())
            {
                string result = await GetLikeHate(articleId, userId);

                if (result == "none")//没有创建过
                {
                    LikeHate likeHate = new LikeHate()
                    {
                        ArticleId = articleId, UserId = userId, Like = true, Hate = false
                    };
                    await likeHateSvc.CreatAsync(likeHate);
                }
                else if (result == "null")//创建了但都为false
                {
                    LikeHate likeHate = await likeHateSvc.GetAll().Where(m => m.ArticleId == articleId && m.UserId == userId && !m.Like && !m.Hate).FirstAsync();

                    likeHate.Like = true;
                    await likeHateSvc.EditAsync(likeHate);
                }
                likeCount = await likeHateSvc.GetAll().Where(m => m.ArticleId == articleId && m.Like).CountAsync();

                hateCount = await likeHateSvc.GetAll().Where(m => m.ArticleId == articleId && m.Hate).CountAsync();
            }

            using (IArticleService articleSvc = new ArticleService())
            {
                var article = await articleSvc.GetOneByIdAsync(articleId);

                article.BadCount  = hateCount;
                article.GoodCount = likeCount;
                await articleSvc.EditAsync(article);
            }
        }
예제 #8
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();  //循环完后保存
                }
            }
        }
예제 #9
0
        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);
                }
            }
        }
예제 #10
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);
                }
            }
        }