using System.Collections.Generic; using MyCore.Models; using MyCore.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; namespace MyCore.Controllers { [Authorize(Roles = "Admin")] [Route("api/books")] [ApiController] public class BooksController : ControllerBase { private readonly BookService _bookService; public BooksController(BookService bookService) { _bookService = bookService; } [HttpGet] public ActionResult> Get() { return _bookService.Get(); } [HttpGet("{id:length(24)}", Name = "GetBook")] public ActionResult Get(string id) { var book = _bookService.Get(id); if (book == null) { return NotFound(); } return book; } [HttpPost] public ActionResult Create(Book book) { _bookService.Create(book); return CreatedAtRoute("GetBook", new { id = book.Id.ToString() }, book); } [HttpPut("{id:length(24)}")] public IActionResult Update(string id, Book bookIn) { var book = _bookService.Get(id); if (book == null) { return NotFound(); } _bookService.Update(id, bookIn); return NoContent(); } [HttpDelete("{id:length(24)}")] public IActionResult Delete(string id) { var book = _bookService.Get(id); if (book == null) { return NotFound(); } _bookService.Remove(book.Id); return NoContent(); } } }