public IActionResult GetAuthor(Guid id, [FromQuery] string fields, [FromHeader(Name = "Accept")] string mediaType) { if (!_typeHelperService.TypeHasProperties <AuthorDto>(fields)) { return(BadRequest()); } Author authorFromRepo = _repository.GetAuthor(id); if (authorFromRepo == null) { return(NotFound()); } AuthorDto author = Mapper.Map <AuthorDto>(authorFromRepo); if (mediaType == Startup.VendorMediaType) { IEnumerable <LinkDto> links = CreateLinksForAuthor(id, fields); IDictionary <string, object> linkedResourceToReturn = author.ShapeData(fields); linkedResourceToReturn.Add("links", links); return(Ok(linkedResourceToReturn)); } return(Ok(author.ShapeData(fields))); }
//[RequestHeaderMatchesMediaType("Accept", new[] { "..."})] public IActionResult AuthorForCreationWithDateOfDeath([FromBody] AuthorForCreationWithDateOfDeathDto author) { if (author == null) { return(BadRequest()); } Author 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 happend with handling your request."); } AuthorDto 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] 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 GetAuthors(int id, [FromQuery] string fields) { if (!this.typeHelperService.TypeHasProperties <AuthorDto>(fields)) { return(BadRequest()); } var author = repository.GetAuthor(id); if (author == null) { return(NotFound()); } AuthorDto authorDto = Mapper.Map <AuthorDto>(author); return(Ok(authorDto.ShapeData(fields))); }
public IActionResult GetAuthor(Guid id, [FromQuery] string fields) { if (!_typeHelperService.TypeHasProperty <AuthorDto>(fields)) { return(BadRequest()); } var author = _libraryRepository.GetAuthor(id); if (author == null) { return(NotFound()); } var authorDto = new AuthorDto(author); var data = (IDictionary <string, object>)authorDto.ShapeData(fields); var links = CreateLinksForAuthor(id, fields); data.Add("links", links); return(Ok(data)); }
public IActionResult GetAuthor(Guid id, [FromQuery] string fields) { Contract.Ensures(Contract.Result <IActionResult>() != null); AuthorDto author = null; if (!_typeHelperService.TypeHasProperties <AuthorDto>(fields)) { return(BadRequest()); } if (!_libraryRepository.AuthorExists(id)) { return(NotFound()); } TryCatchHelper.Execute(() => { var authorFromRepo = _libraryRepository.GetAuthor(id); author = authorFromRepo.Map <AuthorDto>(); }).IfNotNull(ex => { return(StatusCode(500, "An Unhandled fault Happend. Try again later")); }); if (author.IsNotNull()) { var links = CreateLinksForAuthor(id, fields); var linkedResourceToReturn = author.ShapeData(fields) as IDictionary <string, object>; linkedResourceToReturn.Add("links", links); return(Ok(linkedResourceToReturn)); } return(NotFound()); }
public IActionResult GetAuthor(Guid id, [FromQuery] string fields) { if (!typeHelperService.TypeHasProperties <AuthorDto>(fields)) { return(BadRequest()); } Author authorFromRepo = libraryRepository.GetAuthor(id); if (authorFromRepo == null) { return(NotFound()); } AuthorDto author = mapper.Map <AuthorDto>(authorFromRepo); IEnumerable <LinkDto> links = CreateLinksForAuthor(id, fields); IDictionary <string, object> linkedResourceToReturn = author.ShapeData(fields) as IDictionary <string, object>; linkedResourceToReturn.Add("links", links); return(Ok(linkedResourceToReturn)); }
public IActionResult CreateAuthor([FromBody] CreateAuthorDto createAuthorDto) { if (createAuthorDto == null) { return(BadRequest()); } var author = createAuthorDto.ToAuthor(); _libraryRepository.AddAuthor(author); if (!_libraryRepository.Save()) { throw new Exception("Creating an author failed on save."); } var authorDto = new AuthorDto(author); var data = (IDictionary <string, object>)authorDto.ShapeData(null); var links = CreateLinksForAuthor(authorDto.Id, null); data.Add("links", links); return(CreatedAtRoute(nameof(GetAuthor), new { id = author.Id }, data)); }