Esempio n. 1
0
        public IActionResult Show(long id, int?page)  //Show post, id is post id, and page is number of showing comments page
        {
            if (page == null)
            {
                page = 1;
            }
            var post = UnitOfWork_.BlogPostsRepository.Get(id);

            if (post != null)
            {
                var model = new Models.BlogPostViewModel()
                {
                    AddedComment = new Models.CommentViewModel(), //Comment user might add
                    IsAdmin      = User.IsInRole("Admin"),
                    CurrentUser  = UserManager_.GetUserId(User)
                };
                Mapper_.Map(post, model);
                model.PageCount = UnitOfWork_.CommentsRepository.GetCommentsPageCount(id, ON_PAGE);
                if (page > model.PageCount)
                {
                    page = model.PageCount;
                }
                model.PageNum = (int)page;

                var comments = UnitOfWork_.CommentsRepository.GetCommentsPage(post.Id, model.PageNum, ON_PAGE); //Comments on this page
                if (comments != null)
                {
                    Mapper_.Map(comments, model.DisplayedComments);
                } //Adding them to list of displaying comments
                return(View(model));
            }
            return(View("Error"));
        }
Esempio n. 2
0
 public IActionResult Create(Models.BlogPostViewModel model) //Checking if added comment is valid and saving it to database
 {
     if (ModelState.IsValid)
     {
         var entity = new DAL.Models.Comment
         {
             Text        = model.AddedComment.Text,
             AuthorId    = UserManager_.GetUserId(User),
             PostId      = model.Id,
             PostingTime = DateTime.Now
         };
         UnitOfWork_.CommentsRepository.Add(entity);
         UnitOfWork_.Commit();
         return(RedirectToAction("Show", "Post", new { id = model.Id }));
     }
     return(View("Show", model)); //If not - retry
 }