Ejemplo n.º 1
0
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            c_libraryRepository.AddAuthor(authorEntity);

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

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

            IEnumerable <LinkDto> links = CreateLinksForAuthor(authorDto.Id, null);

            var linkedResourcesToReturn = authorDto.ShapeData(null) as IDictionary <string, object>;

            linkedResourcesToReturn.Add("links", links);

            return(CreatedAtRoute(
                       RouteNames.GetAuthorRoute,
                       new
            {
                id = linkedResourcesToReturn["Id"]
            },
                       linkedResourcesToReturn)); //the Response also holds the URI for the newly created resource, so its ID as well.
        }
Ejemplo n.º 2
0
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author,
                                          [FromHeader(Name = "Accept")] string mediaType)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            _repository.AddAuthor(authorEntity);

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

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

            if (mediaType == Startup.VendorMediaType)
            {
                IEnumerable <LinkDto>        links = CreateLinksForAuthor(authorToReturn.Id, null);
                IDictionary <string, object> linkedResourceToReturn = authorToReturn.ShapeData(null);

                linkedResourceToReturn.Add("links", links);

                return(CreatedAtRoute("GetAuthor", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn));
            }

            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn));
        }
Ejemplo n.º 3
0
        [RequestHeaderMatchesMediaType("Content-type", new[] { "application/vnd.mike.author.full+json" })]  //with versioning media types
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

            var authorEntity = author.ConvertToAuthorEntity();

            _libraryRepository.AddAuthor(authorEntity);
            if (!_libraryRepository.Save())
            {
                throw new Exception();  //with global exception handling, we can throw exception
                //return StatusCode(500, "problem");
            }
            var authorToReturn = authorEntity.ConvertToAuthorDto();
            var links          = CreateLinksForAuthor(authorToReturn.Id, null);

            //add links to post author, convert authordto to expandoobject
            var linkedResourceToReturn = authorToReturn.ShapeData() as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            //need a name on the get method call to use it here
            //return CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn);
            //with links
            return(CreatedAtRoute("GetAuthor", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn));
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto 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.");  // Since this is uncaught it will be handled by .useExceptionHandler
            }

            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));
        }
        //Additional Constraints ...
        //[RequestHeaderMatchesMediaType(HeaderNames.Accept, new[] { "dsawdfdsf" })]

        public IActionResult CreateDeadAuthor([FromBody] CreateDeadAuthor author, [FromHeader(Name = Headers.Accept)] string mediaType)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            _repo.AddAuthor(newAuthor);
            if (!_repo.Save())
            {
                throw new Exception("Failed to Create new author");
            }
            var createdAuthor = Mapper.Map <Author>(newAuthor);

            var currentMediaType = new MediaType(mediaType);

            var includeLinks = currentMediaType.IsSubsetOf(VendorMediaType.HateoasLinksMediaType);

            if (includeLinks)
            {
                var links = CreateLinks(createdAuthor.Id, null);

                var linkedResourceToReturn = createdAuthor.ShapeData(null) as IDictionary <string, object>;
                linkedResourceToReturn.Add("links", links);


                return(CreatedAtRoute(nameof(GetAuthor), new { id = linkedResourceToReturn[nameof(Author.Id)] }, linkedResourceToReturn));
            }
            return(CreatedAtRoute(nameof(GetAuthor), new { id = createdAuthor.Id }, createdAuthor));
        }
        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));
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto authorForCreation)
        {
            if (authorForCreation == null)
            {
                return(BadRequest());
            }

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

            _libraryRepository.AddAuthor(authorEntity);

            if (!_libraryRepository.Save())
            {
                // if we handle it like this, exception creation has a cost, but the handling is solved genericly by the middleware
                throw new Exception("Creating an authord failed to save.");

                // if we handle like this, this code will be duplicated in some places, instead of handling it by the middleware
                // but no exeption creation is necessary
                //return StatusCode(500, "A problem occurred");
            }

            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));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest()); // 400
            }
            var authorEntity = Mapper.Map <Author>(author);

            libraryRepository.AddAuthor(authorEntity);

            if (!(await libraryRepository.Save()))
            {
                throw new Exception("Creating an author failed on save.");
                // return StatusCode(500, "Creating an author failed on save.");
            }

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

            /* We'll do the save as with GetAuthor, expect
             * we'll pass null for the fields when shapping the data.
             */
            var links = CreateLinksForAuthor(authorToReturn.Id, null);

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

            linkedResourceToReturn.Add("links", links);

            // Since linkedResourceToReturn is a dictionary, we should use ["Id"]
            return(CreatedAtRoute("GetAuthor",
                                  new { id = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
Ejemplo n.º 9
0
        public IActionResult CreateAuthor([FromBody] AuthorCreateVM authorVM, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (authorVM == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObject(ModelState));
            }
            var author = Mapper.Map <Author>(authorVM);

            libraryRepository.AddAuthor(author);
            if (!libraryRepository.Save())
            {
                throw new Exception("Creating author failed to save");
            }
            var createdAuthor = Mapper.Map <AuthorVM>(author);

            if (mediaType == "application/vnd.marvin.hateoas+json")
            {
                var links = CreateAuthorLinks(createdAuthor.Id, null);
                var linkedResourceToReturn = createdAuthor.ShapeData(null)
                                             as IDictionary <string, object>;

                linkedResourceToReturn.Add("links", links);
                return(CreatedAtRoute("GetAuthor", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn));
            }
            else
            {
                return(CreatedAtRoute("GetAuthor",
                                      new { id = createdAuthor.Id },
                                      createdAuthor));
            }
        }
Ejemplo n.º 10
0
        public IActionResult CreateAuthor([FromBody] AuthorDtoCreating Autor)
        {
            if (Autor == null)
            {
                return(BadRequest());
            }
            if (Autor.FirstName == Autor.LastName)
            {
                ModelState.AddModelError(nameof(AuthorDtoCreating),
                                         "the title and description cannot be the same");
            }
            if (!ModelState.IsValid)
            {
                return(new UnProccessableObjectResult(ModelState));
            }
            Author _Autor = Mapper.Map <Author>(Autor);

            Repo.AddAuthor(_Autor);


            if (!Repo.Save())
            {
                return(StatusCode(500, "There was a problem in the server"));
            }
            var _AuthorToResponse = Mapper.Map <AuthorToReturn>(_Autor);

            //hace una especie de request al metodo Author de arriba por eso devuelve el objeto con la edad y no
            //el dateofbirth
            return(CreatedAtRoute("Author", new { id = _AuthorToResponse.Id }, _AuthorToResponse));
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            // make sure request body was correctly serialized to AuthorForCreationDto
            if (author == null)
            {
                return(BadRequest());
            }

            // map the AuthorForCreationDto to an author entity
            var authorEntity = Mapper.Map <Author>(author);

            // add the entity to the context
            _libraryRepository.AddAuthor(authorEntity);
            // save the context to the database
            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on save.");
            }

            // map the result
            var authorToReturn = Mapper.Map <AuthorDto>(authorEntity);

            // create all HATEOS links for new author
            var links = CreateLinksForAuthor(authorToReturn.Id, null);

            // add links to response
            var linkedResourceToReturn = authorToReturn.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetAuthor",
                                  new { id = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
Ejemplo n.º 12
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);

            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));
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto 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.");
                // 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));
        }
