public ActionResult <AuthorDto> CreateAuthor(AuthorsForCreationDto author)
        {
            var authorEntity = _mapper.Map <Entities.Author>(author);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();

            var authorToReturn = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor",
                                  new { authorId = authorToReturn.Id }, authorToReturn));
        }
Ejemplo n.º 2
0
        public IActionResult CreateAuthor([FromBody] AuthorsForCreationDto author)
        {
            try
            {
                //throw new Exception();
                //Validations
                if (author == null)
                {
                    return(BadRequest("No Author Info available"));
                }

                if (author.Name == author.Description)
                {
                    _logger.LogInformation($"{author} not found");
                    ModelState.AddModelError("Description", "The author name and the description cannot be the same");
                }

                //Is the model in a valid state
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }


                //Demo Purpose will be improved
                //Get Max Book Id
                var maxAuthorId = AuthorsDatastore.Current.Authors.Select(a => a.Id).Max();
                //The above method is slow
                //The above method does not take into account scenarios where multiple users will try to get an ID simultaneously


                var newAuthor = new AuthorDto()
                {
                    Id          = ++maxAuthorId,
                    Name        = author.Name,
                    Description = author.Description,
                    Books       = author.Books
                };

                AuthorsDatastore.Current.Authors.Add(newAuthor);

                //For Post the advised response is 201 Created
                return(CreatedAtRoute("GetAuthor", new { id = newAuthor.Id }, newAuthor));
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"Exception while getting the Author {author}", ex);
                return(StatusCode(500, "A problem happened while handling your request"));
            }
        }
        public ActionResult <AuthorDto> CreateAuthor([FromBody] AuthorsForCreationDto author)
        {
            //map author
            var authorEntity = this.mapper.Map <Entities.Author>(author);

            //create author, DB is black box logic
            this.courseLibraryRepository.AddAuthor(authorEntity);
            this.courseLibraryRepository.Save();

            //return new author detail
            var authorReturn = this.mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute(
                       "GetAuthor",
                       new { authorId = authorReturn.Id },
                       authorReturn
                       ));
        }
        public IActionResult CreateAuthor([FromBody] AuthorsForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            _libraryRepository.AddAuthor(authorEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on save.");
            }

            var authorToReturn = Mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn));
        }
Ejemplo n.º 5
0
        public IActionResult CreateAuthor([FromBody] AuthorsForCreationDto author)
        {
            //Validations
            if (author == null)
            {
                return(BadRequest("No Author Info available"));
            }

            if (author.Name == author.Description)
            {
                ModelState.AddModelError("Description", "The author name and the description cannot be the same");
            }

            //Is the model in a valid state
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            //Demo Purpose will be improved
            //Get Max Book Id
            var maxAuthorId = AuthorsDatastore.Current.Authors.Select(a => a.Id).Max();
            //The above method is slow
            //The above method does not take into account scenarios where multiple users will try to get an ID simultaneously


            var newAuthor = new AuthorDto()
            {
                Id          = ++maxAuthorId,
                Name        = author.Name,
                Description = author.Description,
                Books       = author.Books
            };

            AuthorsDatastore.Current.Authors.Add(newAuthor);

            //For Post the advised response is 201 Created
            return(CreatedAtRoute("GetAuthor", new { id = newAuthor.Id }, newAuthor));
        }