public async Task <IActionResult> DeleteComment(int commentId)
        {
            var commentContent = await _unitOfWork.Comment.GetCommentById(commentId);

            var commentDeleteViewModel = new CommentDeleteViewModel()
            {
                CommentId      = commentContent.CommentId,
                CommentContent = commentContent.CommentContent
            };

            return(View(commentDeleteViewModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> DeleteCommentAsync(CommentDeleteViewModel commentDeleteData)
        {
            if (!this.ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var commentToDelete = await this.commentRepository.GetByIdAsync(commentDeleteData.CommentId);

            if (commentToDelete == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var isAdmin           = this.User.IsInRole("Admin");
            var currentLoggedUser = await this.applicationUserRepository.GetByExpressionAsync(u => u.Id == commentToDelete.UserId);

            var isOwner = currentLoggedUser.Id == commentToDelete.UserId;

            if (!isAdmin && !isOwner)
            {
                return(RedirectToAction("Index", "Home"));
            }

            await this.commentUpdateService.DeleteAsync(commentToDelete);

            var comments = await this.bookRepository.GetOneToManyAsync(b => b.BookId == commentToDelete.BookId,
                                                                       bg => bg.Comments);

            var commentsViewModel = this.objectMapper.Map <IEnumerable <CommentViewModel> >(comments);

            foreach (var comment in commentsViewModel)
            {
                var commentCreatorId = comment.UserId;
                var currentUser      = await this.applicationUserRepository.GetByExpressionAsync(u => u.UserName == this.User.Identity.Name);

                var isCreator = commentCreatorId == currentUser.Id;

                comment.CanEdit = isAdmin || isCreator;

                var comemntCreator = await this.applicationUserRepository.GetByIdAsync(comment.UserId);

                comment.Author       = comemntCreator.UserName;
                comment.AuthorPicUrl = comemntCreator.ProfilePictureUrl;
            }

            return(PartialView("Book/_BookCommentsPartial", new KeyValuePair <string, IEnumerable <CommentViewModel> >(commentToDelete.BookId, commentsViewModel)));
        }