public async Task<IHttpActionResult> PutBook(int id, Book book) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != book.Id) { 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(); var refreshedBook = db.Books.Include("Author").FirstOrDefault(e => e.Id == book.Id); return CreatedAtRoute("DefaultApi", new { id = refreshedBook.Id }, refreshedBook); }