public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Blog blog = db.Blogs.Find(id); if (blog == null) { return(HttpNotFound()); } ViewBlogViewModel blogViewModel = new ViewBlogViewModel { BlogID = blog.BlogID, UserName = blog.User.UserName, PostDate = blog.PostDate, Title = blog.Title, Content = blog.Content }; blogViewModel = checkFavorites(blogViewModel); return(View(blogViewModel)); }
public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Blog blog = db.Blogs.Find(id); var userID = User.Identity.GetUserId(); if (blog == null) { return(HttpNotFound()); } else if (blog.UserID != userID) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ViewBlogViewModel blogViewModel = new ViewBlogViewModel { BlogID = blog.BlogID, UserName = blog.User.UserName, PostDate = blog.PostDate, Title = blog.Title, Content = blog.Content }; return(View(blogViewModel)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Blog blog = db.Blogs.Find(id); var userID = User.Identity.GetUserId(); if (blog == null) { return(HttpNotFound()); } else if (blog.UserID != userID) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ViewBlogViewModel blogViewModel = new ViewBlogViewModel { BlogID = blog.BlogID, UserName = blog.User.UserName, PostDate = blog.PostDate, Title = blog.Title, Content = blog.Content, Tags = blog.Tags }; IEnumerable <Tag> tags = db.Tags.OrderBy(b => b.Name).ToList(); ViewBag.BlogTags = tags; return(View(blogViewModel)); }
public ActionResult ViewBlog(int?id) { if (!id.HasValue) { return(Redirect("/")); //if no id was sent in, redirect to home page } BlogDb db = new BlogDb(Properties.Settings.Default.ConStr); ViewBlogViewModel vm = new ViewBlogViewModel(); vm.Post = db.GetPost(id.Value); vm.Comments = db.GetComments(id.Value); return(View(vm)); }
// // GET: /Blog/Details/5 public ActionResult Details(int id) { Blogs blog = UnitOfWork_.BlogRepo.GetBlogById(id); ViewBlogViewModel viewModel = null; if (blog != null) { List <Comments> comments = UnitOfWork_.CommentsRepo.GetCommentsByBlogId(id); string categoryName = UnitOfWork_.CategoryRepo.GetCategoryById(blog.CategoryId).CategoryName; viewModel = new ViewBlogViewModel(blog, comments, categoryName); } return(View(viewModel)); }
public async Task <IActionResult> ViewBlog(int BlogId) { // Get blog from DB var blog = _blogRepository.GetBlog(BlogId); if (blog == null) { ViewBag.ErrorTitle = "Blog cannot be found!"; ViewBag.ErrorMessage = "No blog with such ID was found."; return(View("Error")); } else { var author = await userManager.FindByIdAsync(blog.UserId); // Unapprove blogs if (blog.Approved == false) { if (User.IsInRole("Admin") || author.UserName.Equals(User.Identity.Name)) { // Author or admin view unapproved blog ViewBag.Alert = "Blog is not approved, therefore not published to public"; ViewBag.AlertClass = "alert-warning"; } else { // Not admin and not author ViewBag.ErrorTitle = "Unapproved blog!"; ViewBag.ErrorMessage = "The blog still awaits approval from moderators or admins."; return(View("Error")); } } // Populate ViewModel with blog data ViewBlogViewModel model = new ViewBlogViewModel { Title = blog.Title, BriefDescription = blog.BriefDescription, Topic = blog.Topic, Date = blog.Date, BlogContent = blog.BlogContent.Split("\n"), Username = author.UserName }; return(View(model)); } }
// Accept list of blogs, set isFavorite flag for each that user has favorited // Return list of blogs with flags set public ViewBlogViewModel checkFavorites(ViewBlogViewModel blog) { // If user is logged in, check if blog is favorited string userID = User.Identity.GetUserId(); if (!String.IsNullOrEmpty(userID)) { FavoriteBlog checkFBlog = db.FavoriteBlogs.SingleOrDefault(b => b.BlogID == blog.BlogID && b.UserID == userID); if (checkFBlog != null) { blog.isFavorited = true; } } return(blog); }
public ActionResult ViewBlog(int id) { if (id == 0) { return(Redirect("/")); //if no id was sent in, redirect to home page } BlogDb db = new BlogDb(_connectionString); ViewBlogViewModel vm = new ViewBlogViewModel(); vm.Post = db.GetPost(id); vm.Comments = db.GetComments(id); if (Request.Cookies["commenter-name"] != null) { vm.CommenterName = Request.Cookies["commenter-name"]; } return(View(vm)); }