Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using System.Web.Http.Description;
- using MSApi.DAL;
- using MSApi.Models;
- namespace MSApi.Controllers
- {
- public class BooksController : ApiController
- {
- private ApplicationDbContext db = new ApplicationDbContext();
- Synchronizer s = new Synchronizer();
- // GET: api/Books
- [AllowAnonymous]
- public async System.Threading.Tasks.Task<List<Book>> GetBooksAsync()
- {
- await s.RunAsync();
- return BookRepository.GetBooks();
- }
- // GET: api/Books/5
- [AllowAnonymous]
- [ResponseType(typeof(Book))]
- public IHttpActionResult GetBook(int id)
- {
- Book book = db.Books.Find(id);
- if (book == null)
- {
- return NotFound();
- }
- return Ok(book);
- }
- // PUT: api/Books/5
- [Authorize(Roles = "ADMIN")]
- [ResponseType(typeof(void))]
- 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);
- }
- // POST: api/Books
- [Authorize(Roles = "ADMIN")]
- [ResponseType(typeof(Book))]
- 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);
- }
- // DELETE: api/Books/5
- [Authorize(Roles = "ADMIN")]
- [ResponseType(typeof(Book))]
- public IHttpActionResult DeleteBook(int id)
- {
- Book book = db.Books.Find(id);
- if (book == null)
- {
- return NotFound();
- }
- db.Books.Remove(book);
- db.SaveChanges();
- return Ok(book);
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- private bool BookExists(int id)
- {
- return db.Books.Count(e => e.ID == id) > 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement