public void EditPost_WithCorrectData_ShouldEditPostSuccessfully()
        {
            string errorMessagePrefix = "BlogService EditPost() method does not work properly.";

            var blogRepo = new Mock <IRepository <Blog> >();
            var fileMock = new Mock <IFormFile>();

            blogRepo.Setup(x => x.All()).Returns(this.GetTestData().AsQueryable);

            this._blogService = new BlogService(blogRepo.Object, null, null, null,
                                                null, null, null);

            var expected = new EditBlogPostDTO
            {
                Id      = "1",
                Title   = "George",
                Content = "qwerty qwerty qwerty",
            };

            var actual = blogRepo.Object.All().Single(x => x.Id == 1);

            var passed = this._blogService.EditPost(expected, true).IsCompletedSuccessfully;

            Assert.True(passed, errorMessagePrefix);

            Assert.True(expected.Content == actual.Content, errorMessagePrefix + " " + "Content is not returned properly.");
            Assert.True(expected.Title == actual.Title, errorMessagePrefix + " " + "Title is not returned properly.");
        }
Beispiel #2
0
        public async Task EditPost(EditBlogPostDTO postDto, bool skipMethodForTest = false)
        {
            try
            {
                var postObj = this._blogRepository.All().Single(x => x.Id == int.Parse(postDto.Id));

                postObj.Content = postDto.Content;
                postObj.Title   = postDto.Title;

                await this._blogRepository.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Invalid post id.", e.InnerException);
            }
        }
        public async Task <ActionResult <EditBlogPostDTO> > EditPost([FromForm] EditBlogPostDTO postDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(this.NoContent());
                }

                _logger.LogInfo($"Editing a post with id {postDto.Id}...");

                await this._blogService.EditPost(postDto);

                _logger.LogInfo($"Post with id {postDto.Id} successfully edited.");

                return(this.Ok());
            }
            catch (Exception e)
            {
                return(this.BadRequest(e.Message));
            }
        }