Esempio n. 1
0
        /// <summary>
        /// 添加博客
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public static int AddArticle(Article article)
        {
            var articleTemp = article;
            using (BlogEntities blogEntities = new BlogEntities())
            {
                var ArticleLabelList = article.ArticleLabel;
                article.ArticleLabel = null;
                var newlabelList = new List<ArticleLabel>();
                foreach (var label in ArticleLabelList)
                {
                    var labelFromDb = (from l in blogEntities.ArticleLabel where l.Id == label.Id select l).FirstOrDefault();

                    newlabelList.Add(labelFromDb);
                }
                article.ArticleLabel = newlabelList;
                blogEntities.Article.Add(article);
                blogEntities.SaveChanges();
                articleTemp = article;
            }
            return articleTemp.Id;
        }
Esempio n. 2
0
        //[ValidateInput(false)]
        public ActionResult SubmitBlogEditor(SubmitBlogEditorRequest request)
        {
            Article article = new Article();


            if (request.Id != null)
                article.Id = request.Id.Value;

            article.ArticleLabel = AnalyzeLabelString(request.LabelString);
            article.Title = request.Title.Trim();
            article.Content = request.Content;
            article.PublicationTime = DateTime.Now;
            if (request.Id == null)
            {
                ArticleDal.AddArticle(article);
            }
            else
            {
                ArticleDal.UpdateArticle(article);
            }
            return Redirect("Index");
        }
Esempio n. 3
0
        /// <summary>
        /// 更新博客
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public static int UpdateArticle(Article article)
        {
            using (BlogEntities blogEntities = new BlogEntities())
            {
                var changeArticle = blogEntities.Article.First(a => a.Id == article.Id);

                //changeArticle.PublicationTime = article.PublicationTime;
                changeArticle.Title = article.Title;
                changeArticle.Content = article.Content;
                var ArticleLabelList = article.ArticleLabel;
                var newLabelList =new List<ArticleLabel>();
                var oldLabelList = new List<ArticleLabel>(changeArticle.ArticleLabel);
                foreach (var label in ArticleLabelList)
                {
                    var labelFromDb = (from l in blogEntities.ArticleLabel where l.Id == label.Id select l).FirstOrDefault();
                    newLabelList.Add(labelFromDb);
                }

                foreach (var label in newLabelList)
                {
                    if (!changeArticle.ArticleLabel.Contains(label))
                    {
                        changeArticle.ArticleLabel.Add(label);
                    }
                }

                foreach (var label in oldLabelList)
                {
                    if (!newLabelList.Contains(label))
                    {
                        changeArticle.ArticleLabel.Remove(label);
                    }
                }
                blogEntities.SaveChanges();
            }
            return article.Id;
        }