Ejemplo n.º 14
0
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto authorForCreation)
        {
            if (authorForCreation == null)
            {
                return(BadRequest());
            }

            var authorInDb = Mapper.Map <Author>(authorForCreation);

            _libraryRepository.AddAuthor(authorInDb);

            if (!_libraryRepository.Save())
            {
                throw new Exception("Something went wrong.");
            }

            var authorToReturn         = Mapper.Map <AuthorDto>(authorInDb);
            var links                  = CreateLinksForAuthor(authorInDb.Id, null);
            var linkedResourceToReturn = authorToReturn.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetNewAuthor",
                                  new { authorId = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            _libraryRepository.AddAuthor(authorEntity);
            if (!_libraryRepository.Save())
            {
                //global exception handler will catch the error
                throw new ApplicationException("Creating an author failed on save");
                //return StatusCode(500, "A problem happened with handling your request");
            }

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

            //we are passing null because no data shaping
            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));

            //GetAuthor is name given to method GetAuthor
            //Id is the Id for Author
            //authorToReturn will be serialised in the body
            //return CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn);
        }
Ejemplo n.º 16
0
        public IActionResult CreateAuthor([FromBody] Models.AuthorForCreation author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

            if (author.FirstName == author.LastName)
            {
                ModelState.AddModelError(nameof(Models.AuthorForCreation), "The first name and the last name of the author must be different");
            }

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

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

            libraryRepository.AddAuthor(authorCreated);
            if (!libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on save.");
                //return StatusCode(500, "A problem ocurred when handling the request.");
            }

            var authorToReturn = Mapper.Map <Models.Author>(authorCreated);

            return(CreatedAtRoute("GetAuthor", new { authorId = authorToReturn.Id }, authorToReturn));
        }
