public async Task<IHttpActionResult> PutBook(int id, Book book) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != book.BookId) { return BadRequest(); } db.Entry(book).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostBook(Book book) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Books.Add(book); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = book.BookId }, book); }
public void PostBookTest() { //Arrange var controller = new BooksController { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; //Act var book = new Book { }; //Assert Assert.IsNotNull(book); Assert.AreEqual(book.BookId, 1, "Returned Id is different to the expected Id."); }