Exemplo n.º 1
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));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <AuthorResource> > UpdateAuthor(int id, [FromBody] SaveAuthorResource saveAuthorResource)
        {
            var validator        = new SaveAuthorResourceValidator();
            var validationResult = await validator.ValidateAsync(saveAuthorResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var authorToBeUpdated = await _authorService.GetAuthorById(id);

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

            var author = _mapper.Map <SaveAuthorResource, Author>(saveAuthorResource);

            await _authorService.UpdateAuthor(authorToBeUpdated, author);

            var updatedAuthor = await _authorService.GetAuthorById(id);

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

            return(Ok(updatedAuthorResource));
        }
Exemplo n.º 3
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));
            }
        }
Exemplo n.º 4
0
        public async Task <ActionResult <AuthorResource> > CreateAuthor([FromBody] SaveAuthorResource saveAuthorResource)
        {
            var validator        = new SaveAuthorResourceValidator();
            var validationResult = await validator.ValidateAsync(saveAuthorResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var authorToCreate = _mapper.Map <SaveAuthorResource, Author>(saveAuthorResource);

            var newAuthor = await _authorService.CreateAuthor(authorToCreate);

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

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

            return(Ok(authorResource));
        }