Ejemplo n.º 17
0
        [RequestHeaderMatchesMediaType("Content-type", new[] { "application/vnd.marvin.author.full+json" })] // Versioning support from request header
        public IActionResult CreateAuthor(
            [FromBody] AuthorForCreationDto author)
        {
            // This automatically handles basic validation (such as text where a datetime is expected) because
            // the incoming data could not be serialized into the incoming object type (AuthorForCreationDto)
            // and the incoming object type is subsequently null.
            if (author == null)
            {
                return(BadRequest());
            }

            // TODO - Add some validation

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

            _libraryRepository.AddAuthor(authorEntity);

            // Option one: Throw exception if save failed to be captured by global error handler in middleware.
            // Expensive but allows central error handling.
            if (!_libraryRepository.Save())
            {
                throw new Exception();
            }

            // Need to return URI which consumer can use to get created entity. Can use CreatedAtRoute for this.

            // Deprecated and replaced with support for HATEOAS on POST (below).
            //return CreatedAtRoute(
            //	routeName: "GetAuthor",
            //	routeValues: new { id = authorEntity.Id },
            //	value: AutoMapper.Mapper.Map<AuthorDto>(authorEntity));

            // Option two: Return 500 if save failed. Bypasses middleware but it's performant.
            //return
            //	_libraryRepository.Save()
            //	?
            //	CreatedAtRoute()
            //	:
            //	StatusCode(500, "err");

            // Support for dynamic HATEOAS on POST
            var authorToReturn = AutoMapper.Mapper.Map <AuthorDto>(authorEntity);
            var links          = CreateLinksForAuthor(authorToReturn.Id, null);                        // Null fields as no data shaping
            // Need an ExpandoObject to enable us to add links to the response
            var linkedResourceToReturn = (IDictionary <string, object>)authorToReturn.ShapeData(null); // ExpandoObject returned when null input provided

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute(
                       routeName: "GetAuthor",
                       routeValues: new { id = linkedResourceToReturn["Id"] },          // Happens to be same as object Id but technically should be from ExpandoObject
                       value: linkedResourceToReturn));
        }
        public IActionResult CreateAuthorCollections([FromBody] IEnumerable <AuthorForCreationDto> dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            var entities = Mapper.Map <IEnumerable <Author> >(dto);

            foreach (var author in entities)
            {
                libraryRepository.AddAuthor(author);
            }

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

            var authorsToReturn = Mapper.Map <IEnumerable <AuthorDto> >(entities);

            return(CreatedAtAction(
                       nameof(GetAuthorCollection),
                       new { ids = string.Join(",", authorsToReturn.Select(x => x.Id)) },
                       authorsToReturn));
        }
        public IActionResult CreateAuthorsCollection([FromBody] IEnumerable <AuthorForCreationDto> authors)
        {
            if (authors == null || !authors.Any())
            {
                return(BadRequest());
            }

            var authorEntities = Mapper.Map <IEnumerable <Author> >(authors);

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

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

            var authorsToReturn = Mapper.Map <IEnumerable <AuthorDto> >(authorEntities);

            var idsAsString = string.Join(",", authorEntities.Select(x => x.Id));

            return(CreatedAtRoute("GetAuthorsCollection", new { ids = idsAsString }, authorsToReturn));
        }
