Beispiel #1
0
        public IActionResult CreateAuthorWithDateOfDeath(
            [FromBody] AuthorForCreationWithDateOfDeathDto 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));
        }
Beispiel #2
0
        //[RequestHeaderMatchesMediaType("Accept", new[] { "application/json" })] // multiples because AttributeUsage > AllowMultiples
        public async Task <IActionResult> CreateAuthorWithDateOfDeath(
            [FromBody] AuthorForCreationWithDateOfDeathDto 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));
        }
        //[RequestHeaderMatchesMediaType("Accept",
        //    new[] { "..." })]
        public IActionResult CreateAuthorWithDateOfDeath([FromBody] AuthorForCreationWithDateOfDeathDto 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));
        }
Beispiel #4
0
        public IActionResult CreateAuthorWithDateOfDeath([FromBody] AuthorForCreationWithDateOfDeathDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            LibraryRepository.AddAuthor(authorEntity);

            if (!LibraryRepository.Save())
            {
                throw new Exception("Ocorreu um problema ao salvar a criação do autor");
            }
            ;

            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));
        }
Beispiel #5
0
        public IActionResult AuthorForCreationWithDateOfDeathDto([FromBody] AuthorForCreationWithDateOfDeathDto author)
        {
            if (author.IsNull())
            {
                return(BadRequest());
            }


            var authorEntity = new Author();

            authorEntity.UpdateDestination <Author, AuthorForCreationWithDateOfDeathDto>(author);

            _libraryRepository.AddAuthor(authorEntity);

            if (!_libraryRepository.Save())
            {
                return(StatusCode(500, "A problem happend with handling your request."));
            }

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

            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));
        }
Beispiel #6
0
                                               "application/vnd.marvin.authorwithdateofdeath.full+xml" })] // v2.1 - Support XML for DoD inputs (goodness knows why)
        // For metadata output we might want to create additional constraints or we might want to use the same approach as we did for HATEOAS
        // Add a new constraint for the Accept header...
        // This causes an error unless the custom attribute RequestHeaderMatchesMediaType is adjusted to allow multiple attributes of this type.
        // [RequestHeaderMatchesMediaType("Accept", new[] { "..." })]
        //
        // Other options include projects which attempt to describe languages for link representations and to include media type descriptions in
        // a resource representation with links to handle metadata without separate documents...
        // HAL - Hypertext Application Language
        // SIREN - Structured Interface for Representation Entities
        // JSON-LD - Light-weight linked data format
        // JSON-API - Specification for building JSON APIs which includes a way to represent links
        // ODATA - Effort to standardise REST APIs! Becoming industry standard?
        public IActionResult CreateAuthorWithDateOfDeath(
            [FromBody] AuthorForCreationWithDateOfDeathDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            _libraryRepository.AddAuthor(authorEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception();
            }

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

            linkedResourceToReturn.Add("links", links);

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

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

            _libraryRepository.AddAuthor(authorModel);

            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>(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));
        }
Beispiel #8
0
        public IActionResult CreateAuthorWithDateOfDeath([FromBody] AuthorForCreationWithDateOfDeathDto 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.
        }
Beispiel #9
0
        [RequestHeaderMatchesMediaType("Content-type", new[] { "application/vnd.mike.author.authorwithdateofdeath+json" })]  //action constraint, selection action based on content type
        public IActionResult CreateAuthorWithDateOfDeath([FromBody] AuthorForCreationWithDateOfDeathDto 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));
        }
Beispiel #10
0
        public IActionResult CreateAuthorWithDateOfDeath([FromBody] AuthorForCreationWithDateOfDeathDto 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));
        }
        public IActionResult CreateAuthorWithDateOfDeath(
            [FromBody] AuthorForCreationWithDateOfDeathDto author, [FromHeader(Name = "Accept")] string mediaType)
        {
            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);

            if (mediaType == "application/vnd.marvin.authorwithdateofdeath.full.hateoas+json")
            {
            }
            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));
        }
