Ejemplo n.º 1
0
        public async Task <ActionResult> CreateAuthorAsync([FromBody] AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            await _libraryRepository.AddAuthorAsync(authorEntity);

            if (!await _libraryRepository.SaveChangesAsync())
            {
                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 { authorId = linkedResourceToReturn["Id"] }, linkedResourceToReturn));
        }
        public async Task <ActionResult <IEnumerable <AuthorDto> > > CreateAuthorCollectionAsync(
            [FromBody] IEnumerable <AuthorForCreationDto> authorCollection)
        {
            if (authorCollection == null)
            {
                return(BadRequest());
            }

            var authorEntities = _mapper.Map <IEnumerable <Author> >(authorCollection);

            foreach (var author in authorEntities)
            {
                await _libraryRepository.AddAuthorAsync(author);
            }

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

            var authorCollectionToReturn = _mapper.Map <IEnumerable <AuthorDto> >(authorEntities);
            var idsAsString = string.Join(",", authorCollectionToReturn.Select(a => a.Id));

            return(CreatedAtRoute("GetAuthorCollection", new { authorIds = idsAsString }, authorCollectionToReturn));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest("Error"));
            }
            var authorEntity = AutoMapper.Mapper.Map <Author>(author);

            _libraryRepository.AddAuthorAsync(authorEntity);
            if (!(await _libraryRepository.SaveAsync()))
            {
                throw new Exception("Creating an author failed on save.");
                // return StatusCode(500, "A problem happened with handling your request.");
            }
            var authorToReturn = AutoMapper.Mapper.Map <AuthorDto>(authorEntity);

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