Example #1
0
        public async Task patch_author_code_author_succeeds()
        {
            var author = new Author()
            {
                Id = 737, AuthorCode = "111-22-3333", FirstName = "A", LastName = "B", PhoneNumber = "1", Contract = false
            };
            var authorUpdated = new Author()
            {
                Id = 737, AuthorCode = "444-55-6666", FirstName = "Unit", LastName = "Test", PhoneNumber = "1", Contract = false
            };
            var authorDto = new AuthorUpdateAuthorCodeDto()
            {
                AuthorId = 737, AuthorCode = "444-55-6666"
            };

            _mockAuthorRepository.Setup(s => s.GetAuthorAsync(It.IsAny <int>())).ReturnsAsync(author);
            _mockAuthorRepository.Setup(s => s.UpdateAsync(It.IsAny <Author>()));

            var sut = CreateAuthorController(_mockAuthorRepository, _mapper, _mockLogger);

            var result = await sut.PatchUpdateAuthorCode(737, authorDto);

            using (new AssertionScope())
            {
                result.GetObjectResult().Should().BeOfType <Author>();
                result.GetObjectResult().AuthorCode.Should().Be(authorUpdated.AuthorCode);
                result.GetObjectResult().Id.Should().Be(737);
            }
        }
Example #2
0
        public void property_update_succeeds()
        {
            var sut = new AuthorUpdateAuthorCodeDto()
            {
                AuthorId   = 7,
                AuthorCode = "UnitTest"
            };

            using (new AssertionScope())
            {
                sut.AuthorId.Should().Be(7);
                sut.AuthorCode.Should().Be("UnitTest");
            }
        }
Example #3
0
        public async Task <ActionResult <Author> > PatchUpdateAuthorCode(int authorId, [FromBody] AuthorUpdateAuthorCodeDto authorUpdateDto)
        {
            if (authorUpdateDto == null)
            {
                return(BadRequest());
            }

            if (authorId != authorUpdateDto.AuthorId)
            {
                return(BadRequest("The AuthorId parameter does not match the AuthorId in the request body."));
            }

            var existingAuthor = await _authorRepository.GetAuthorAsync(authorId);

            if (existingAuthor == null)
            {
                return(NotFound());
            }

            _mapper.Map(authorUpdateDto, existingAuthor);

            await _authorRepository.UpdateAsync(existingAuthor);

            _logger.LogInformation($"The author id:: {authorId} has been updated. The author code is now :: {authorUpdateDto.AuthorCode}");

            return(Ok(existingAuthor));
        }