Exemplo n.º 1
0
 public HttpResponseMessage Post(BookDetails book)
 {
     if ((this.ModelState.IsValid) && (book != null))
     {
         BookDetails newBook = this.repository.CreateBook(book);
         if (newBook != null)
         {
             var httpResponse = Request.CreateResponse<BookDetails>(HttpStatusCode.Created, newBook);
             string uri = Url.Link("DefaultApi", new { id = newBook.ID });
             httpResponse.Headers.Location = new Uri(uri);
             return httpResponse;
         }
     }
     return Request.CreateResponse(HttpStatusCode.BadRequest);
 }
Exemplo n.º 2
0
 public HttpResponseMessage Put(string id, BookDetails book)
 {
     if((this.ModelState.IsValid) && (book != null) && (book.ID.Equals(id)))
     {
         BookDetails modifiedBook = this.repository.UpdateBook(id, book);
         if(modifiedBook != null)
         {
             return Request.CreateResponse<BookDetails>(HttpStatusCode.OK, modifiedBook);
         }
         else
         {
             return Request.CreateResponse(HttpStatusCode.NotFound);
         }
     }
     return Request.CreateResponse(HttpStatusCode.BadRequest);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Method to update an existing book in the catalog.
        /// Defines the implementation of the PUT method.
        /// </summary>
        public BookDetails UpdateBook(string id, BookDetails book)
        {
            try
            {
                // Retrieve a specific book from the catalog.
                XElement updateBook = xmlDocument.XPathSelectElement(String.Format("catalog/book[@id='{0}']", id));

                // Verify that the book exists.
                if(updateBook != null)
                {
                    // Create elements for each of the book's data items.
                    XElement[] bookInfo = FormatBookData(book);

                    // Add the element to the book element.
                    updateBook.ReplaceNodes(bookInfo);

                    // Save the XML document.
                    xmlDocument.Save(xmlFilename);

                    // Return an object for the updated book.
                    return this.ReadBook(id);
                }
            }
            catch (Exception ex)
            {
                // Rethrow the exception.
                throw ex;
            }
            // Return null to signify failure.
            return null;
        }
Exemplo n.º 4
0
 /// <summary>
 /// Populates a book BookDetails class with the data for a book.
 /// </summary>
 private XElement[] FormatBookData(BookDetails book)
 {
     XElement[] bookInfo =
     {
         new XElement("author", book.Author),
         new XElement("title", book.Title),
         new XElement("genre", book.Genre),
         new XElement("price", book.Price.ToString()),
         new XElement("publish_date", book.PublishDate.ToString()),
         new XElement("description", book.Description)
     };
     return bookInfo;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Method to add a new book to the catalog.
        /// Defines the implementation of the POST method.
        /// </summary>
        public BookDetails CreateBook(BookDetails book)
        {
            try
            {
                // Retrieve the book with the highest ID from the catalog.
                var highestBook = (
                    from bookNode in xmlDocument.Elements("catalog").Elements("book")
                    orderby bookNode.Attribute("id").Value descending
                    select bookNode).Take(1);

                // Extract the ID from the book date.
                string highestID = highestBook.Attributes("id").First().Value;

                // Create an ID for the new book.
                string newID = "bk" + (Convert.ToInt32(highestID.Substring(2)) + 1).ToString();

                // Verify that this book ID does not currently exit.
                if(this.ReadBook(newID) == null)
                {
                    // Retrieve the parent element for the book catalog.
                    XElement bookCatalogRoot = xmlDocument.Elements("catalog").Single();

                    // Create a new book element.
                    XElement newBook = new XElement("book", new XAttribute("id", newID));

                    // Create elements for each of the book's data items.
                    XElement[] bookInfo = FormatBookData(book);

                    // Add the element to the book element.
                    newBook.ReplaceNodes(bookInfo);

                    // Append the new book to the XML document.
                    bookCatalogRoot.Add(newBook);

                    // Save the XML document.
                    xmlDocument.Save(xmlFilename);

                    // Return an object for the newly-added book.
                    return this.ReadBook(newID);
                }
            }
            catch (Exception ex)
            {
                // Rethrow the exception
                throw ex;
            }
            // Return null to signify failure.
            return null;
        }