Ejemplo n.º 20
0
        public IActionResult CreateAuthorCollection([FromBody] IEnumerable <AuthorForCreationDto> authorCollection) // create multiple authors at once
        {
            if (authorCollection == null)
            {
                return(BadRequest());
            }

            // map each author to our author class
            var authorEntities = Mapper.Map <IEnumerable <Author> >(authorCollection);

            // add each author
            foreach (var author in authorEntities)
            {
                _libraryRepository.AddAuthor(author);
            }

            // save
            if (!_libraryRepository.Save())
            {
                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));

            // created at the GetAuthorCollection route with the ids (idsAsString), and authorCollectionToReturn as the response body
            // **Note: this lets you use response header to find the GUID of the created authors, allowing you to then to GET the response header and get back your created authors.
            return(CreatedAtRoute("GetAuthorCollection", new { ids = idsAsString }, authorCollectionToReturn));
        }
Ejemplo n.º 21
0
        public IActionResult CreateAuthorCollection([FromBody] IEnumerable <AuthorForCreationDto> authorCollection)
        {
            if (authorCollection == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

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

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

            if (!_libraryRepository.Save())
            {
                throw new Exception("Creation 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 { ids = idsAsString }, authorCollectionToReturn));
        }
        public IActionResult CreateAuthorCollection([FromBody] ICollection <AuthorForCreationDto> listAuthor)
        {
            if (listAuthor == null)
            {
                return(BadRequest());
            }

            var authorEntities = AutoMapper.Mapper.Map <ICollection <Author> >(listAuthor).ToList();

            authorEntities.ForEach(a =>
            {
                _libraryRepository.AddAuthor(a);
            });

            if (!_libraryRepository.Save())
            {
                throw new Exception("An error occurred while saving collection of author.");
            }

            var authorsToReturn = AutoMapper.Mapper.Map <ICollection <AuthorDto> >(authorEntities);

            var ids = string.Join(",", authorsToReturn.Select(a => a.Id));

            return(CreatedAtRoute("GetAuthorCollection", new { ids = ids }, authorsToReturn));
        }
Ejemplo n.º 23
0
        public ActionResult Create(AuthorCreateViewModel viewModel)
        {
            int id = 0;

            try
            {
                if (!ModelState.IsValid)
                {
                    throw new ModelNotValidException("Podaci nisu ispravno uneseni");
                }

                _repository.AddAuthor(viewModel);
                id = _repository.GetLastAuthorId();
                TempData["InfoMsg"] = "Novi autor uspješno kreiran";
            }
            catch (Exception e)
            {
                if (e is ModelNotValidException)
                {
                    TempData["ErrorMsg"] = e.Message;
                }
                else
                {
                    TempData["ErrorMsg"] = "Pogreška prilikom kreiranja novog autora";
                }

                return(View(viewModel));
            }

            return(RedirectToAction("Details", new { @id = id }));
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            _libraryrepository.AddAuthor(authorentity);

            // _libraryrepository.Save();

            if (!_libraryrepository.Save())
            {
                return(StatusCode(500, "Un problema guardando en la base de datos"));
            }


            var authorReturn = Mapper.Map <AuthorsDTO>(authorentity);

            try
            {
                return(CreatedAtRoute("GetAuthor", new { id = authorReturn.id }, authorReturn));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
        public IActionResult CreateAuthorCollection(
            [FromBody] IEnumerable <AuthorForCreationDto> authorCollection)
        {
            if (authorCollection == null)
            {
                return(BadRequest());
            }

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

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

            if (!_libraryRepository.Save())
            {
                throw new Exception("An error occured while saving your collection of authors");
            }

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

            return(CreatedAtRoute("GetAuthorCollection",
                                  new { ids = idsAsString },
                                  authorCollectionToReturn));
        }
Ejemplo n.º 26
0
        public IActionResult CreateAuthorCollection(
            [FromBody] IEnumerable <AuthorForCreationDto> authorCollection)
        {
            //Check for collection input
            if (authorCollection == null)
            {
                return(BadRequest());
            }

            //Map entity
            var authorEntities = Mapper.Map <IEnumerable <Author> >(authorCollection);

            //Add authors to repository
            foreach (var author in authorEntities)
            {
                _libraryRepository.AddAuthor(author);
            }

            if (!_libraryRepository.Save())
            {
                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 { ids = idsAsString },
                                  authorCollectionToReturn));
            //return Ok();
        }
        public ActionResult CreateAuthorCollection([FromBody] IEnumerable <AuthorForCreationDto> authorCollections)
        {
            if (authorCollections == null)
            {
                return(BadRequest());
            }

            var authorEntities = AutoMapper.Mapper.Map <IEnumerable <Author> >(authorCollections);

            foreach (var authorEntity in authorEntities)
            {
                _libraryRepository.AddAuthor(authorEntity);
            }

            if (!_libraryRepository.Save())
            {
                throw new Exception("something went wrong");
            }

            var authors = AutoMapper.Mapper.Map <IEnumerable <AuthorDto> >(authorEntities);

            var idsAsString = string.Join(",", authors.Select(author => author.Id));

            return(CreatedAtRoute("GetAuthorCollection", new { ids = idsAsString }, authors));
        }
Ejemplo n.º 28
0
        public IActionResult CreateAuthorCollections([FromBody] IEnumerable <AuthorForCreationModel> authorCollections)
        {
            if (authorCollections == null)
            {
                return(BadRequest());
            }

            var authorEntities = Mapper.Map <IEnumerable <Author> >(authorCollections);

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

            if (!_libraryRepository.Save())
            {
                throw new Exception("Something went wrong. Please try again later.");
            }

            var authors = Mapper.Map <IEnumerable <AuthorsModel> >(authorEntities);


            var ids = string.Join(",", authorEntities.Select(a => a.Id));


            return(new CreatedAtRouteResult("GetAuthorCollections", new { ids = ids }, authorEntities));
        }
Ejemplo n.º 29
0
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest()); // return 400 error
            }

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

            // the entity has not been added to the database yet, it has been added to the dbcontext
            // which represents a session with the database.
            _libraryRepository.AddAuthor(authorEntity);
            // To persist the changes we have to save on the repo.
            // if it fails we will return 500 error

            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on save");
                // return StatusCode(500, "An error occured");
            }

            var authorToReturn         = Mapper.Map <AuthorDto>(authorEntity);
            var links                  = CreateLinksForAuthor(authorToReturn.Id, null);
            var linkedResourceToReturn = authorToReturn.ShapeData(null) as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", linkedResourceToReturn);
            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, linkedResourceToReturn));
        }
Ejemplo n.º 30
0
        public IActionResult CreateAuthorCollection(
            [FromBody] IEnumerable <AuthorInputDto> authorCollection)
        {
            if (authorCollection == null)
            {
                return(BadRequest());
            }

            var authors = AutoMapper.Mapper.Map <IEnumerable <Author> >(authorCollection);

            foreach (var author in authors)
            {
                _libraryRepository.AddAuthor(author);
            }

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

            var newAuthors = AutoMapper.Mapper.Map <IEnumerable <AuthorDto> >(authors);

            var ids = string.Join(",", newAuthors.Select(x => x.Id));

            return(CreatedAtRoute("GetAuthorCollection",
                                  new { ids = ids },
                                  newAuthors));
        }