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)); }
public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author) { if (author == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var authorEntity = Mapper.Map <Author>(author); _libraryRepository.AddAuthor(authorEntity); if (!_libraryRepository.Save()) { throw new Exception("Creating an author failed on save."); } var authorToReturn = Mapper.Map <AuthorDto>(authorEntity); return(CreatedAtAction("GetAuthor", new { id = authorToReturn.Id }, authorToReturn)); }
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)); }
public void CreateAuthor(AuthorForCreationDto authorForCreationDto, string authorId) { string sql = @"insert into authors( au_id, au_lname , au_fname , phone , address , city , state , zip) values( @au_id, @au_lname , @au_fname , @phone , @address , @city , @state , @zip )"; var parameters = new DynamicParameters(); //TODO: Remove unnecessary params parameters.Add("@au_id", authorId, DbType.String, ParameterDirection.Input, authorId.Length); parameters.Add("@au_fname", authorForCreationDto.LastName, DbType.String, ParameterDirection.Input, authorForCreationDto.LastName.Length); parameters.Add("@au_lname", authorForCreationDto.FirstName, DbType.String, ParameterDirection.Input, authorForCreationDto.FirstName.Length); parameters.Add("@phone", authorForCreationDto.Phone, DbType.String, ParameterDirection.Input, authorForCreationDto.Phone.Length); parameters.Add("@address", authorForCreationDto.Address, DbType.String, ParameterDirection.Input, authorForCreationDto.Address.Length); parameters.Add("@city", authorForCreationDto.City, DbType.String, ParameterDirection.Input, authorForCreationDto.City.Length); parameters.Add("@state", authorForCreationDto.State, DbType.String, ParameterDirection.Input, authorForCreationDto.State.Length); parameters.Add("@zip", authorForCreationDto.Zip, DbType.String, ParameterDirection.Input, authorForCreationDto.Zip.Length); _repository.ModifyDatabase(sql, parameters); }
public ActionResult <AuthorDto> CreateAuthor(AuthorForCreationDto author) { if (author == null) { return(BadRequest()); } 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 CreatedAtRoute("GetAuthor", new { authorId = authorToReturn.Id }, authorToReturn); return(CreatedAtRoute("GetAuthor", new { authorId = linkedResourceToReturn["Id"] }, linkedResourceToReturn)); }
public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author) { //when the client messes up if (author == null) { return(BadRequest()); } //need to create a new mapping for creation dto to entity var authorEntity = Mapper.Map <Author>(author); // add to our db context using our database repository service _libraryRepository.AddAuthor(authorEntity); //call save, if it fails return 500 server error if (!_libraryRepository.Save()) { throw new Exception("Creating an author failed on save."); // return StatusCode(500, "A problem happened with handling your request."); } // map the entity to a DTO so we can show it var authorToReturn = Mapper.Map <AuthorDto>(authorEntity); // return 201 created response with Method name, anonymous object containing the Id, and the author to return object. return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn)); }
public IActionResult AddAuthor([FromBody] AuthorForCreationDto author) { if (author == null) { return(BadRequest()); } var authorToCreate = AutoMapper.Mapper.Map <Author>(author); _libraryRepository.AddAuthor(authorToCreate); if (!_libraryRepository.Save()) { throw new Exception("An unexpected error occurred while adding an actor"); //return BadRequest(500, "An unexpected error occurred while handling your request"); } var authorToReturn = AutoMapper.Mapper.Map <AuthorDto>(authorToCreate); 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)); }
[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 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 request) { if (request == null) { return(BadRequest()); } Author authorEntity = Mapper.Map <Author>(request); 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)); }
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); }
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)); }
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"); } 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 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 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)); }
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)); }
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. }
public async Task <IActionResult> CreateAuthor([FromBody] AuthorForCreationDto author) { if (author == null) { return(BadRequest()); } try { var authorEntity = Mapper.Map <Author>(author); _libraryRepository.AddAuthor(authorEntity); if (!_libraryRepository.Save()) { throw new Exception("Creating an author failed on 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 { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn)); } catch (Exception ex) { await _logger.LogCustomExceptionAsync(ex, null); return(RedirectToAction("Error", "Home")); } }
public IActionResult UpdateAuthor(int id, [FromBody] AuthorForCreationDto author) { if (author == null) { return(BadRequest()); } if (!_libraryRepository.AuthorExists(id)) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var authorEntity = _libraryRepository.GetAuthor(id, false); if (authorEntity == null) { return(NotFound()); } Mapper.Map(author, authorEntity); if (!_libraryRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } return(NoContent()); }
public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author) { if (author.IsNull()) { return(BadRequest()); } var authorEntity = author.Map <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)); }
public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author) { if (author == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var finalAuthor = Mapper.Map <Entities.Author>(author); if (_libraryRepository.AuthorExists(finalAuthor)) { return(StatusCode(409, "Author already exists")); } _libraryRepository.AddAuthor(finalAuthor); if (!_libraryRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdAuthorToReturn = Mapper.Map <AuthorDto>(finalAuthor); return(CreatedAtRoute("GetAuthor", new { id = createdAuthorToReturn.Id, includeBooks = false }, createdAuthorToReturn)); }
public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author) { if (author == null) { return(BadRequest()); } var authorEntity = Mapper.Map <Author>(author); _libRepository.AddAuthor(authorEntity); if (!_libRepository.Save()) { throw new Exception("Creating an author failed on save."); //Já estou a tratar globalmente //return StatusCode(500, "A problem happened while handling your request. Please try again later."); } var authorToReturn = Mapper.Map <AuthorDto>(authorEntity); var links = CreateLinksForAuthor(authorToReturn.Id, null); //transforma o DTO num expando object 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 author, [FromHeader(Name = "Accept")] string mediaType) { if (author == null) { return(this.BadRequest()); } var authorEntity = Mapper.Map <Author>(author); this.libraryRepository.AddAuthor(authorEntity); if (!this.libraryRepository.Save()) { throw new LibraryException("Creating an author failed on save."); } else { var authorToReturn = Mapper.Map <AuthorDto>(authorEntity); if (mediaType == "application/vnd.nagarro.hateoas+json") { var links = this.CreateLinksForAuthor(authorToReturn.Id, null); var linkedResourceToReturn = authorToReturn.ShapeData(null) as IDictionary <string, object>; linkedResourceToReturn.Add("links", links); return(this.CreatedAtRoute("GetAuthor", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn)); } else { var resourceToReturn = authorToReturn.ShapeData(null) as IDictionary <string, object>; return(this.CreatedAtRoute("GetAuthor", new { id = resourceToReturn["Id"] }, resourceToReturn)); } } }
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 CreateAuthor([FromBody] AuthorForCreationDto author) { if (author == null) { return(BadRequest()); } var authorEntity = Mapper.Map <Author>(author); _libraryRepository.AddAuthor(authorEntity); if (!_libraryRepository.Save()) { // lepsi je returnout exception - je to vykonove narocnejsi, ale kodove lepsi 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); // nereturnujeme objekt autora, ale zavolame metodu return(CreatedAtRoute("GetAuthor", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn)); }
private void CreateAuthorHelper(out AuthorForCreationDto model, out string authorId) { authorId = System.Guid.NewGuid().ToString(); string bookId = Guid.NewGuid().ToString(); model = ObjectMocks.GetAuthorForCreation(authorId, bookId); _auRepo.CreateAuthor(model, authorId); }
public ActionResult <AuthorDto> CreateAuthor(AuthorForCreationDto authorForCreationDto) { var author = _mapper.Map <Author>(authorForCreationDto); _courseLibraryRepository.AddAuthor(author); _courseLibraryRepository.Save(); var authorToReturn = _mapper.Map <AuthorDto>(author); return(CreatedAtRoute("GetAuthor", new { authorId = authorToReturn.Id }, authorToReturn)); }
public ActionResult <AuthorDto> CreateAuthor(AuthorForCreationDto author) { var authorEntity = _mapper.Map <Author>(author); _libraryRepository.AddAuthor(authorEntity); _libraryRepository.Save(); var authorToReturn = _mapper.Map <AuthorDto>(authorEntity); return(CreatedAtAction(nameof(GetAuthor), new { authorId = authorToReturn.Id }, authorToReturn)); }
public static Author Map(this AuthorForCreationDto author) { return(new Author { Books = author.Books.Map().ToList(), DateOfBirth = author.DateOfBirth, FirstName = author.FirstName, LastName = author.LastName, Genre = author.Genre }); }
public IActionResult CreateAuthor([FromBody] AuthorForCreationDto authorForCreationDto) { var author = _mapper.Map <Author>(authorForCreationDto); _courseLibraryRepository.AddAuthor(author); _courseLibraryRepository.Save(); var authorDto = _mapper.Map <AuthorDto>(author); return(CreatedAtRoute(nameof(GetAuthor), new { authorId = authorDto.Id }, authorDto)); }