Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Infrastructure;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Threading.Tasks;
  10. using System.Web.Http;
  11. using System.Web.Http.Description;
  12. using BookService.Models;
  13. namespace BookService.Controllers
  14. {
  15. public class BooksController : ApiController
  16. {
  17. private BookServiceContext db = new BookServiceContext();
  18.  
  19. // GET: api/Books
  20. public IQueryable<BookDTO> GetBooks()
  21. {
  22. var books = from b in db.Books
  23. select new BookDTO()
  24. {
  25. Id = b.Id,
  26. Title = b.Title,
  27. AuthorName = b.Author.Name
  28. };
  29.  
  30. return books;
  31. }
  32.  
  33. // GET: api/Books/5
  34. [ResponseType(typeof(BookDetailDTO))]
  35. public async Task<IHttpActionResult> GetBook(int id)
  36. {
  37. var book = await db.Books.Include(b => b.Author).Select(b =>
  38. new BookDetailDTO()
  39. {
  40. Id = b.Id,
  41. Title = b.Title,
  42. Year = b.Year,
  43. Price = b.Price,
  44. AuthorName = b.Author.Name,
  45. Genre = b.Genre
  46. }).SingleOrDefaultAsync(b => b.Id == id);
  47. if (book == null)
  48. {
  49. return NotFound();
  50. }
  51.  
  52. return Ok(book);
  53. }
  54.  
  55. // PUT: api/Books/5
  56. [ResponseType(typeof(void))]
  57. public async Task<IHttpActionResult> PutBook(int id, Book book)
  58. {
  59. if (!ModelState.IsValid)
  60. {
  61. return BadRequest(ModelState);
  62. }
  63.  
  64. if (id != book.Id)
  65. {
  66. return BadRequest();
  67. }
  68.  
  69. db.Entry(book).State = EntityState.Modified;
  70.  
  71. try
  72. {
  73. await db.SaveChangesAsync();
  74. }
  75. catch (DbUpdateConcurrencyException)
  76. {
  77. if (!BookExists(id))
  78. {
  79. return NotFound();
  80. }
  81. else
  82. {
  83. throw;
  84. }
  85. }
  86.  
  87. return StatusCode(HttpStatusCode.NoContent);
  88. }
  89.  
  90. // POST: api/Books
  91. [ResponseType(typeof(Book))]
  92. public async Task<IHttpActionResult> PostBook(Book book)
  93. {
  94. if (!ModelState.IsValid)
  95. {
  96. return BadRequest(ModelState);
  97. }
  98.  
  99. db.Books.Add(book);
  100. await db.SaveChangesAsync();
  101.  
  102. // New code:
  103. // Load author name
  104. db.Entry(book).Reference(x => x.Author).Load();
  105.  
  106. var dto = new BookDTO()
  107. {
  108. Id = book.Id,
  109. Title = book.Title,
  110. AuthorName = book.Author.Name
  111. };
  112.  
  113. return CreatedAtRoute("DefaultApi", new { id = book.Id }, dto);
  114. }
  115.  
  116. // DELETE: api/Books/5
  117. [ResponseType(typeof(Book))]
  118. public async Task<IHttpActionResult> DeleteBook(int id)
  119. {
  120. Book book = await db.Books.FindAsync(id);
  121. if (book == null)
  122. {
  123. return NotFound();
  124. }
  125.  
  126. db.Books.Remove(book);
  127. await db.SaveChangesAsync();
  128.  
  129. return Ok(book);
  130. }
  131.  
  132. protected override void Dispose(bool disposing)
  133. {
  134. if (disposing)
  135. {
  136. db.Dispose();
  137. }
  138. base.Dispose(disposing);
  139. }
  140.  
  141. private bool BookExists(int id)
  142. {
  143. return db.Books.Count(e => e.Id == id) > 0;
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement