Created
July 30, 2020 10:10
-
-
Save PradeepLoganathan/7e5c2dd63bc9b761df5dec175aeead7c to your computer and use it in GitHub Desktop.
BooksController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Route("api/[controller]")] | |
| [ApiController] | |
| public class BooksController : ControllerBase | |
| { | |
| private IUnitOfWork _unitOfWork; | |
| public BooksController(IUnitOfWork unitOfWork) | |
| { | |
| _unitOfWork = unitOfWork; | |
| } | |
| // GET: api/<Books> | |
| [HttpGet] | |
| public async Task<IEnumerable<Book>> Get() | |
| { | |
| return await _unitOfWork.Books.GetAll(); | |
| } | |
| [HttpGet] | |
| public IEnumerable<Book> GetByGenre([FromQuery] string Genre) | |
| { | |
| return _unitOfWork.Books.GetBooksByGenre(Genre); | |
| } | |
| // GET api/<Books>/5 | |
| [HttpGet("{id}")] | |
| public async Task<Book> Get(int id) | |
| { | |
| return await _unitOfWork.Books.Get(id); | |
| } | |
| // POST api/<Books> | |
| [HttpPost] | |
| public IActionResult Post() | |
| { | |
| var book = new Book | |
| { | |
| Id = 1, | |
| Genre = "Technology", | |
| Author = "Charles Petzold", | |
| Title = "Programming Windows 5th Edition", | |
| Price = 30, | |
| Publisher = "Microsoft Press" | |
| }; | |
| var Catalog = new Catalogue | |
| { | |
| CatalogueId = 1, | |
| Name = "Programming Books", | |
| Description = "Books on Software development" | |
| }; | |
| _unitOfWork.Books.Add(book); | |
| _unitOfWork.Catalogues.Add(Catalog); | |
| _unitOfWork.Complete(); | |
| return Ok(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment