Exemplo n.º 1
0
        public ActionResult DeleteConfirmed(int id)
        {
            tbl_blog tbl_blog = db.tbl_blog.Find(id);

            db.tbl_blog.Remove(tbl_blog);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 新增博客
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <CommonResultDto <string> > Post(BlogAddDto dto)
        {
            using (await _context.Database.BeginTransactionAsync())
            {
                System.ComponentModel.NullableConverter nullableDateTime = new System.ComponentModel.NullableConverter(typeof(DateTime?));
                string           blogContentId = Guid.NewGuid().ToString();
                string           blogId        = Guid.NewGuid().ToString();
                tbl_blog_content tbl_content   = new tbl_blog_content
                {
                    Id      = blogContentId,
                    Content = dto.Content
                };

                tbl_blog tbl_blog = new tbl_blog
                {
                    Id         = blogId,
                    Title      = dto.Title,
                    ContentId  = blogContentId,
                    Remark     = dto.Remark,
                    CategoryId = dto.CategoryId,
                    PicUrl     = dto.PicUrl,
                    ViewTimes  = 0,
                    LikeTimes  = 0,
                    State      = dto.State == "1" ? '1' : '2', //状态(枚举类型 1未发布 2已发布)
                    CreateAt   = DateTime.Now,
                    PublishAt  = dto.State == "2" ? DateTime.Now : (DateTime?)nullableDateTime.ConvertFromString(null),
                    DeleteAt   = null
                };

                List <tbl_blog_tag_relation> relationList = new List <tbl_blog_tag_relation>();
                foreach (var item in dto.TagIdList)
                {
                    relationList.Add(new tbl_blog_tag_relation
                    {
                        Id     = Guid.NewGuid().ToString(),
                        BlogId = blogId,
                        TagId  = item
                    });
                }

                await _context.tbl_blog.AddAsync(tbl_blog);

                await _context.tbl_blog_content.AddAsync(tbl_content);

                await _context.tbl_blog_tag_relation.AddRangeAsync(relationList);

                await _context.SaveChangesAsync();

                _context.Database.CommitTransaction();

                return(new CommonResultDto <string> {
                    Msg = "新增成功", Success = true
                });
            }
        }
Exemplo n.º 3
0
 public ActionResult Edit([Bind(Include = "blog_id,blog_datetime,blog_groupid,blog_createdby,blog_img,blog_title,blog_body")] tbl_blog tbl_blog)
 {
     if (ModelState.IsValid)
     {
         db.Entry(tbl_blog).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.blog_groupid   = new SelectList(db.tbl_group, "group_id", "group_id", tbl_blog.blog_groupid);
     ViewBag.blog_createdby = new SelectList(db.tbl_member, "mem_id", "mem_name", tbl_blog.blog_createdby);
     return(View(tbl_blog));
 }
Exemplo n.º 4
0
        /// <summary>
        /// 发布博客
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <CommonResultDto <string> > ReleaseBlog(string id)
        {
            tbl_blog blog = await _context.tbl_blog.FirstOrDefaultAsync(i => i.Id == id);

            blog.State = '2';
            _context.tbl_blog.Update(blog);
            await _context.SaveChangesAsync();

            return(new CommonResultDto <string> {
                Msg = "发布成功", Success = true
            });
        }
Exemplo n.º 5
0
        // GET: tbl_blog/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tbl_blog tbl_blog = db.tbl_blog.Find(id);

            if (tbl_blog == null)
            {
                return(HttpNotFound());
            }
            return(View(tbl_blog));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 点赞
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <CommonResultDto <string> > Like(string id)
        {
            tbl_blog tbl = await _context.tbl_blog.FirstOrDefaultAsync(i => i.Id == id);

            if (tbl != null)
            {
                ++tbl.LikeTimes;
            }
            _context.tbl_blog.Update(tbl);
            await _context.SaveChangesAsync();

            return(new CommonResultDto <string> {
                Msg = "点赞成功", Success = true
            });
        }
Exemplo n.º 7
0
        // GET: tbl_blog/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tbl_blog tbl_blog = db.tbl_blog.Find(id);

            if (tbl_blog == null)
            {
                return(HttpNotFound());
            }
            ViewBag.blog_groupid   = new SelectList(db.tbl_group, "group_id", "group_id", tbl_blog.blog_groupid);
            ViewBag.blog_createdby = new SelectList(db.tbl_member, "mem_id", "mem_name", tbl_blog.blog_createdby);
            return(View(tbl_blog));
        }
Exemplo n.º 8
0
        /// <summary>
        /// 更新博客
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <CommonResultDto <string> > Update(BlogUpdateDto dto)
        {
            using (await _context.Database.BeginTransactionAsync())
            {
                tbl_blog blog = await _context.tbl_blog.FirstOrDefaultAsync(i => i.Id == dto.Id);

                tbl_blog_content content = await _context.tbl_blog_content.FirstOrDefaultAsync(i => i.Id == blog.ContentId);

                List <tbl_blog_tag_relation> tagRelations = await _context.tbl_blog_tag_relation.Where(i => i.BlogId == dto.Id).ToListAsync();

                List <tbl_blog_tag_relation> insertTags = new List <tbl_blog_tag_relation>();
                foreach (var tagId in dto.TagIdList)
                {
                    insertTags.Add(new tbl_blog_tag_relation
                    {
                        Id     = Guid.NewGuid().ToString(),
                        BlogId = dto.Id,
                        TagId  = tagId
                    });
                }

                blog.Title      = dto.Title;
                blog.PicUrl     = dto.PicUrl;
                blog.CategoryId = dto.CategoryId;
                blog.UpdateAt   = DateTime.Now;
                _context.tbl_blog.Update(blog);

                content.Content = dto.Content;
                _context.tbl_blog_content.Update(content);

                _context.tbl_blog_tag_relation.RemoveRange(tagRelations);
                await _context.tbl_blog_tag_relation.AddRangeAsync(insertTags);

                await _context.SaveChangesAsync();

                _context.Database.CommitTransaction();

                return(new CommonResultDto <string> {
                    Msg = "更新成功", Success = true
                });
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 删除博客
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <CommonResultDto <string> > Delete(string id)
        {
            using (await _context.Database.BeginTransactionAsync())
            {
                tbl_blog tbl = await _context.tbl_blog.FirstOrDefaultAsync(i => i.Id == id);

                tbl.DeleteAt = DateTime.Now;
                _context.tbl_blog.Update(tbl);
                ////删除博客内容
                //tbl_blog_content tbl_content = await _context.tbl_blog_content.FirstOrDefaultAsync(i => i.Id == tbl.ContentId);
                //_context.tbl_blog_content.Remove(tbl_content);

                ////删除博客标签管理表数据
                //IEnumerable<tbl_blog_tag_relation> tbl_tag_relations = _context.tbl_blog_tag_relation.Where(i => i.BlogId == id);
                //_context.tbl_blog_tag_relation.RemoveRange(tbl_tag_relations);
                await _context.SaveChangesAsync();

                _context.Database.CommitTransaction();
                return(new CommonResultDto <string> {
                    Msg = "更新成功", Success = true
                });
            }
        }
Exemplo n.º 10
0
        public ActionResult Create(tbl_blog tbl_Blog, HttpPostedFileBase imagefile, int id)
        {
            string path = uploadingfile(imagefile);

            if (path.Equals("-1"))
            {
                ViewBag.error = "Image could not be uploaded....";
            }
            else
            {
                int      memid = Convert.ToInt32(Session["mem_id"]);
                tbl_blog blog  = new tbl_blog();
                blog.blog_datetime  = DateTime.Now;
                blog.blog_groupid   = id;
                blog.blog_createdby = memid;
                blog.blog_img       = path;
                blog.blog_title     = tbl_Blog.blog_title;
                blog.blog_body      = tbl_Blog.blog_body;
                db.tbl_blog.Add(blog);
                db.SaveChanges();
                return(RedirectToAction("Details", "Home", new { id = id }));
            }
            return(View());
        }
Exemplo n.º 11
0
        /// <summary>
        /// 查看博客详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <CommonResultDto <BlogViewDto> > BlogDetail(string id)
        {
            List <tbl_blog>        list         = _context.tbl_blog.Where(i => i.DeleteAt == null).ToList();
            List <BlogListViewDto> commentGroup = await(from a in _context.tbl_comment
                                                        group a by a.BlogId into g
                                                        select new BlogListViewDto
            {
                Id           = g.Key,
                CommentTimes = g.Count()
            }).ToListAsync();

            var qry = (from a in list
                       join b in _context.tbl_blog_content
                       on a.ContentId equals b.Id
                       join c in _context.tbl_category
                       on a.CategoryId equals c.Id
                       join d in commentGroup
                       on a.Id equals d.Id into temp
                       from g in temp.DefaultIfEmpty()
                       where a.Id == id
                       select new BlogViewDto
            {
                Title = a.Title,
                PicUrl = a.PicUrl,
                Content = b.Content,
                Remark = a.Remark,
                CategoryName = c.Name,
                CreateAt = a.CreateAt,
                ViewTimes = a.ViewTimes,
                LikeTimes = a.LikeTimes,
                CommentTimes = g == null ? 0 : g.CommentTimes            //_context.tbl_comment.Count(i => i.BlogId == a.Id)
            }).ToList();

            BlogViewDto             rlt    = qry.FirstOrDefault();
            IQueryable <TagViewDto> tagRlt = (from a in _context.tbl_blog_tag_relation
                                              join b in _context.tbl_tag
                                              on a.TagId equals b.Id
                                              select new TagViewDto {
                BlogId = a.BlogId, TagName = b.Name, TagId = b.Id, Sequence = b.Sequence
            });

            if (rlt != null)
            {
                List <string> tagNameList = tagRlt.Where(i => i.BlogId == id).Select(i => i.TagName).ToList();
                rlt.TagNameList = tagNameList;
                //if (tagNameList != null) { rlt.TagNameList.AddRange(tagNameList); };
            }

            //查看一次详情,浏览次数累加一次
            tbl_blog tbl = await _context.tbl_blog.FirstOrDefaultAsync(i => i.Id == id);

            if (tbl != null)
            {
                ++tbl.ViewTimes;
            }
            _context.tbl_blog.Update(tbl);
            await _context.SaveChangesAsync();

            return(new CommonResultDto <BlogViewDto> {
                Msg = "查询成功", Success = true, Response = rlt
            });
        }