Spaces:
Sleeping
Sleeping
File size: 4,202 Bytes
b0a0ed0 6f32ae8 b0a0ed0 861f0db b0a0ed0 36144b0 861f0db 36144b0 861f0db 36144b0 b0a0ed0 861f0db 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | using Microsoft.AspNetCore.Mvc;
using Frontend.Services;
using LibraryManagement.Shared.Models;
namespace Frontend.Controllers
{
public class BooksController : Controller
{
private readonly LibraryApiClient _apiClient;
public BooksController(LibraryApiClient apiClient)
{
_apiClient = apiClient;
}
public async Task<IActionResult> Index(int page = 1, int? categoryId = null)
{
const int pageSize = 8;
var allBooks = await _apiClient.GetBooksAsync(categoryId) ?? Enumerable.Empty<BookDto>();
var totalCount = allBooks.Count();
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
page = Math.Max(1, Math.Min(page, Math.Max(1, totalPages)));
var pagedResult = new Frontend.Models.PagedResult<BookDto>
{
Items = allBooks.Skip((page - 1) * pageSize).Take(pageSize),
CurrentPage = page,
TotalPages = totalPages,
TotalCount = totalCount,
PageSize = pageSize
};
ViewBag.Categories = await _apiClient.GetCategoriesWithBooksAsync();
ViewBag.SelectedCategoryId = categoryId;
return View(pagedResult);
}
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,
CategoryIds = book.Categories.Select(c => c.Id).ToList()
});
}
[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));
}
}
}
|