Пример #1
0
 public IActionResult Delete(long id, int page, long postId)
 {
     if (!UnitOfWork_.CommentsRepository.Remove(id))
     {
         return(View("Error")); //There's no such comment in database
     }
     UnitOfWork_.Commit();
     return(RedirectToAction("Show", "Post", new { id = postId, page }));//Open same post, same page
 }
Пример #2
0
 public IActionResult Edit(Models.BlogPostCreationViewModel model) //Checking is edited post is valid and saving it to database
 {
     if (ModelState.IsValid)
     {
         var entity = UnitOfWork_.BlogPostsRepository.Get((long)model.Id);
         if (entity == null)
         {
             return(View("Error"));
         }
         Mapper_.Map(model, entity);
         UnitOfWork_.BlogPostsRepository.Update(entity);
         UnitOfWork_.Commit();
         return(RedirectToAction("Show", "Post", new { id = model.Id })); //Opening edited post
     }
     return(View("Create", model));
 }
Пример #3
0
        public IActionResult DeletePost(long id)
        {
            var commentsId = _unitOfWork.CommentRepository.GetCommentsByPostId(id);

            if (commentsId != null && commentsId.Count > 0)
            {
                foreach (var commentId in commentsId)
                {
                    _unitOfWork.CommentRepository.Remove(commentId);
                }
            }
            if (!_unitOfWork.PostRepository.Remove(id))
            {
                return(View("Error"));
            }
            _unitOfWork.Commit();
            return(RedirectToAction("Index", "Home"));
        }
Пример #4
0
        public IActionResult Create(PostCreationViewModel model) //Checking is new post is valid and saving it to database
        {
            if (model != null)
            {
                model.AuthorId = _userManager.GetUserId(User); //Setting current user as author

                if (ModelState.IsValid)
                {
                    var entity = new DAL.Models.Post();
                    _mapper.Map(model, entity);
                    entity.Created = DateTime.Now;
                    entity.Image   = model.Image;
                    _unitOfWork.PostRepository.Add(entity);
                    int x = _unitOfWork.Commit();
                    return(RedirectToAction("Index", "Home"));
                }
            }
            var errors = ModelState.Select(x => x.Value.Errors)
                         .Where(y => y.Count > 0)
                         .ToList();

            return(View(model));
        }