Exemplo n.º 1
0
        // PUT api/Books/5
        public 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
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Exemplo n.º 2
0
        public IHttpActionResult PostBook(Book book)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Books.Add(book);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = book.Id }, book);
        }
Exemplo n.º 3
0
        public void PostBook()
        {
            // Arrange
            BooksController controller = new BooksController();
            var book = new Book() { Title = "Hello Test", ISBN = "12234", Pages = 3 };

            // Act
            var result = (CreatedAtRouteNegotiatedContentResult<Book>)controller.PostBook(book);

            // Assert
            Assert.AreEqual(result.Content.ISBN, "12234");
            Assert.AreEqual(result.Content.Title, "Hello Test");
            Assert.AreEqual(result.Content.Pages, 3);
        }