From 0e06944931a4e597604a2c61f50b8c01e00db4b5 Mon Sep 17 00:00:00 2001 From: Bohdan Date: Sun, 2 Jun 2019 18:24:24 +0300 Subject: [PATCH] bug fix --- .../Models/Manage/LibraryIndexViewModel.cs | 2 + .../Models/Manage/PaginationViewModel.cs | 36 +++ LibraryManager/Controllers/AdminController.cs | 1 + .../Controllers/LibraryController.cs | 14 +- LibraryManager/Views/Library/Index.cshtml | 274 ++++++++++-------- 5 files changed, 211 insertions(+), 116 deletions(-) create mode 100644 LibraryManager.DTO/Models/Manage/PaginationViewModel.cs diff --git a/LibraryManager.DTO/Models/Manage/LibraryIndexViewModel.cs b/LibraryManager.DTO/Models/Manage/LibraryIndexViewModel.cs index 3fd7c6d..067a3bf 100644 --- a/LibraryManager.DTO/Models/Manage/LibraryIndexViewModel.cs +++ b/LibraryManager.DTO/Models/Manage/LibraryIndexViewModel.cs @@ -12,6 +12,8 @@ public class LibraryIndexViewModel public IEnumerable LanguageDTOs { get; set; } + public PaginationViewModel PaginationViewModel { get; set; } + public string SearchCategory { get; set; } public string SearchValue { get; set; } } diff --git a/LibraryManager.DTO/Models/Manage/PaginationViewModel.cs b/LibraryManager.DTO/Models/Manage/PaginationViewModel.cs new file mode 100644 index 0000000..b15b82c --- /dev/null +++ b/LibraryManager.DTO/Models/Manage/PaginationViewModel.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace LibraryManager.DTO.Models.Manage +{ + public class PaginationViewModel + { + + public int PageNumber { get; private set; } + public int TotalPages { get; private set; } + + public PaginationViewModel(int count, int pageNumber, int pageSize) + { + PageNumber = pageNumber; + TotalPages = (int)Math.Ceiling(count / (double)pageSize); + } + + public bool HasPreviousPage + { + get + { + return (PageNumber > 1); + } + } + + public bool HasNextPage + { + get + { + return (PageNumber < TotalPages); + } + } + } +} + \ No newline at end of file diff --git a/LibraryManager/Controllers/AdminController.cs b/LibraryManager/Controllers/AdminController.cs index 4f125b5..a84c882 100644 --- a/LibraryManager/Controllers/AdminController.cs +++ b/LibraryManager/Controllers/AdminController.cs @@ -40,6 +40,7 @@ public AdminController(IBookService bookService, IGenreService genreService, IAd [Authorize(Roles = "Admin")] public IActionResult Index() { + AddNewBookModel addNewBookModel = new AddNewBookModel() { Genres = new SelectList(_genreService.GetAll(), "Id", "GenreName"), diff --git a/LibraryManager/Controllers/LibraryController.cs b/LibraryManager/Controllers/LibraryController.cs index 6ac180a..c4037e6 100644 --- a/LibraryManager/Controllers/LibraryController.cs +++ b/LibraryManager/Controllers/LibraryController.cs @@ -44,17 +44,25 @@ public LibraryController(IBookService bookService, IUserService userService, IGe } - public async Task Index() + public async Task Index(int page = 1, int pageSize = 3) { - var books = _bookService.GetAll(); + var books = _bookService.GetAll().ToList(); var genres = _genreService.GetAll(); InitializeTempData(); + + var count = books.Count; + + var pagination = new PaginationViewModel(count, page, pageSize); + + var bookItems = books.Skip((page - 1) * pageSize).Take(pageSize).ToList(); + var libraryIndexViewModel = new LibraryIndexViewModel { - BookDTOs = books, + BookDTOs = bookItems, GenreDTOs = genres, + PaginationViewModel = pagination }; diff --git a/LibraryManager/Views/Library/Index.cshtml b/LibraryManager/Views/Library/Index.cshtml index 7cfbc26..3917901 100644 --- a/LibraryManager/Views/Library/Index.cshtml +++ b/LibraryManager/Views/Library/Index.cshtml @@ -1,131 +1,179 @@ @using LibraryManager.DTO.Models.Manage - @using Microsoft.AspNetCore.Identity - @using LibraryManager.DAL.Entities - - @inject SignInManager SignInManager - @inject UserManager UserManager - - @model LibraryIndexViewModel - - @{ - ViewData["Title"] = "Index"; - } - @{ - var numberOfGenres = Model.GenreDTOs.Count() <= 0 ? 1 : Model.GenreDTOs.Count(); - var genreIndex = ((int?)TempData["genreIndex"]).Value < 0 - ? numberOfGenres + ((int?)TempData["genreIndex"]).Value % numberOfGenres - : ((int?)TempData["genreIndex"]).Value; - - var displayedGenres = ((int?)TempData["displayedGenres"]).Value >= numberOfGenres - ? numberOfGenres - : 4; - //((int?)TempData["displayedGenres"]).Value; - - TempData.Keep(); - } - - @if (SignInManager.IsSignedIn(User)) - { - - Layout = "~/Views/Shared/_Layout.cshtml"; - - } - @if (User.IsInRole("Admin")) - { - - Layout = "~/Views/Shared/_AdminLayout.cshtml"; - - } - @if (!User.Identity.IsAuthenticated) - { - Layout = "~/Views/Shared/_GuestLayout.cshtml"; - } - -
- -
-
-
-
-
-
-
-
-
- - - - - - -
- - - - - +@using Microsoft.AspNetCore.Identity +@using LibraryManager.DAL.Entities + +@inject SignInManager SignInManager +@inject UserManager UserManager + +@model LibraryIndexViewModel + +@{ + ViewData["Title"] = "Index"; +} +@{ + var numberOfGenres = Model.GenreDTOs.Count() <= 0 ? 1 : Model.GenreDTOs.Count(); + var genreIndex = ((int?)TempData["genreIndex"]).Value < 0 + ? numberOfGenres + ((int?)TempData["genreIndex"]).Value % numberOfGenres + : ((int?)TempData["genreIndex"]).Value; + + var displayedGenres = ((int?)TempData["displayedGenres"]).Value >= numberOfGenres + ? numberOfGenres + : 4; + //((int?)TempData["displayedGenres"]).Value; + + TempData.Keep(); +} + +@if (SignInManager.IsSignedIn(User)) +{ + + Layout = "~/Views/Shared/_Layout.cshtml"; + +} +@if (User.IsInRole("Admin")) +{ + + Layout = "~/Views/Shared/_AdminLayout.cshtml"; + +} +@if (!User.Identity.IsAuthenticated) +{ + Layout = "~/Views/Shared/_GuestLayout.cshtml"; +} + +
+ + +
+
+
+
+
+
+
+
+ + + + + +
+ + + + +
-
- - -
-
-
- -
-
- @for (var i = genreIndex % numberOfGenres; i != (genreIndex + displayedGenres) % (numberOfGenres); i = (i + 1) % numberOfGenres) - { - -
- @Model.GenreDTOs.ElementAt(i).GenreName -
@Model.GenreDTOs.ElementAt(i).GenreName
-
@Model.GenreDTOs.ElementAt(i).NumberOfBooks
-
-
- } -
-
- -
+
+ -
- @foreach (var book in Model.BookDTOs) +
+ +
+
- \ No newline at end of file + @if (Model.PaginationViewModel.HasPreviousPage) + { + + + + + } + @for (int i = 1; i < Model.PaginationViewModel.TotalPages; i++) + { + @if (i == Model.PaginationViewModel.PageNumber) + { + + @i + + } + else + { + + @i + + } + } + + @if (Model.PaginationViewModel.HasNextPage) + { + + + + + } +
+ + + \ No newline at end of file