Example #1
0
        /// <summary>
        /// 新增文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public async Task <Article> AddArticle(Article article)
        {
            using (var addDbContext = new DiscoveryDbContext())
            {
                //插入文章并且插入文章和标签的关系
                if (string.IsNullOrEmpty(article.CategoryTags) == false)
                {
                    var Categorys        = JsonConvert.DeserializeObject <List <JObject> >(article.CategoryTags).Select(t => t.Value <int>("key"));
                    var articleCategorys = await addDbContext.Category.Where(t => Categorys.Contains(t.Id)).ToListAsync();

                    //插入文章
                    addDbContext.Article.Add(article);
                    await addDbContext.SaveChangesAsync();

                    foreach (var Category in articleCategorys)
                    {
                        var articleCategory = new ArticleCategory {
                            ArticleId = article.PKID, CategoryTagId = Category.Id
                        };
                        addDbContext.ArticleCategory.Add(articleCategory);
                    }
                    await addDbContext.SaveChangesAsync();
                }
                else
                {
                    //插入文章
                    addDbContext.Article.Add(article);
                    await addDbContext.SaveChangesAsync();
                }

                return(article);
            }
        }
        /// <summary>
        /// 收藏或取消收藏文章
        /// </summary>
        /// <param name="articleFavorite">文章收藏模型</param>
        /// <returns></returns>
        public async Task <int> AddOrUpdateArticleFavorite(ArticleFavorite articleFavorite)
        {
            var oldArticleFavorite = await dbContext.ArticleFavorite.SingleOrDefaultAsync(af => af.PKID == articleFavorite.PKID && af.UserId == articleFavorite.UserId);

            try
            {
                //取消收藏文章
                if (oldArticleFavorite != null)
                {
                    dbContext.ArticleFavorite.Remove(oldArticleFavorite);
                    await dbContext.SaveChangesAsync();

                    return(Success);
                }
                //收藏文章
                else
                {
                    dbContext.ArticleFavorite.Add(articleFavorite);
                    await dbContext.SaveChangesAsync();

                    return(Success);
                }
            }
            catch (Exception ex)
            {
                //记录日志

                return(Error);
            }
        }
Example #3
0
        /// <summary>
        /// 更新文章状态
        /// </summary>
        /// <param name="articleId">文章Id</param>
        /// <param name="status">状态值</param>
        /// <returns></returns>
        public async Task <Article> UpdateArticleStatus(int articleId, ArticleStatus status)
        {
            var updateArticle = await discoveryDbContext.Article.FirstOrDefaultAsync(article => article.PKID == articleId);

            if (updateArticle == null)
            {
                throw new Exception("文章不存在");
            }

            updateArticle.Status             = status.ToString();
            updateArticle.LastUpdateDateTime = DateTime.Now;
            //如果将文章撤回,并且当前文章为置顶状态,则设置为非置顶
            if (status == ArticleStatus.Withdrew && updateArticle.IsTopMost.HasValue && updateArticle.IsTopMost.Value == true)
            {
                updateArticle.IsTopMost = false;
            }

            await discoveryDbContext.SaveChangesAsync();

            if (status == ArticleStatus.Withdrew)
            {
                await InsertDataChangeRecord(updateArticle, DataOperationEnum.Withdrew);
            }
            return(updateArticle);
        }
Example #4
0
        /// <summary>
        /// 修改Category标签
        /// </summary>
        /// <param name="Category">要更新的标签对象</param>
        /// <returns></returns>
        public async Task <Category> UpdateCategory(Category Category)
        {
            var currentCategory = await discoveryDbContext.Category.FirstOrDefaultAsync(t => t.Id == Category.Id);

            //var currentCategory = discoveryDbContext.Category.SingleOrDefault(t => t.Id == Category.Id);
            if (currentCategory == null)
            {
                return(null);
            }

            currentCategory.Name     = Category.Name;
            currentCategory.Image    = Category.Image;
            currentCategory.Describe = Category.Describe;
            //currentCategory.ParentId = Category.ParentId;

            await discoveryDbContext.SaveChangesAsync();

            return(Category);
        }
