public IActionResult CreateAuthor([FromBody] AuthorCreationDto author)
        {
            if (author == null || !ModelState.IsValid)
            {
                return(BadRequest());
            }

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

            _libraryRepository.AddAuthor(authorEntity);
            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on save");
                //return StatusCode(500, "A problem happened with handling your request");
            }

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

            var links = CreateLinksForAuthor(authorToReturn.Id, null);

            var linkedResourceToReturn = authorToReturn.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetAuthor", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn));
        }
        public IActionResult CreateAuthorWithDateOfDeath([FromBody] AuthorCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            libraryRepository.AddAuthor(authorEntity);

            if (!libraryRepository.Save())
            {
                throw new Exception("error !!!");
                ////return StatusCode(500, "some error");
            }

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

            var links = CreateLinksForAuthor(authorDto.Id, null);
            var linkedResourceToReturn = authorDto.ShapeData(null) as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetAuthor",
                                  new { authorID = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
Beispiel #3
0
        public async Task <IActionResult> Post([FromBody] AuthorCreationDto authorCreationDto)
        {
            if (authorCreationDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (!ModelState.IsValid)
            {
                LogModelStateErrors(_logger);
                return(ValidationProblem(ModelState));
            }

            try
            {
                var author = await _authorService.CreateAuthor(authorCreationDto);

                _logger.LogInformation($"Author with Id: {author.Id} created");

                return(CreatedAtAction(
                           "Get",
                           new
                {
                    author.Id
                },
                           author));
            }
            catch (Exception exception)
            {
                _logger.LogError($"Error occurred: {exception.GetMessageWithStackTrace()}");
                return(InternalServerErrorResult("Error occurred creating Author."));
            }
        }
Beispiel #4
0
        public async Task <AuthorDto> CreateAuthor(AuthorCreationDto authorCreationDto)
        {
            ValidateEntity(authorCreationDto);

            var author = _mapper.Map <Author>(authorCreationDto);
            await _authorRepository.CreateAuthorAsync(author);

            return(_mapper.Map <AuthorDto>(author));
        }
Beispiel #5
0
        public ActionResult <AuthorDto> CreateAuthor([FromBody] AuthorCreationDto authorDto)
        {
            var author = _mapper.Map <Author>(authorDto);

            _courseLibraryRepository.AddAuthor(author);
            _courseLibraryRepository.Save();
            var authorToReturn = _mapper.Map <AuthorDto>(author);

            return(CreatedAtRoute("GetAuthor", new { authorId = authorToReturn.Id }, authorToReturn));
        }
Beispiel #6
0
        public ActionResult <AuthorDto> CreateAuthor([FromBody] AuthorCreationDto author)
        {
            var authorEntity = _mapper.Map <Author>(author);

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

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

            return(CreatedAtRoute(nameof(GetAuthor), new { authorId = authorDto.Id }, authorDto));
        }
Beispiel #7
0
        public ActionResult <AuthorDto> CreateAuthor(AuthorCreationDto author)
        {
            var authorEntity = _mapper.Map <Author>(author);

            _libraryRepository.AddAuthor(authorEntity);
            _libraryRepository.Save();

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

            // this refers to public IActionResult GetAuthor(Guid authorId)
            // which has it's own Name GetAuthor plus parameters.
            return(CreatedAtRoute("GetAuthor", new { authorId = authorToReturn.Id }, authorToReturn));
        }
Beispiel #8
0
        public ActionResult <AuthorDto> CreateAuthor(AuthorCreationDto 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));
        }
Beispiel #9
0
        public async Task <ActionResult <Author> > PostAuthor(AuthorCreationDto authorDto)
        {
            if (authorDto == null)
            {
                return(BadRequest("Invalid author details provided."));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new { Author = ModelState, Message = "Incomplete author details." }));
            }

            var id = await _authorService.CreateAsync(_mapper.Map <Author>(authorDto));

            var author = await _authorService.GetById(id);

            return(CreatedAtAction("GetAuthor", new { id = author.Id }, _mapper.Map <GetAuthorWithBooks>(author)));
        }
        public async Task <ActionResult <AuthorDto> > CreateAuthor(AuthorCreationDto authorCreation)
        {
            //if (author == null) // This control provide us from ApiController attribute.
            //{
            //    return BadRequest();
            //}

            var authorEntity = _mapper.Map <Author>(authorCreation);

            _courseLibraryRepository.AddAuthor(authorEntity);
            await _courseLibraryRepository.SaveAsync();

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

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

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

            repository.AddAuthor(authorEntity);

            if (!repository.Save())
            {
                throw new Exception("A problem happened with handling your request.");
            }

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

            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn));
        }
Beispiel #12
0
        public ActionResult <AuthorDto> Post([FromBody] AuthorCreationDto authorCreationDto)
        {
            var author = _mapper.Map <Author>(authorCreationDto);

            _repository.AddAuthor(author);

            if (!_repository.Save())
            {
                throw new InvalidOperationException("Error occurred: Unable to create Author");
            }

            var authorDto = _mapper.Map <AuthorDto>(author);

            return(CreatedAtAction(
                       "GetAuthor",
                       new
            {
                id = authorDto.Id
            },
                       authorDto));
        }
        public IActionResult CreateAuthor([FromBody] AuthorCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }


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

            _libraryRepository.AddAuthor(authorEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception("Error al crear un nuevo Author");
            }


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

            return(CreatedAtRoute("GetAuthor",
                                  new { id = authorToReturn.Id },
                                  authorToReturn));
        }
Beispiel #14
0
        public IActionResult CreateAuthor([FromBody] AuthorCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            libraryRepository.AddAuthor(authorEntity);

            if (!libraryRepository.Save())
            {
                throw new Exception("error !!!");
                ////return StatusCode(500, "some error");
            }

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

            // below line of code provide location URL in response body for newly created resource
            // return CreatedAtRoute("GetAuthor", new { authorID = authorDto.Id }, authorDto);

            return(new JsonResult(authorDto));
        }
Beispiel #15
0
        public async Task <ActionResult> AddAsync([FromBody] AuthorCreationDto authorDto)
        {
            await _authorService.AddAuthorAsync().ConfigureAwait(false);

            return(Ok());
        }