Пример #1
0
        public async Task <ActionResult <AuthorDTO> > CreateAuthor([FromBody] SaveAuthorDTO saveAuthorResource)
        {
            try
            {
                var validator        = new SaveAuthorResourceValidator();
                var validationResult = await validator.ValidateAsync(saveAuthorResource);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(validationResult.Errors));
                }

                var authorToCreate = _mapper.Map <SaveAuthorDTO, Author>(saveAuthorResource);

                var newAuthor = await _authorService.Create(authorToCreate);

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

                var authorResource = _mapper.Map <Author, AuthorDTO>(author);

                return(BuildObjectResult(HttpStatusCode.OK, true, value: authorResource));
            }
            catch (Exception ex)
            {
                return(BuildObjectResult(HttpStatusCode.BadRequest, false, ex.Message));
            }
        }
Пример #2
0
        public async Task <ActionResult <AuthorDTO> > UpdateAuthor([FromRoute] int id, [FromBody] SaveAuthorDTO saveAuthorResource)
        {
            try
            {
                var validator        = new SaveAuthorResourceValidator();
                var validationResult = await validator.ValidateAsync(saveAuthorResource);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(validationResult.Errors));
                }

                var authorToBeUpdated = await _authorService.GetAuthorById(id);

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

                var artist = _mapper.Map <SaveAuthorDTO, Author>(saveAuthorResource);

                await _authorService.Update(authorToBeUpdated, artist);

                var updatedAuthor = await _authorService.GetAuthorById(id);

                var updatedAuthorResource = _mapper.Map <Author, AuthorDTO>(updatedAuthor);

                return(BuildObjectResult(HttpStatusCode.OK, true, value: updatedAuthorResource));
            }
            catch (Exception ex)
            {
                return(BuildObjectResult(HttpStatusCode.BadRequest, false, ex.Message));
            }
        }