コード例 #1
0
        public async Task <ActionResult <AuthorDto> > UpdateAuthor(int id, [FromBody] SaveAuthorDto updateAuthorDto)
        {
            if (id == 0)
            {
                return(BadRequest());
            }

            if (updateAuthorDto == null)
            {
                return(BadRequest());
            }

            var authorToUpdate = await _authorService.GetAuthorById(id);

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

            var newAuthor = _mapper.Map <SaveAuthorDto, Author>(updateAuthorDto);

            await _authorService.UpdateAuthor(authorToUpdate, newAuthor);

            var updatedAuthor = await _authorService.GetAuthorById(id);

            var updatedAuthorDto = _mapper.Map <Author, AuthorDto>(updatedAuthor);

            return(Ok(updatedAuthorDto));
        }
コード例 #2
0
        public async Task <ActionResult <AuthorDto> > CreateAuthor([FromBody] SaveAuthorDto createAuthorDto)
        {
            if (createAuthorDto == null)
            {
                return(BadRequest());
            }

            var authorToCreate = _mapper.Map <SaveAuthorDto, Author>(createAuthorDto);

            var newAuthor = await _authorService.CreateAuthor(authorToCreate);

            var author = await _authorService.GetAuthorById(newAuthor.Id);

            var authorCreated = _mapper.Map <Author, AuthorDto>(author);

            return(Ok(authorCreated));
        }