public async Task <IActionResult> Update(int id, [FromBody] AuthorForUpdateDto author)
        {
            try
            {
                if (id < 1 || author == null || id != author.Id)
                {
                    _logger.LogWarn("Invalid Id or Update Detail {id}[{author}]");
                    return(BadRequest());
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (!await _authorRepository.isExist(id))
                {
                    _logger.LogWarn($"Author is not found for Id: {id}");
                    return(NotFound($"Author is not found for Id: {id}"));
                }

                var authorToUpdate = _mapper.Map <Author>(author);
                var IsSuccess      = await _authorRepository.Update(authorToUpdate);

                if (!IsSuccess)
                {
                    return(InternalError($"Update Operation Failed"));
                }
                return(NoContent());
            } catch (Exception _ex)
            {
                return(InternalError($"{_ex.Message}\n{_ex.InnerException}"));
            }
        }
Exemple #2
0
        public IActionResult UpdateAuthor(Guid id, [FromBody] AuthorForUpdateDto authorForUpdateDto)
        {
            if (authorForUpdateDto == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var authorInDb = _repository.GetAuthor(id);

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

            var updatedAuthor = _mapper.Map <AuthorForUpdateDto, Author>(authorForUpdateDto, authorInDb);

            _repository.UpdateAuthor(updatedAuthor);

            return(NoContent());
        }
Exemple #3
0
        public ActionResult UpdateAuthor([FromRoute] string authorId, AuthorForUpdateDto author)
        {
            if (!_authorService.AuthorExists(authorId))
            {
                return(NotFound());
            }

            author.AuthorId = authorId;
            _authorService.UpdateAuthor(author);

            return(NoContent());
        }
        public IActionResult UpdateAuthor(Guid authorId, AuthorForUpdateDto author)
        {
            if (!service.AuthorExists(authorId))
            {
                return(BadRequest());
            }

            var authorEntity = mapper.Map <Entities.Author>(author);

            service.UpdateAuthor(authorEntity);
            service.Save();
            return(NoContent());
        }
Exemple #5
0
        public IActionResult UpdateAuthor(int authorId, AuthorForUpdateDto author)
        {
            var authorEntity = _authorRepository.getAuthor(authorId);

            if (authorEntity == null)
            {
                return(NotFound());
            }
            _mapper.Map(author, authorEntity);
            _authorRepository.UpdateAuthor();
            var authorDto = _mapper.Map <AuthorDto>(authorEntity);

            return(NoContent());
        }
Exemple #6
0
        public IActionResult UpdateAuthor(Guid authorId, AuthorForUpdateDto author)
        {
            if (!_restApiService.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var authorFromRepo = _restApiService.GetAuthor(authorId);

            _mapper.Map(author, authorFromRepo);
            _restApiService.UpdateAuthor(authorFromRepo);
            _restApiService.Save();

            return(NoContent());
        }
Exemple #7
0
        public void UpdateAuthor(AuthorForUpdateDto author)
        {
            string sql        = @"Update authors set au_fname = @au_fname, au_lname= @au_lname, phone = @phone, address= @address, city = @city, state = @state, zip = @zip where au_id= @au_id";
            var    parameters = new DynamicParameters();

            parameters.Add("@au_id", author.AuthorId, DbType.String, ParameterDirection.Input);
            parameters.Add("@au_fname", author.FirstName, DbType.String, ParameterDirection.Input);
            parameters.Add("@au_lname", author.LastName, DbType.String, ParameterDirection.Input);
            parameters.Add("@phone", author.Phone, DbType.String, ParameterDirection.Input);
            parameters.Add("@address", author.Address, DbType.String, ParameterDirection.Input);
            parameters.Add("@city", author.City, DbType.String, ParameterDirection.Input);
            parameters.Add("@state", author.State, DbType.String, ParameterDirection.Input);
            parameters.Add("@zip", author.Zip, DbType.String, ParameterDirection.Input);
            _repository.ModifyDatabase(sql, parameters);
        }
Exemple #8
0
        public void when_put_a_author_with_invalid_author_id_should_return_no_found_result()
        {
            // Arrange
            var authorId  = _invalidadAuthorId;
            var authorDto = new AuthorForUpdateDto {
                FirstName = "TestName", LastName = "TestLastName"
            };

            // Act
            var response = _authorsController.UpdateAuthor(authorId, authorDto);


            // Assert
            Assert.IsNotNull(response);
            Assert.IsInstanceOfType(response, typeof(NotFoundResult));
        }
        public async Task <IActionResult> UpdateAuthorAsync(Guid authorId, [FromBody] AuthorForUpdateDto updatedAuthor)
        {
            var author = await RepositoryWrapper.Author.GetByIdAsync(authorId);

            if (author == null)
            {
                return(NotFound());
            }
            //这个重载能将updatedAuthor map到已经存在的author实体
            Mapper.Map(updatedAuthor, author, typeof(BookForUpdateDto), typeof(Book));
            if (!await RepositoryWrapper.Author.SaveAsync())
            {
                throw new Exception("Update resource author failed!");
            }
            return(NoContent());
        }
        public async Task <IActionResult> UpdateAuthor(Guid authorId,
                                                       [FromBody] AuthorForUpdateDto authorForUpdateDto)
        {
            var authorFromRepo = await _authorRepository.GetAuthorAsync(authorId);

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

            _mapper.Map(authorForUpdateDto, authorFromRepo);
            _authorRepository.UpdateAuthor(authorFromRepo);
            await _authorRepository.SaveChangesAsync();

            return(NoContent());
        }
Exemple #11
0
        public ActionResult PatchAuthor([FromRoute] string authorId, JsonPatchDocument <AuthorForUpdateDto> patchDocument)
        {
            if (!_authorService.AuthorExists(authorId))
            {
                return(NotFound());
            }

            AuthorDto author         = _authorService.GetAuthor(authorId);
            var       booksForUpdate = new List <BookForUpdateDto>();

            foreach (var book in author.Books)
            {
                booksForUpdate.Add(new BookForUpdateDto
                {
                    AuthorId      = authorId,
                    BookId        = book.Id,
                    Genre         = book.Genre,
                    Price         = book.Price,
                    PublishedDate = book.PublishedDate,
                    Title         = book.Title
                });
            }

            AuthorForUpdateDto updateAuthor = new AuthorForUpdateDto
            {
                Address   = author.Address,
                FirstName = author.Name,
                LastName  = author.Name,
                AuthorId  = authorId,
                Books     = booksForUpdate,
                State     = author.State,
                City      = author.City,
                Phone     = author.Phone,
                Zip       = author.ZipCode
            };

            if (!TryValidateModel(updateAuthor))
            {
                return(ValidationProblem(ModelState));
            }

            patchDocument.ApplyTo(updateAuthor, ModelState);

            _authorService.UpdateAuthor(updateAuthor);

            return(NoContent());
        }
Exemple #12
0
        public ActionResult <AuthorDto> UpdateAuthor(Guid authorId, AuthorForUpdateDto authorForUpdate)
        {
            var author = _authorRepository.GetAuthor(authorId);

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

            _mapper.Map(authorForUpdate, author);

            _authorRepository.UpdateAuthor(author);

            _authorRepository.SaveChanges();

            return(Ok(_mapper.Map <AuthorDto>(author)));
        }
        public async Task <IActionResult> UpdateAuthor(Guid authorId, AuthorForUpdateDto authorForUpdateDto)
        {
            var authorFromRepo = await _courseLibraryRepository.GetAuthorAsync(authorId);

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

            //copiem valorile editate peste valorile entitatii
            _mapper.Map(authorForUpdateDto, authorFromRepo);

            _courseLibraryRepository.UpdateAuthor(authorFromRepo);
            await _courseLibraryRepository.SaveAsync();

            return(NoContent());
        }
Exemple #14
0
        public async Task <ActionResult <AuthorDto> > UpdateAuthor(
            Guid authorId,
            AuthorForUpdateDto authorForUpdate)
        {
            var authorFromRepo = await _authorsRepository.GetAuthorAsync(authorId);

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

            _mapper.Map(authorForUpdate, authorFromRepo);

            //// update & save
            _authorsRepository.UpdateAuthor(authorFromRepo);
            await _authorsRepository.SaveChangesAsync();

            // return the author
            return(Ok(_mapper.Map <AuthorDto>(authorFromRepo)));
        }
        public IActionResult UpdateAuthor(Guid authorId, AuthorForUpdateDto course)
        {
            if (!_bookstoreRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var authorFromRepoforUpdate = _bookstoreRepository.GetAuthor(authorId);

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

            authorFromRepoforUpdate.LastName = course.LastName;
            // _mapper.Map(course, authorFromRepoforUpdate);

            _bookstoreRepository.UpdateAuthor(authorFromRepoforUpdate);

            _bookstoreRepository.Save();

            return(NoContent());
        }