Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 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.Web.Http;
  10. using System.Web.Http.Description;
  11. using MSApi.DAL;
  12. using MSApi.Models;
  13.  
  14. namespace MSApi.Controllers
  15. {
  16.     public class BooksController : ApiController
  17.     {
  18.         private ApplicationDbContext db = new ApplicationDbContext();
  19.         Synchronizer s = new Synchronizer();
  20.  
  21.         // GET: api/Books
  22.         [AllowAnonymous]
  23.         public async System.Threading.Tasks.Task<List<Book>> GetBooksAsync()
  24.         {            
  25.             await s.RunAsync();
  26.             return BookRepository.GetBooks();
  27.         }
  28.  
  29.         // GET: api/Books/5
  30.         [AllowAnonymous]
  31.         [ResponseType(typeof(Book))]
  32.         public IHttpActionResult GetBook(int id)
  33.         {
  34.             Book book = db.Books.Find(id);
  35.             if (book == null)
  36.             {
  37.                 return NotFound();
  38.             }
  39.            
  40.             return Ok(book);
  41.         }
  42.  
  43.         // PUT: api/Books/5
  44.         [Authorize(Roles = "ADMIN")]
  45.         [ResponseType(typeof(void))]
  46.         public IHttpActionResult PutBook(int id, Book book)
  47.         {
  48.             if (!ModelState.IsValid)
  49.             {
  50.                 return BadRequest(ModelState);
  51.             }
  52.  
  53.             if (id != book.ID)
  54.             {
  55.                 return BadRequest();
  56.             }
  57.  
  58.             db.Entry(book).State = EntityState.Modified;
  59.  
  60.             try
  61.             {
  62.                 db.SaveChanges();
  63.             }
  64.             catch (DbUpdateConcurrencyException)
  65.             {
  66.                 if (!BookExists(id))
  67.                 {
  68.                     return NotFound();
  69.                 }
  70.                 else
  71.                 {
  72.                     throw;
  73.                 }
  74.             }
  75.  
  76.             return StatusCode(HttpStatusCode.NoContent);
  77.         }
  78.  
  79.         // POST: api/Books
  80.         [Authorize(Roles = "ADMIN")]
  81.         [ResponseType(typeof(Book))]
  82.         public IHttpActionResult PostBook(Book book)
  83.         {
  84.             if (!ModelState.IsValid)
  85.             {
  86.                 return BadRequest(ModelState);
  87.             }
  88.  
  89.             db.Books.Add(book);
  90.             db.SaveChanges();
  91.  
  92.             return CreatedAtRoute("DefaultApi", new { id = book.ID }, book);
  93.         }
  94.  
  95.         // DELETE: api/Books/5
  96.         [Authorize(Roles = "ADMIN")]
  97.         [ResponseType(typeof(Book))]
  98.         public IHttpActionResult DeleteBook(int id)
  99.         {
  100.             Book book = db.Books.Find(id);
  101.             if (book == null)
  102.             {
  103.                 return NotFound();
  104.             }
  105.  
  106.             db.Books.Remove(book);
  107.             db.SaveChanges();
  108.  
  109.             return Ok(book);
  110.         }
  111.  
  112.         protected override void Dispose(bool disposing)
  113.         {
  114.             if (disposing)
  115.             {
  116.                 db.Dispose();
  117.             }
  118.             base.Dispose(disposing);
  119.         }
  120.  
  121.         private bool BookExists(int id)
  122.         {
  123.             return db.Books.Count(e => e.ID == id) > 0;
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement