Exemplo n.º 1
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.º 2
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] SaveAuthorResource resource)
        {
            if (!ModelState.IsValid)
            {
                var badresult = new ResponseData
                {
                    Data    = null,
                    Message = string.Join(" ", ModelState.GetErrorMessages().ToArray()),
                    Success = false
                };
                return(Ok(badresult));
            }

            var author         = mapper.Map <SaveAuthorResource, Author>(resource);
            var authorResponse = await authorService.UpdateAsync(id, author);

            var authorResource = mapper.Map <Author, AuthorResource>(authorResponse.Author);

            var result = new ResponseData
            {
                Data    = authorResource,
                Message = authorResponse.Message,
                Success = authorResponse.Success
            };

            return(Ok(result));
        }
Exemplo n.º 3
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));
        }