public IActionResult Details(int id)
        {
            ViewData["BodyClass"] = "blog-page";
            var model = new DetailBlogViewModel();

            model.Blog           = _blogService.GetById(id);
            model.BlogCategory   = _blogCategoryService.GetById(model.Blog.CategoryId);
            model.GetReatedBlogs = _blogService.GetReatedBlogs(id, 9);
            model.Tags           = _blogService.GetListTagById(id);

            return(View(model));
        }
예제 #2
0
        public ActionResult BlogDetail(int id)
        {
            ApplicationDbContext _context = new ApplicationDbContext();

            DetailBlogViewModel model = new DetailBlogViewModel();

            var blog = _context.Blogs.Include(t => t.BlogComments).Where(s => s.Id == id).SingleOrDefault();

            model.Id           = blog.Id;
            model.blogImage    = blog.blogImage;
            model.Title        = blog.Title;
            model.postedBy     = blog.postedBy;
            model.postedDate   = blog.postedDate;
            model.blogMessage  = blog.blogMessage;
            model.RecentBlogs  = _context.Blogs.Take(5).OrderByDescending(x => x.Id).ToList();
            model.BlogComments = blog.BlogComments;
            return(View(model));
        }
예제 #3
0
        public ActionResult DetailBlog(string slug)
        {
            DetailBlogViewModel viewModel = new DetailBlogViewModel();
            BlogEntities        db        = new BlogEntities();

            POST content = new POST();

            content = db.POSTs.FirstOrDefault(c => c.SLUG.Equals(slug) && c.IS_ACTIVE.Equals("1"));

            //CAN'T FIND POST, REDIRECT TO 404 PAGE
            if (content == null)
            {
                return(RedirectToAction("NotFound"));
            }

            viewModel.Post = content;

            //GET POST'S TAG
            viewModel.Tags = db.TAGs.SqlQuery(string.Format("SELECT TB2.* FROM POST TB1, TAG TB2, POST_TAG TB3 WHERE TB1.ID=TB3.POST_ID AND TB3.TAG_ID=TB2.ID AND TB1.ID={0}", content.ID)).ToList <TAG>();

            //GET POST'S AUTHOR
            viewModel.Author = db.PAGE_USER.FirstOrDefault(c => c.USERNAME.Equals(content.CREATED_USER));

            //GET PREV AND NEXT POSTs
            try
            {
                string   sql      = string.Format(@"SELECT CONVERT(nvarchar,ISNULL( K.PREV,0))+'#'+CONVERT(nvarchar,ISNULL( K.NEXT,0)) 
                                         FROM(
	                                          SELECT LAG(P.ID) OVER(ORDER BY P.ID) PREV,
			                                         P.ID,
			                                         LEAD(P.ID) OVER(ORDER BY P.ID) NEXT
	                                          FROM	POST P
	                                          WHERE	P.ID<=({0}*2) AND IS_ACTIVE='1' ) K
                                         WHERE ID={1}", content.ID <= 1 ? 2 : content.ID, content.ID);
                string[] nearById = db.Database.SqlQuery <string>(sql).ToList().FirstOrDefault().Split('#');
                //GET PREV_ID#NEXT_ID
                int prevId = Convert.ToInt32(nearById[0]);
                int nextId = Convert.ToInt32(nearById[1]);

                viewModel.PrevPost = db.POSTs.Where(c => c.ID.Equals(prevId)).Select(c => new PostViewModel {
                    TITLE = c.TITLE, IMAGE_COVER = c.IMAGE_COVER, SLUG = c.SLUG
                }).FirstOrDefault();
                viewModel.NextPost = db.POSTs.Where(c => c.ID.Equals(nextId)).Select(c => new PostViewModel {
                    TITLE = c.TITLE, IMAGE_COVER = c.IMAGE_COVER, SLUG = c.SLUG
                }).FirstOrDefault();

                if (viewModel.PrevPost == null)
                {
                    viewModel.PrevPost = new PostViewModel()
                    {
                        SLUG = "/", TITLE = "Trang chủ"
                    };
                }
                if (viewModel.NextPost == null)
                {
                    viewModel.NextPost = new PostViewModel()
                    {
                        SLUG = "/", TITLE = "Trang chủ"
                    };
                }
            }
            catch (Exception ex)
            {
                viewModel.PrevPost = new PostViewModel()
                {
                    SLUG = "/", TITLE = "Trang chủ"
                };
                viewModel.NextPost = new PostViewModel()
                {
                    SLUG = "/", TITLE = "Trang chủ"
                };
            }

            //GET 3 RELATED POST
            if (viewModel.Tags != null)
            {
                viewModel.RelatedPosts = new List <PostViewModel>();
                foreach (TAG tag in viewModel.Tags)
                {
                    PostViewModel pV = db.POSTs.SqlQuery(string.Format("SELECT TB1.* FROM POST TB1, TAG TB2, POST_TAG TB3 WHERE TB1.ID=TB3.POST_ID AND TB3.TAG_ID=TB2.ID AND TB2.ID={0} AND TB1.ID <> {1}", tag.ID, content.ID)).Select(c => new PostViewModel {
                        TITLE = c.TITLE, IMAGE_COVER = c.IMAGE_COVER, SLUG = c.SLUG, CREATED_DATE = c.CREATED_DATE, CREATED_USER = c.CREATED_USER
                    }).FirstOrDefault();
                    if (pV != null)
                    {
                        pV.MAIN_TAG = tag.NAME;
                        viewModel.RelatedPosts.Add(pV);
                        if (viewModel.RelatedPosts.Count >= 3)
                        {
                            break;
                        }
                    }
                }
            }


            //SETTING VIEWBAG
            ViewBag.Title = content.TITLE;
            return(View(viewModel));
        }