public async Task <ActionResult> AddABook([FromBody] PostBookCreate bookToAdd) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var book = new Book { Title = bookToAdd.Title, Author = bookToAdd.Author, Genre = bookToAdd.Genre, NumberOfPages = bookToAdd.NumberOfPages, InStock = true }; Context.Books.Add(book); // I have no Id! await Context.SaveChangesAsync(); // Suddenly I have an Id! var response = new GetABookResponse { Id = book.Id, Title = book.Title, Author = book.Author, Genre = book.Genre, NumberOfPages = book.NumberOfPages }; return(CreatedAtRoute("books#getabook", new { bookId = response.Id }, response)); }
public async Task <GetABookResponse> AddBook(PostBookCreate bookToAdd) { //var book = new Book //{ // Title = bookToAdd.Title, // Author = bookToAdd.Author, // Genre = bookToAdd.Genre, // NumberOfPages = bookToAdd.NumberOfPages, // InStock = true //}; var book = Mapper.Map <Book>(bookToAdd); Context.Books.Add(book); // I have not Id! await Context.SaveChangesAsync(); // Suddenly I have an Id! // Book -> GetA BookResponse //var response = new GetABookResponse //{ // Id = book.Id, // Title = book.Title, // Author = book.Author, // Genre = book.Genre, // NumberOfPages = book.NumberOfPages //}; var response = Mapper.Map <GetABookResponse>(book); return(response); }
public async Task <ActionResult> AddABook([FromBody] PostBookCreate bookToAdd) { // 3. Add it to the database GetABookResponse response = await Mapper.AddBook(bookToAdd); return(CreatedAtRoute("books#getabook", new { bookId = response.Id }, response)); }
public async Task <ActionResult <GetBookDetailsResponse> > AddABook([FromBody] PostBookCreate bookToAdd) { var response = await _bookCommands.AddBook(bookToAdd); // return a 201, with location header, with a copy of what they'd get from that location return(CreatedAtRoute("books#getbyid", new { bookId = response.Id }, response)); }
public async Task <GetABookResponse> AddBook(PostBookCreate bookToAdd) { var book = Mapper.Map <Book>(bookToAdd); Context.Books.Add(book); await Context.SaveChangesAsync(); var response = Mapper.Map <GetABookResponse>(book); return(response); }
public async Task <GetABookResponse> AddABook(PostBookCreate bookToAdd) { // PostBookCreate -> Book var book = Mapper.Map <Book>(bookToAdd); Context.Books.Add(book); // I have no Id! await Context.SaveChangesAsync(); // Suddenly I have an ID! // Book -> GetABookResponse var response = Mapper.Map <GetABookResponse>(book); return(response); }
public async Task <GetBookDetailsResponse> AddBook(PostBookCreate bookToAdd) { // add it to the db context. (we need to make it a book) var book = _mapper.Map <Book>(bookToAdd); // PostBookCreate -> Book _context.Books.Add(book); // book.Id = 0; // save the changes to the database. await _context.SaveChangesAsync(); // book.Id = 8; var response = _mapper.Map <GetBookDetailsResponse>(book); // Book -> GetBookDetailsResponse return(response); }
public async Task <GetBookDetailsResponse> AddBook(PostBookCreate bookToAdd) { // add it to the db context. (we need to make it a book) var book = _mapper.Map <Book>(bookToAdd); // PostBookCreate -> Book _context.Books.Add(book); // book.Id = 0; // save the changes to the database. await _context.SaveChangesAsync(); // book.Id = 8; var response = _mapper.Map <GetBookDetailsResponse>(book); // Book -> GetBookDetailsResponse // return a 201, with location header, with a copy of what they'd get from that location return(response); }
public async Task <ActionResult> AddABook([FromBody] PostBookCreate bookToAdd) { // X 1. Need a model for the post [FromBody] // 2. Validate the data coming in. // - declarative Validation // - Programmatic validation // - Return a 400 (Bad Request) //if(!ModelState.IsValid) //{ // return BadRequest(ModelState); //} // 3. Add it to the database GetABookResponse response = await Mapper.AddBook(bookToAdd); return(CreatedAtRoute("books#getabook", new { bookId = response.Id }, response)); }
public async Task <ActionResult <GetBookDetailsResponse> > AddBook([FromBody] PostBookCreate bookToAdd) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else { // add it to the db context. (we need to make it a book) var book = _mapper.Map <Book>(bookToAdd); _context.Books.Add(book); // save the changes to the database. await _context.SaveChangesAsync(); var response = _mapper.Map <GetBookDetailsResponse>(book); // returna 201, with location header, with a cogpy of what they'd get from that location return(CreatedAtRoute("books#getbyid", new { bookId = response.Id }, response)); } }
public async Task <ActionResult> AddABook([FromBody] PostBookCreate bookToAdd) { // X 1. Need a model for the post [FromBody] // 2. Validate the data coming in. // - declarative Validation // - Programmatic validation // - Return a 400 (Bad Request) if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // 3. Add it to the database var book = new Book { Title = bookToAdd.Title, Author = bookToAdd.Author, Genre = bookToAdd.Genre, NumberOfPages = bookToAdd.NumberOfPages, InStock = true }; Context.Books.Add(book); // I have no Id! await Context.SaveChangesAsync(); // Suddenly I have an ID! // 4. Return (if the post is to a collection) // - a 201 Created status code. // - Add a location header to the response. Location: http://localhost:1337/books/3 // - Add an entity to the response that is EXACTLY what they'd get if they followed // - the location header. var response = new GetABookResponse { Id = book.Id, Title = book.Title, Author = book.Author, Genre = book.Genre, NumberOfPages = book.NumberOfPages }; return(CreatedAtRoute("books#getabook", new { bookId = response.Id }, response)); }
public async Task <ActionResult <GetBookDetailsResponse> > AddABook([FromBody] PostBookCreate bookToAdd) { GetBookDetailsResponse response = await _bookCommands.AddBook(bookToAdd); return(CreatedAtRoute("books#getbyid", new { bookId = response.Id }, response)); }