Exemplo n.º 1
0
 public void AddText(BlogText addThis)        //新增文章,参数为BlogText的实体
 {
     using (BlogContext db = new BlogContext())
     {
         BlogText before = new BlogText();              //连接前后文
         before = db.BlogTexts.ToList().LastOrDefault();
         BlogText newBlog;
         if (before == null)
         {
             newBlog = db.BlogTexts.Add(new BlogText {
                 TextTitle = addThis.TextTitle, CategoryName = addThis.CategoryName, Text = addThis.Text, FirstView = addThis.FirstView
             });
         }
         else
         {
             newBlog = db.BlogTexts.Add(new BlogText {
                 TextTitle = addThis.TextTitle, CategoryName = addThis.CategoryName, Text = addThis.Text, FirstView = addThis.FirstView, PreID = before.TextID
             });
         }
         db.SaveChanges();
         if (before != null)
         {
             db.BlogTexts.Find(before.TextID).NexID = newBlog.TextID;
         }
         db.SaveChanges();
     }
 }
Exemplo n.º 2
0
        public bool UpdateText(UpdateText blogText)        //更新文章
        {
            bool isSuccess = false;

            try
            {
                BlogText blog = new BlogText
                {
                    Text         = blogText.Text,
                    FirstView    = getFirstView(blogText.Text),
                    TextTitle    = blogText.Title,
                    CategoryName = blogText.Category
                };
                if (string.IsNullOrEmpty(blog.CategoryName))
                {
                    blog.CategoryName = "未分类";
                }
                if (blogText.Id == 0)                //新增文章
                {
                    repository.AddText(blog);
                }
                else                //修改文章
                {
                    blog.TextID = blogText.Id;
                    RemoveFiles(getRemovedAttachmentUrl(repository.GetTextByID(blog.TextID).Text, blog.Text)); //删除失效的附件
                    repository.UpdateText(blog.TextID, blog);                                                  //实际上是替换掉分类名、文章内容、文章标题、文章摘要四项
                }
                isSuccess = true;
            }
            catch
            {
            }
            return(isSuccess);
        }
Exemplo n.º 3
0
 public void AddTextHot(int id)//增加文章点击量,参数为文章ID号
 {
     using (BlogContext db = new BlogContext())
     {
         BlogText item = db.BlogTexts.Find(id);
         item.Hot++;
         db.SaveChanges();
     }
 }
Exemplo n.º 4
0
 public void UpdateText(int textID, BlogText updateThis)       //更新文章,参数为(文章ID号,更新后的文章BlogText)
 {
     using (BlogContext db = new BlogContext())
     {
         BlogText item = db.BlogTexts.Find(textID);
         item.CategoryName = updateThis.CategoryName;
         item.Text         = updateThis.Text;
         item.TextTitle    = updateThis.TextTitle;
         item.FirstView    = updateThis.FirstView;
         db.SaveChanges();
     }
 }
Exemplo n.º 5
0
        public IActionResult Create(BlogText model)
        {
            ViewBag.Blog = _blogrepository.GetBlogText();

            if (model == null)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                model.AddedBy   = _admin.Name;
                model.AddedDate = DateTime.Now;

                _blogrepository.AddBlogtext(model);
                return(RedirectToAction("index"));
            }
            return(View(model));
        }
Exemplo n.º 6
0
        public IActionResult Edit(BlogText model)
        {
            if (model == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                var updateBlogTExt = _blogrepository.GetBlogTextById(model.Id);

                updateBlogTExt.ModifiedBy   = _admin.Name;
                updateBlogTExt.ModifiedDate = DateTime.Now;



                _blogrepository.UpdateBlogtext(updateBlogTExt, model);

                return(RedirectToAction("index"));
            }
            return(View(model));
        }
Exemplo n.º 7
0
 public void UpdateBlogtext(BlogText updateBlogTExt, BlogText model)
 {
     updateBlogTExt.BlogId = model.BlogId;
     updateBlogTExt.Text   = model.Text;
     _context.SaveChanges();
 }
Exemplo n.º 8
0
 public BlogText AddBlogtext(BlogText model)
 {
     _context.BlogTexts.Add(model);
     _context.SaveChanges();
     return(model);
 }
Exemplo n.º 9
0
 public void RemoveBlogText(BlogText model)
 {
     _context.BlogTexts.Remove(model);
     _context.SaveChanges();
 }