예제 #1
0
        public async Task EditCommentAsyncShouldEditCommentSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var commentsService = new CommentsService(dbContext, mapper, usersService);

            var user = new ApplicationUser
            {
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var comment = new Comment
            {
                Id       = CommentId,
                Text     = CommentText,
                AuthorId = user.Id,
            };

            await dbContext.Comments.AddAsync(comment);

            await dbContext.SaveChangesAsync();

            var commentModel = new CommentAdminEditViewModel
            {
                Id       = CommentId,
                Text     = CommentText,
                AuthorId = user.Id,
                Rating   = CommentRating,
            };

            await commentsService.EditCommentAsync(commentModel);

            Assert.Equal(comment.Rating, commentModel.Rating);
        }
예제 #2
0
        public async Task <IActionResult> Edit(CommentAdminEditViewModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            await this.commentsService.EditCommentAsync(inputModel);

            return(this.RedirectToAction("Comments", "Users", new { id = inputModel.AuthorId }));
        }
예제 #3
0
        public async Task EditCommentAsync(CommentAdminEditViewModel inputModel)
        {
            if (inputModel.PostId != null && !this.dbContext.Post.Any(p => p.Id == inputModel.PostId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidPostId, inputModel.PostId));
            }

            if (inputModel.ProductId != null && !this.dbContext.Products.Any(p => p.Id == inputModel.ProductId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProductId, inputModel.ProductId));
            }

            if (inputModel.ProgramId != null && !this.dbContext.Programs.Any(p => p.Id == inputModel.ProgramId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProgramId, inputModel.ProgramId));
            }

            var comment = await this.dbContext.Comments
                          .FirstOrDefaultAsync(c => c.Id == inputModel.Id);

            if (comment == null)
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidComment));
            }

            comment.IsDeleted = inputModel.IsDeleted;

            comment.DeletedOn = inputModel.DeletedOn;

            comment.ModifiedOn = inputModel.ModifiedOn;

            comment.CreatedOn = inputModel.CreatedOn;

            comment.Text = inputModel.Text;

            comment.AuthorId = inputModel.AuthorId;

            comment.Rating = inputModel.Rating;

            comment.PostId = inputModel.PostId;

            comment.ProductId = inputModel.ProductId;

            comment.ProgramId = inputModel.ProgramId;

            this.dbContext.Update(comment);

            await this.dbContext.SaveChangesAsync();
        }
예제 #4
0
        public async Task EditCommentAsyncShouldThrowExceptionIfInvalidProgramId()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var commentsService = new CommentsService(dbContext, mapper, usersService);

            var user = new ApplicationUser
            {
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var commentModel = new CommentAdminEditViewModel
            {
                Text      = CommentText,
                AuthorId  = user.Id,
                ProgramId = ProgramId,
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await commentsService.EditCommentAsync(commentModel));

            Assert.IsType <ArgumentNullException>(exception);
        }