public async Task <IActionResult> Edit(int id, int supplementId, string returnUrl)
        {
            bool isCommentExistingById = await this.commentService.IsCommentExistingById(id, false);

            if (!isCommentExistingById)
            {
                TempData.AddErrorMessage(string.Format(EntityNotFound, CommentEntity));

                return(this.RedirectToHomeIndex());
            }

            bool isSupplementExistingById = await this.supplementService.IsSupplementExistingById(supplementId, false);

            if (!isSupplementExistingById)
            {
                TempData.AddErrorMessage(string.Format(EntityNotFound, SupplementEntity));

                return(this.RedirectToHomeIndex());
            }

            string username = User.Identity.Name;

            bool isUserRestricted = await this.userService.IsUserRestricted(username);

            if (isUserRestricted)
            {
                TempData.AddErrorMessage(UserRestrictedErrorMessage);

                return(this.RedirectToHomeIndex());
            }

            string userId = this.userManager.GetUserId(User);

            bool isUserAuthor = await this.commentService.IsUserAuthor(id, userId);

            bool isUserModerator = User.IsInRole(ModeratorRole);

            if (!isUserAuthor && !isUserModerator)
            {
                return(this.RedirectToHomeIndex());
            }

            CommentBasicServiceModel comment = await this.commentService.GetEditModelAsync(id);

            CommentFormViewModel model = Mapper.Map <CommentFormViewModel>(comment);

            return(View(model));
        }
Пример #2
0
        public async Task GetEditModelAsync_WithCommentId_ShouldReturnValidViewModel()
        {
            // Arrange
            FitStoreDbContext database = this.Database;

            DatabaseHelper.SeedData(database);

            ICommentService commentService = new CommentService(database);

            // Act
            CommentBasicServiceModel result = await commentService.GetEditModelAsync(commentId);

            // Assert
            result.Id.Should().Be(commentId);
            result.Content.Should().Be(content);
            result.AuthorId.Should().Be(userId);
            result.SupplementId.Should().Be(supplementId);
        }