示例#1
0
        public HttpResponseMessage Put(Book book)
        {
            if (book == null)
                return Request.CreateResponse(HttpStatusCode.BadRequest);

            //if (!_books.Contains(book))
            if(!_books.Any(b => b.Code == book.Code))
                return Request.CreateResponse(HttpStatusCode.BadRequest);

            try
            {
                //update the given book
                var bookToBeUpdated = _books.Where(b => b.Code== book.Code).FirstOrDefault();
                _books.Remove(bookToBeUpdated);
                _books.Add(book);

                return Request.CreateResponse(HttpStatusCode.OK, _books.ToList());
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, "Error to update the given book in the List");
            }
        }
示例#2
0
        public HttpResponseMessage Post(Book book)
        {
            if (book == null)
                return Request.CreateResponse(HttpStatusCode.BadRequest); // if you want a specific error object, just create a class and return here.

            try
            {
                _books.Add(book);

                return Request.CreateResponse(HttpStatusCode.OK, _books.ToList()); //returning all the books
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, "Error to add a new Book in the List");

            }
        }