Example #5
0
        public async Task <int> Modify(Comment model, params string[] proNames)
        {
            discoveryDbContext.Set <Comment>().Attach(model);
            //将对象添加到EF中
            DbEntityEntry entry = discoveryDbContext.Entry <Comment>(model);

            //先设置对象的包装 状态为Unchanged
            entry.State = EntityState.Unchanged;
            //将每个 被修改的属性的状态 设置为已修改状态;后面生成update语句时,就只为已修改的属性 更新
            foreach (string proName in proNames)
            {
                //将每个 被修改的属性的状态 设置为已修改状态;后面生成update语句时,就只为已修改的属性 更新
                entry.Property(proName).IsModified = true;
            }
            return(await discoveryDbContext.SaveChangesAsync());
        }
Example #6
0
        /// <summary>
        /// 新增评论
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        public async Task <Comment> AddComment(Comment comment)
        {
            using (var addDBContext = new DiscoveryDbContext())
            {
                if (comment.ParentID > 0)
                {
                    Comment reply = await addDBContext.Comment.FirstOrDefaultAsync(m => m.Id == comment.ParentID);

                    if (reply == null)
                    {
                        return(comment);
                    }
                    comment.ReplyComment = reply;
                    comment.ParentID     = reply.Id;
                    //await discoveryDbContext.SaveChangesAsync();
                }
                addDBContext.Comment.Add(comment);
                await addDBContext.SaveChangesAsync();

                return(comment);
            }
        }
Example #7
0
        /// <summary>
        /// 修改Article
        /// </summary>
        /// <param name="article">Article模型</param>
        /// <returns></returns>
        public async Task <Article> UpdateArticle(Article article)
        {
            using (var updateDbContext = new DiscoveryDbContext())
            {
                var updateArticle = await updateDbContext.Article.FirstOrDefaultAsync(a => a.PKID == article.PKID);

                //var updateArticle = updateDbContext.Article.Include("ArticleCategorys").SingleOrDefault(a => a.Id == article.Id);
                if (updateArticle == null)
                {
                    throw new Exception("文章不存在");
                }

                //清空旧的文章标签关系
                //updateArticle.ArticleCategorys.Clear();


                updateArticle.SmallTitle         = article.SmallTitle;
                updateArticle.BigTitle           = article.SmallTitle;
                updateArticle.Content            = article.ContentHtml;
                updateArticle.ContentHtml        = article.ContentHtml;
                updateArticle.CoverImage         = article.CoverImage;
                updateArticle.CoverTag           = article.CoverTag;
                updateArticle.CoverMode          = article.CoverMode;
                updateArticle.CategoryTags       = article.CategoryTags;
                updateArticle.Category           = article.Category;
                updateArticle.LastUpdateDateTime = article.LastUpdateDateTime;
                updateArticle.Status             = article.Status;
                updateArticle.IsShow             = article.IsShow;
                if (article.PublishDateTime.HasValue)
                {
                    updateArticle.PublishDateTime = article.PublishDateTime;
                }
                updateArticle.Brief         = article.Brief;
                updateArticle.Image         = article.Image;
                updateArticle.SmallImage    = article.SmallImage;
                updateArticle.ShowImages    = article.ShowImages;
                updateArticle.ShowType      = article.ShowType;
                updateArticle.IsShowTouTiao = article.IsShowTouTiao;
                updateArticle.QRCodeImg     = article.QRCodeImg;
                updateArticle.Type          = article.Type;
                //文章存在关联的标签
                if (string.IsNullOrEmpty(article.CategoryTags) == false)
                {
                    //文章关联的标签

                    var addArticleCategorys = JsonConvert.DeserializeObject <List <JObject> >(article.CategoryTags).Select(t => t.Value <int>("key"));
                    var articleCategorys    = await updateDbContext.Category.Where(t => addArticleCategorys.Contains(t.Id)).ToListAsync();

                    var oldArtilceCategorys = updateDbContext.ArticleCategory.Where(at => at.ArticleId == updateArticle.PKID);
                    updateDbContext.ArticleCategory.RemoveRange(oldArtilceCategorys);
                    foreach (var Category in articleCategorys)
                    {
                        var articleCategory = new ArticleCategory {
                            ArticleId = article.PKID, CategoryTagId = Category.Id
                        };
                        updateDbContext.ArticleCategory.Add(articleCategory);
                    }
                }

                await updateDbContext.SaveChangesAsync();

                //await InsertDataChangeRecord(updateArticle, DataOperationEnum.Update);

                return(article);
            }
        }