Beispiel #1
0
        public void property_update_succeeds()
        {
            var sut = new AuthorUpdateDto()
            {
                Id          = 7,
                FirstName   = "Unit",
                LastName    = "Test",
                PhoneNumber = "303-333-4455",
                Address     = "12345 Unit Test",
                City        = "Denver",
                State       = "CO",
                ZipCode     = "80014"
            };

            using (new AssertionScope())
            {
                sut.FirstName.Should().Be("Unit");
                sut.LastName.Should().Be("Test");
                sut.Id.Should().Be(7);
                sut.PhoneNumber.Should().Be("303-333-4455");
                sut.Address.Should().Be("12345 Unit Test");
                sut.City.Should().Be("Denver");
                sut.State.Should().Be("CO");
                sut.ZipCode.Should().Be("80014");
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Put(Guid id, [FromBody] AuthorUpdateDto author)
        {
            if (id == Guid.Empty)
            {
                return(BadRequest(GetMessageObject(InvalidIdentifier)));
            }

            if (!ModelState.IsValid)
            {
                LogModelStateErrors(_logger);
                return(ValidationProblem(ModelState));
            }

            try
            {
                await _authorService.UpdateAuthor(id, author);

                return(NoContent());
            }
            catch (AuthorNotFoundException exception)
            {
                _logger.LogError($"Error occurred: {exception.GetMessageWithStackTrace()}");
                return(NotFound($"Author with id: {id} not found."));
            }
            catch (Exception exception)
            {
                _logger.LogError($"Error occurred: {exception.GetMessageWithStackTrace()}");
                return(InternalServerErrorResult($"Error occurred updating author with id: {id}"));
            }
        }
Beispiel #3
0
        public async Task <ActionResult <Author> > UpdateAuthor(int authorId, [FromBody] AuthorUpdateDto authorUpdate)
        {
            if (authorUpdate == null)
            {
                return(BadRequest());
            }

            if (authorId != authorUpdate.Id)
            {
                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(authorUpdate, existingAuthor);

            await _authorRepository.UpdateAsync(existingAuthor);

            _logger.LogInformation($"The author id:: {authorId} has been updated.");

            return(Ok(existingAuthor));
        }
Beispiel #4
0
        public async Task update_author_succeeds()
        {
            var author = new Author()
            {
                Id = 99, AuthorCode = "111-22-3333", FirstName = "A", LastName = "B", PhoneNumber = "1", Contract = false
            };
            var authorUpdated = new Author()
            {
                Id = 99, AuthorCode = "111-22-3333", FirstName = "Unit", LastName = "Test", PhoneNumber = "1", Contract = false
            };
            var authorDto = new AuthorUpdateDto()
            {
                Id = 99, FirstName = "Unit", LastName = "Test", PhoneNumber = "1"
            };

            _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.UpdateAuthor(99, authorDto);

            using (new AssertionScope())
            {
                result.GetObjectResult().Should().BeOfType <Author>();
                result.GetObjectResult().FirstName.Should().Be(authorUpdated.FirstName);
                result.GetObjectResult().LastName.Should().Be(authorUpdated.LastName);
                result.GetObjectResult().Id.Should().Be(99);
            }
        }
        public IActionResult UpdateAuthor(int authorId, [FromBody] AuthorUpdateDto updatedAuthor)
        {
            if (updatedAuthor == null)
            {
                return(BadRequest(ModelState));
            }

            if (authorId != updatedAuthor.Id)
            {
                return(BadRequest(ModelState));
            }

            if (!_unitOfWork.AuthorRepository.AuthorExists(authorId))
            {
                ModelState.AddModelError("", "Author doesn't exist!");
            }

            if (!ModelState.IsValid)
            {
                return(StatusCode(404, ModelState));
            }

            if (!_unitOfWork.AuthorRepository.UpdateAuthor(updatedAuthor))
            {
                ModelState.AddModelError("", $"Something went wrong updating the author " + $"{updatedAuthor.AuthorFirstName} {updatedAuthor.AuthorLastName}");
                return(StatusCode(500, ModelState));
            }

            _unitOfWork.Commit();

            return(NoContent());
        }
Beispiel #6
0
        public async Task <IActionResult> PutAuthor(int id, AuthorUpdateDto authorDto)
        {
            var author = await _context.Authors.FindAsync(id);

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

            _context.Entry(author).State = EntityState.Modified;

            _mapper.Map(authorDto, author);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuthorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #7
0
        public bool UpdateAuthor(AuthorUpdateDto authorToUpdateDto)
        {
            var authorToUpdate = MapConfig.Mapper.Map <Author>(authorToUpdateDto);

            _authorContext.Update(authorToUpdate);
            return(Save());
        }
Beispiel #8
0
        public async Task <AuthorOutputDto> Put([FromBody] AuthorUpdateDto input)
        {
            var book   = _mapper.Map <Author>(input);
            var result = await _authorManager.UpdateAsync(book);

            return(_mapper.Map <AuthorOutputDto>(result));
        }
Beispiel #9
0
        public async Task <ActionResult> Update(int id, [FromBody] AuthorUpdateDto authorUpdateDto)
        {
            Author author = await _authorRepository.FindByIdAsync(id);

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

            _mapper.Map(authorUpdateDto, author);
            await _authorRepository.UpdateAsync(author);

            return(NoContent());
        }
 public ActionResult <Author> Update(AuthorUpdateDto AuthorUpdateDto)
 {
     try
     {
         return(service.Update(AuthorUpdateDto));
     }
     catch (ArgumentException error)
     {
         return(NotFound(error.Message));
     }
     catch (Exception error)
     {
         return(Conflict(error.Message));
     }
 }
Beispiel #11
0
        public async Task UpdateAuthor(Guid id, AuthorUpdateDto authorUpdateDto)
        {
            ValidateId(id);
            ValidateEntity(authorUpdateDto);

            var author = await _authorRepository.GetByIdAsync(id);

            if (author == null)
            {
                throw new AuthorNotFoundException(id);
            }

            _mapper.Map(authorUpdateDto, author);
            await _authorRepository.SaveAsync();
        }
Beispiel #12
0
        public async Task <IActionResult> PutAuthor(int id, AuthorUpdateDto authorDto)
        {
            if (id < 1 || id != authorDto.Id || authorDto == null)
            {
                return(BadRequest("Invalid author details provided."));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new { Author = ModelState, Message = "Incomplete author details." }));
            }

            await _authorService.Update(_mapper.Map <Author>(authorDto));

            return(NoContent());
        }
Beispiel #13
0
        public ActionResult Put(Guid id, AuthorUpdateDto authorDto)
        {
            var author = _bookLibrary.Authors.FirstOrDefault(p => p.Id == id);

            if (author is null)
            {
                author = new Author {
                    FirstName = authorDto.FirstName, LastName = authorDto.LastName, Id = id
                };
                _bookLibrary.Authors.Add(author);
                _bookLibrary.SaveChanges();
                return(CreatedAtAction("Get", new { id = id }, author));
            }
            author.FirstName = authorDto.FirstName;
            author.LastName  = authorDto.LastName;
            _bookLibrary.SaveChanges();
            return(NoContent());
        }
        public Author Update(AuthorUpdateDto dto)
        {
            var isExist = GetDetail(dto.FullName);

            if (isExist != null && dto.Id != isExist.Id)
            {
                throw new Exception(dto.FullName + " existed");
            }

            var entity = new Author
            {
                Id        = dto.Id,
                FullName  = FormatString.Trim_MultiSpaces_Title(dto.FullName, true),
                Biography = dto.Biography,
                Image     = dto.Image
            };

            return(repository.Update(entity));
        }
        public async Task <IActionResult> UpdateAuthor(int authorId, AuthorUpdateDto authorUpdateDto)
        {
            var author = await _unitOfWork.Author.GetById(authorId);

            if (author != null)
            {
                var result = _mapper.Map(authorUpdateDto, author);
                await _unitOfWork.Author.Update(author);

                await _unitOfWork.Save();

                _logger.LogInformation("PUT api/author/{authorId} => OK", authorId);
                return(Ok());
            }
            else
            {
                _logger.LogInformation("PUT api/author/{authorId} => NOT OK", authorId);
                return(NoContent());
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDto authorDto)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Update attempted on record with id: {id}.");
                if (id < 1 || authorDto == null || id != authorDto.Id)
                {
                    _logger.LogWarn($"{location}: Update failed with bad data - id: {id}.");
                    return(BadRequest());
                }
                var isExists = await _authorRepository.isExists(id);

                if (!isExists)
                {
                    _logger.LogWarn($"{location}: Failed to retrieve record with id: {id}.");
                    return(NotFound());
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{location}: Data was incomplete.");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDto);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"{location}: Update failed for record with id: {id}."));
                }
                _logger.LogInfo($"{location}: Record with id: {id} successfully updated.");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Beispiel #17
0
        public async Task <ActionResult> UpdatePartial(int id, [FromBody] JsonPatchDocument <AuthorUpdateDto> authorUpdateDtoPatchDoc)
        {
            Author authorFromDb = await _authorRepository.FindByIdAsync(id);

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

            AuthorUpdateDto authorUpdateDto = _mapper.Map <AuthorUpdateDto>(authorFromDb);

            authorUpdateDtoPatchDoc.ApplyTo(authorUpdateDto);
            if (!TryValidateModel(authorUpdateDto))
            {
                return(ValidationProblem(ModelState));
            }

            _mapper.Map(authorUpdateDto, authorFromDb);
            await _authorRepository.UpdateAsync(authorFromDb);

            return(NoContent());
        }