mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 17:51:20 +00:00
78 lines
1.7 KiB
C#
78 lines
1.7 KiB
C#
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<List<Book>> Get()
|
|
{
|
|
return _bookService.Get();
|
|
}
|
|
|
|
[HttpGet("{id:length(24)}", Name = "GetBook")]
|
|
public ActionResult<Book> Get(string id)
|
|
{
|
|
var book = _bookService.Get(id);
|
|
|
|
if (book == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return book;
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult<Book> 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();
|
|
}
|
|
}
|
|
} |