コード例 #1
0
        /// <summary>
        /// 置顶或取消置顶文章
        /// </summary>
        /// <param name="articleId">文章Id</param>
        /// <returns></returns>
        public async Task <int> UpdateArticleTopMost(int articleId, string op)
        {
            using (var updateDbContext = new DiscoveryDbContext())
            {
                var updateArticle = await discoveryDbContext.Article.FirstOrDefaultAsync(a => a.PKID == articleId);

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

                //当前为非置顶状态
                if (updateArticle.IsTopMost == null || updateArticle.IsTopMost == false)
                {
                    updateArticle.LastUpdateDateTime = DateTime.Now;
                }

                //修改当前文章为置顶(取消置顶)
                updateArticle.IsTopMost = op == "top" ? true : false;

                await discoveryDbContext.SaveChangesAsync();

                //if (updateArticle.IsTopMost.HasValue && updateArticle.IsTopMost.Value==true)
                //    await InsertDataChangeRecord(updateArticle, DataOperationEnum.TopBest);
                return(Success);
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
0
 public List <DiscoveryGoodsRelation> GetDiscoveryRelationGoods(Guid discoveryId)
 {
     using (var dbContext = new DiscoveryDbContext())
     {
         var goods = dbContext.DiscoveryGoodsRelations.Include(dg => dg.Goods).Where(g => g.DiscoveryId.Equals(discoveryId));
         return(goods.ToList());
     }
 }
コード例 #4
0
 public List <Models.Discovery> GetDiscoveryByPage(int pageIndex, int pageSize, out int totalCount)
 {
     using (var dbContext = new DiscoveryDbContext())
     {
         var query = dbContext.Discoveries.Where(me => me.Status == DiscoveryStatus.Normal).OrderByDescending(me => me.CreateTime);
         totalCount = query.Count();
         return(query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList());
     }
 }
コード例 #5
0
        public DbContext GetDbContext()
        {
            //从当前线程中获取EF上下文
            DbContext dbContext = CallContext.GetData(typeof(DBContextFactory).Name) as DbContext;

            if (dbContext == null)
            {
                dbContext = new  DiscoveryDbContext();
                CallContext.SetData(typeof(DBContextFactory).Name, dbContext);
            }
            return(dbContext);
        }
コード例 #6
0
        public Guid SaveDiscovery(Models.Discovery model, List <Guid> goodsIds)
        {
            bool result;

            using (var dbContext = new DiscoveryDbContext())
            {
                model.LastUpdateTime = DateTime.Now;

                //新增
                if (model.Id == Guid.Empty)
                {
                    model.Id         = KeyGenerator.GetGuidKey();
                    model.CreateTime = DateTime.Now;
                    var currentUser = _userContainer.CurrentUser;
                    model.CreateUserId = currentUser.Id;
                    model.CreateName   = currentUser.UserName;
                    model.Status       = DiscoveryStatus.Normal;
                    dbContext.Discoveries.Add(model);
                }
                else//编辑
                {
                    //删除商品关联
                    var goods = dbContext.DiscoveryGoodsRelations.Where(x => x.DiscoveryId == model.Id).ToList();
                    goods.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted);

                    dbContext.Discoveries.Attach(model);
                    dbContext.Entry(model).State = EntityState.Modified;
                }

                //关联商品
                foreach (var goodsId in goodsIds)
                {
                    DiscoveryGoodsRelation relation = new DiscoveryGoodsRelation
                    {
                        Id          = KeyGenerator.GetGuidKey(),
                        DiscoveryId = model.Id,
                        GoodsId     = goodsId
                    };
                    dbContext.DiscoveryGoodsRelations.Add(relation);
                }

                result = dbContext.SaveChanges() > 0;
            }

            if (!result)
            {
                return(Guid.Empty);
            }

            return(model.Id);
        }
コード例 #7
0
        public static async Task <Article> SelectArticleDetailByTitle(string title)
        {
            using (var detailDbcontext = new DiscoveryDbContext())
            {
                //查询当前文章详情
                var article = await detailDbcontext.Article.FirstOrDefaultAsync <Article>(a => a.SmallTitle == title);

                if (article == null)
                {
                    throw new Exception("文章不存在");
                }
                return(article);
            }
        }
コード例 #8
0
        /// <summary>
        /// 根据文章Id查询文章详情
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public async Task <Article> SelectArticleDetailById(int articleId)
        {
            using (var detailDbcontext = new DiscoveryDbContext())
            {
                //查询当前文章详情
                var article = await detailDbcontext.Article.FirstOrDefaultAsync <Article>(a => a.PKID == articleId);

                if (article == null)
                {
                    throw new Exception("文章不存在");
                }
                return(article);
            }
        }
コード例 #9
0
        public List <Article> SearchShowArticle()
        {
            using (var searchDbContext = new DiscoveryDbContext())
            {
                var articles = searchDbContext.Article.Where(article => (
                                                                 (
                                                                     article.Type == 1 ||
                                                                     article.Type == 0
                                                                 ) &&
                                                                 article.IsShow == 1));

                return(articles.ToList());
            }
        }
コード例 #10
0
        public List <Article> SearchPublishArticle()
        {
            using (var searchDbContext = new DiscoveryDbContext("name=Tuhu_Discovery_Db_ReadOnly"))
            {
                var articles = searchDbContext.Article.Where(article => (
                                                                 (
                                                                     article.Status == ArticleStatus.Published.ToString() ||
                                                                     article.Status == ArticleStatus.PrePublish.ToString()
                                                                 ) &&
                                                                 article.Type == 5));

                return(articles.ToList());
            }
        }
コード例 #11
0
        public void DeleteDiscovery(Guid id)
        {
            using (var dbContext = new DiscoveryDbContext())
            {
                //删除发现文章
                var discovery = dbContext.Discoveries.FirstOrDefault(x => x.Id == id);
                dbContext.Entry(discovery).State = EntityState.Deleted;
                //删除关联商品
                var goods = dbContext.DiscoveryGoodsRelations.Where(x => x.DiscoveryId == id).ToList();
                goods.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted);

                dbContext.SaveChanges();
            }
        }
コード例 #12
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);
            }
        }
コード例 #13
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);
            }
        }