public IActionResult CreateAuthor(
            [FromBody] AuthorForCreateDto authorForCreateDto)
        {
            if (authorForCreateDto == null)
            {
                return(BadRequest());
            }

            var authorModel = Mapper.Map <Author>(authorForCreateDto);

            _libraryRepository.AddAuthor(authorModel);

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

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

            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));
        }
Esempio n. 2
0
        public async Task <IActionResult> CreateAuthor([FromBody] AuthorForCreateDto author)
        {
            try
            {
                if (author == null)
                {
                    _logger.LogWarn("Empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn("Invalid data received");
                    return(BadRequest(ModelState));
                }
                var authorForCreate = _mapper.Map <Author>(author);
                var IsSuccess       = await _authorRepository.Create(authorForCreate);

                if (!IsSuccess)
                {
                    _logger.LogError("Author Creation Failed");
                    return(InternalError("Author Creation Failed"));
                }
                _logger.LogInfo("Author Created");
                return(Created("Create", new { author }));
            } catch (Exception _ex)
            {
                return(InternalError($"{_ex.Message}\n{_ex.InnerException}"));
            }
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreateDto authorForCreateDto)
        {
            if (authorForCreateDto == null)
            {
                return(BadRequest());
            }

            TryValidateModel(authorForCreateDto);

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

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

            _libraryRepository.AddAuthor(author);

            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on server");
                //return StatusCode(500,"Unexpected problem occurs, try again later!")
            }

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

            //do not working
            //return CreatedAtRoute("GetAuthor", new { id = authorDto.Id });


            //return new JsonResult(authorDto);
            return(CreatedAtAction(nameof(GetAuthor), new { id = authorDto.Id }, author));
        }
Esempio n. 4
0
        public ActionResult <AuthorDto> CreateAuthor(AuthorForCreateDto 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));
        }
Esempio n. 5
0
        public ActionResult <AuthorDto> CreateAuthor(AuthorForCreateDto authorCreateDto) // [ApiController] themself to deserializes json
        {
            // we dont need to check if argument is null, because [ApiController] do this for us
            var authorEntity = this._mapper.Map <Author>(authorCreateDto);

            this._repos.AddAuthor(authorEntity);
            this._repos.Save();

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

            // Status 201
            return(CreatedAtRoute("GetAuthor", new { authorId = authorDto.Id }, authorDto));
        }
Esempio n. 6
0
        public IActionResult CreateAuthor([FromBody] AuthorForCreateDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }
            var authorEntity = Mapper.Map <Author>(author);

            libraryRepository.AddAuthor(authorEntity);
            if (!libraryRepository.Save())
            {
                throw new Exception();
            }
            var authorToReturn = Mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn));
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreateDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }
            var authorEntity = Mapper.Map <Author>(author);

            _libraryRepository.AddAuthor(authorEntity);
            if (!_libraryRepository.Save())
            {
                return(StatusCode(500, "A problem when add author"));
            }

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

            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn));
        }
Esempio n. 8
0
        public IActionResult CreateAuthor([FromBody] AuthorForCreateDto authorForCreateDto)
        {
            if (authorForCreateDto == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var author         = _mapper.Map <Author>(authorForCreateDto);
            var createdAuthor  = _repository.AddAuthor(author);
            var authorToReturn = _mapper.Map <AuthorDto>(createdAuthor);

            return(CreatedAtRoute("GetAuthor",
                                  new { id = authorToReturn.Id },
                                  authorToReturn
                                  ));
        }