File size: 3,327 Bytes
b0a0ed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using Microsoft.AspNetCore.Mvc;
using Frontend.Services;
using Frontend.Models.Dtos;

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

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

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

        public async Task<IActionResult> Details(int id)
        {
            var book = await _apiClient.GetBookAsync(id);
            if (book == null) return NotFound();
            return View(book);
        }

        [HttpGet]
        public async Task<IActionResult> Create()
        {
            ViewBag.Categories = await _apiClient.GetCategoriesAsync();
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(BookCreateRequest request)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Categories = await _apiClient.GetCategoriesAsync();
                return View(request);
            }

            var result = await _apiClient.CreateBookAsync(request);
            if (result != null)
            {
                TempData["Success"] = "Book added successfully!";
                return RedirectToAction(nameof(Index));
            }

            ModelState.AddModelError("", "Failed to create book.");
            ViewBag.Categories = await _apiClient.GetCategoriesAsync();
            return View(request);
        }

        [HttpGet]
        public async Task<IActionResult> Edit(int id)
        {
            var book = await _apiClient.GetBookAsync(id);
            if (book == null) return NotFound();

            ViewBag.Categories = await _apiClient.GetCategoriesAsync();
            return View(new BookUpdateRequest 
            { 
                Title = book.Title, 
                Author = book.Author, 
                Description = book.Description,
                TotalCopies = book.TotalCopies
            });
        }

        [HttpPost]
        public async Task<IActionResult> Edit(int id, BookUpdateRequest request)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Categories = await _apiClient.GetCategoriesAsync();
                return View(request);
            }

            var result = await _apiClient.UpdateBookAsync(id, request);
            if (result != null)
            {
                TempData["Success"] = "Book updated successfully!";
                return RedirectToAction(nameof(Index));
            }

            ModelState.AddModelError("", "Failed to update book.");
            ViewBag.Categories = await _apiClient.GetCategoriesAsync();
            return View(request);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            var success = await _apiClient.DeleteBookAsync(id);
            if (success)
            {
                TempData["Success"] = "Book removed from catalog.";
            }
            else
            {
                TempData["Error"] = "Failed to delete book.";
            }
            return RedirectToAction(nameof(Index));
        }
    }
}