Spaces:
Sleeping
Sleeping
File size: 2,839 Bytes
4433399 068485b c312181 068485b da13799 068485b 1723ad3 da13799 1723ad3 068485b c312181 da13799 068485b c312181 068485b da13799 068485b c312181 068485b 4433399 | 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | using LibraryManagement.Shared.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
namespace Backend.Features.Borrowings
{
[ApiController]
[Route("api/borrowings")]
public class BorrowingController : ControllerBase
{
private readonly IBorrowingService _borrowingService;
public BorrowingController(IBorrowingService borrowingService)
{
_borrowingService = borrowingService;
}
[HttpPost("borrow")]
[Authorize]
public async Task<ActionResult<BorrowingDto>> BorrowBook([FromBody] BorrowRequest request)
{
try
{
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userIdStr)) return Unauthorized();
var userId = Guid.Parse(userIdStr);
var borrowing = await _borrowingService.BorrowBookAsync(userId, request.BookId);
return Ok(borrowing);
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
[HttpPost("return-request/{id}")]
[Authorize]
public async Task<ActionResult<BorrowingDto>> RequestReturn(Guid id)
{
try
{
var borrowing = await _borrowingService.RequestReturnAsync(id);
return Ok(borrowing);
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
[HttpPost("return/{id}")]
[Authorize(Roles = "Librarian")]
public async Task<ActionResult<BorrowingDto>> ReturnBook(Guid id)
{
try
{
var borrowing = await _borrowingService.ReturnBookAsync(id);
return Ok(borrowing);
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
[HttpGet("me")]
[Authorize]
public async Task<ActionResult<IEnumerable<BorrowingDto>>> GetMyBorrowings()
{
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userIdStr)) return Unauthorized();
var userId = Guid.Parse(userIdStr);
var borrowings = await _borrowingService.GetUserBorrowingsAsync(userId);
return Ok(borrowings);
}
[HttpGet]
[Authorize(Roles = "Librarian")]
public async Task<ActionResult<IEnumerable<BorrowingDto>>> GetAllBorrowings()
{
var borrowings = await _borrowingService.GetAllBorrowingsAsync();
return Ok(borrowings);
}
}
}
|