Spaces:
Sleeping
Sleeping
File size: 2,079 Bytes
87c9973 f53877e 87c9973 f53877e 87c9973 f53877e 87c9973 f53877e 87c9973 f53877e 87c9973 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace LibraryManagement.Backend.Features.Books
{
[ApiController]
[Route("api/books")]
public class BookController : ControllerBase
{
private readonly IBookService _bookService;
public BookController(IBookService bookService)
{
_bookService = bookService;
}
[HttpGet]
//[Authorize]
public async Task<ActionResult<IEnumerable<BookDto>>> GetBooks()
{
var books = await _bookService.GetAllBooksAsync();
return Ok(books);
}
[HttpGet("{id}")]
//[Authorize]
public async Task<ActionResult<BookDto>> GetBook(int id)
{
var book = await _bookService.GetBookByIdAsync(id);
if (book == null) return NotFound();
return Ok(book);
}
[HttpPost]
//[Authorize(Roles = "Librarian")]
public async Task<ActionResult<BookDto>> CreateBook([FromBody] BookCreateRequest request)
{
try
{
var book = await _bookService.CreateBookAsync(request);
return CreatedAtAction(nameof(GetBook), new { id = book.Id }, book);
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
[HttpPut("{id}")]
//[Authorize(Roles = "Librarian")]
public async Task<ActionResult<BookDto>> UpdateBook(int id, [FromBody] BookUpdateRequest request)
{
var updatedBook = await _bookService.UpdateBookAsync(id, request);
if (updatedBook == null) return NotFound();
return Ok(updatedBook);
}
[HttpDelete("{id}")]
//[Authorize(Roles = "Librarian")]
public async Task<IActionResult> DeleteBook(int id)
{
var success = await _bookService.DeleteBookAsync(id);
if (!success) return NotFound();
return NoContent();
}
}
}
|