Beispiel #12
0
        public async Task <IActionResult> CreateAuthorWithDateOfDeath(AuthorForCreationWithDateOfDeathDto author)
        {
            Guard.Against.Null(author, nameof(author));

            var command = new CreateAuthorWithDeathDateCommand(author.FirstName, author.LastName,
                                                               author.DateOfBirth, author.DateOfDeath, author.MainCategory);

            Result <Author> result = await _messages.Dispatch(command);

            if (result.IsFailure)
            {
                return(BadRequest(result.Error));
            }

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

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

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

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute(nameof(GetAuthor),
                                  new { authorId = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
        //  Output Formatters =>
        // [RequestHeaderMatchesMediaType("Accept", new[] { "..." })]
        public IActionResult CreateAuthorWithDateOfDeath(
            [FromBody] AuthorForCreationWithDateOfDeathDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

            _libraryRepository.AddAuthor(authorEntity);

            if (!_libraryRepository.Save())
            {
                //  This exception will redirect/invoke the Global exception handler at Startup.cs
                throw new Exception("Creating an author failed on save.");

                // return StatusCode(500, "A problem happened with handling your request.");    // 500 => Internal Server Error
            }

            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 CreateAuthor([FromBody] AuthorForCreationWithDateOfDeathDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

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

            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));
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreationWithDateOfDeathDto authorForCreation)
        {
            // same code as withoutdateofdeath bc of automapper
            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));
        }
 public static Author Map(this AuthorForCreationWithDateOfDeathDto author)
 {
     return(new Author
     {
         Books = author.Books.Map().ToList(),
         DateOfBirth = author.DateOfBirth,
         DateOfDeath = author.DateOfDeath,
         FirstName = author.FirstName,
         LastName = author.LastName,
         Genre = author.Genre
     });
 }
        public ActionResult <AuthorDto> CreateAuthorWithDateOfDeath(AuthorForCreationWithDateOfDeathDto author)
        {
            var authorEntity = _mapper.Map <Author>(author);

            _context.AddAuthor(authorEntity);
            _context.Save();
            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 IActionResult CreateAuthorWithDateOfDeath(AuthorForCreationWithDateOfDeathDto authorToAdd)
        {
            Author authorEntity = _mapper.Map <Author>(authorToAdd);

            _repository.AddAuthor(authorEntity);
            _repository.Save();

            var linkResourceToReturn = _mapper.Map <AuthorDto>(authorEntity).ShapeData() as IDictionary <string, object>;
            var links = CreateLinksForAuthor(authorEntity.Id);

            linkResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetAuthor", new { authorId = authorEntity.Id }, linkResourceToReturn));
        }
Beispiel #19
0
        public static Author ConvertToAuthorEntity(this AuthorForCreationWithDateOfDeathDto inputAuthor)
        {
            Author author = new Author()
            {
                FirstName   = inputAuthor.FirstName,
                LastName    = inputAuthor.LastName,
                DateOfBirth = inputAuthor.DateOfBirth,
                DateOfDeath = inputAuthor.DateOfDeath,
                Genre       = inputAuthor.Genre
            };

            if (inputAuthor.Books != null)
            {
                author.Books = inputAuthor.Books.ConvertToBookEntityList();
            }
            return(author);
        }
        public IActionResult CreateAuthor([FromBody] AuthorForCreationWithDateOfDeathDto 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 to save.");
            }

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

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

            _libraryRepository.AddAuthor(authorEntity);
            if (!_libraryRepository.Save())
            {
                return(StatusCode(500, "A problem occurred while createing the author."));
            }
            var authorToReturn = _mapper.Map <AuthorDto>(authorEntity);
            // pass null as there we not need any data shapeing
            var links = CreateLinksForAuthor(authorEntity.Id, null);
            var linkedResourceToReturn = authorToReturn.ShapeData(null) as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);
            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, linkedResourceToReturn));
        }
Beispiel #22
0
        public ActionResult <AuthorDto> CreateAuthorWithDateOfDeath(AuthorForCreationWithDateOfDeathDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

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

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

            var links = CreateLinkForAuthor(authorDto.Id, null);

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

            linkedResourceForReturn.Add("links", links);

            return(CreatedAtRoute("GetAuthor", new { authorId = linkedResourceForReturn["Id"] }, linkedResourceForReturn));
        }
Beispiel #23
0
        public IActionResult CreateAuthorWithDateOfDeath(AuthorForCreationWithDateOfDeathDto author)
        {
            var authorEntity = _mapper.Map <Entities.Author>(author);

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

            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 status code : 201 (Created)
            return(CreatedAtRoute("GetAuthor",
                                  new { authorId = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
Beispiel #24
0
        public IActionResult CreateAuthorWithDateOfDeath([FromBody] AuthorForCreationWithDateOfDeathDto author)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var authorToEntity = Mapper.Map <Author>(author);

            _libraryRepository.AddAuthor(authorToEntity);
            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on save.");
                // return StatusCode(500, "An unexpected fault happened. Try again later.");
            }
            var authorToReturn = Mapper.Map <AuthorDto>(authorToEntity);

            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));
        }