File size: 3,407 Bytes
244fdfb
6f32ae8
c312181
 
 
b0a0ed0
 
 
 
 
 
 
 
 
 
 
 
 
 
c312181
 
 
 
1723ad3
da13799
1723ad3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244fdfb
 
 
 
 
 
 
 
 
1723ad3
 
c312181
 
 
 
 
b0a0ed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c312181
da13799
b0a0ed0
 
 
 
 
 
 
 
 
 
c312181
 
 
 
b0a0ed0
 
25f925e
 
 
 
 
 
 
 
 
 
 
 
b0a0ed0
 
6f32ae8
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Frontend.Models;
using LibraryManagement.Shared.Models;
using Frontend.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Frontend.Controllers
{
    public class BorrowingsController : Controller
    {
        private readonly LibraryApiClient _apiClient;

        public BorrowingsController(LibraryApiClient apiClient)
        {
            _apiClient = apiClient;
        }

        public async Task<IActionResult> Index()
        {
            var borrowings = await _apiClient.GetMyBorrowingsAsync();
            return View(borrowings);
        }

        [HttpPost]
        public async Task<IActionResult> RequestReturn(Guid id)
        {
            var success = await _apiClient.RequestReturnAsync(id);
            if (success)
            {
                TempData["Success"] = "Return request sent! Awaiting librarian approval.";
            }
            else
            {
                TempData["Error"] = "Failed to request return.";
            }
            return RedirectToAction(nameof(Index));
        }

        [HttpGet]
        public async Task<IActionResult> Rewards()
        {
            var rewards = await _apiClient.GetActiveRewardsAsync();
            var account = await _apiClient.GetMyLoyaltyAccountAsync();
            
            var viewModel = new RewardsViewModel
            {
                Rewards = rewards,
                Account = account
            };
            
            return View(viewModel);
        }

        [Authorize(Roles = "Librarian")]
        public async Task<IActionResult> Manage()
        {
            var allBorrowings = await _apiClient.GetAllBorrowingsAsync();
            return View(allBorrowings);
        }

        [HttpPost]
        public async Task<IActionResult> Borrow(int bookId)
        {
            var request = new BorrowRequest { BookId = bookId };
            var result = await _apiClient.BorrowBookAsync(request);

            if (result != null)
            {
                TempData["Success"] = "Book borrowed successfully!";
                return RedirectToAction(nameof(Index));
            }

            TempData["Error"] = "Failed to borrow book. Check your subscription or book availability.";
            return RedirectToAction("Details", "Books", new { id = bookId });
        }

        [HttpPost]
        [Authorize(Roles = "Librarian")]
        public async Task<IActionResult> Return(Guid id, bool fromManage = false)
        {
            var result = await _apiClient.ReturnBookAsync(id);
            if (result != null)
            {
                TempData["Success"] = "Book returned successfully!";
            }
            else
            {
                TempData["Error"] = "Failed to process return.";
            }
            
            if (fromManage)
                return RedirectToAction(nameof(Manage));
            
            return RedirectToAction(nameof(Index));
        }
        [HttpPost]
        public async Task<IActionResult> Redeem(string rewardId)
        {
            var (success, message) = await _apiClient.ClaimRewardAsync(rewardId, "Redeemed via Library Rewards Page");
            
            if (success)
            {
                return Json(new { success = true, message });
            }
            
            return Json(new { success = false, message });
        }
    }
}