Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions LibraryManager.DTO/Models/Manage/LibraryIndexViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class LibraryIndexViewModel

public IEnumerable<LanguageDTO> LanguageDTOs { get; set; }

public PaginationViewModel PaginationViewModel { get; set; }

public string SearchCategory { get; set; }
public string SearchValue { get; set; }
}
Expand Down
36 changes: 36 additions & 0 deletions LibraryManager.DTO/Models/Manage/PaginationViewModel.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}

1 change: 1 addition & 0 deletions LibraryManager/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
14 changes: 11 additions & 3 deletions LibraryManager/Controllers/LibraryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,25 @@ public LibraryController(IBookService bookService, IUserService userService, IGe

}

public async Task<ActionResult> Index()
public async Task<ActionResult> 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
};


Expand Down
274 changes: 161 additions & 113 deletions LibraryManager/Views/Library/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,131 +1,179 @@
@using LibraryManager.DTO.Models.Manage
@using Microsoft.AspNetCore.Identity
@using LibraryManager.DAL.Entities

@inject SignInManager<User> SignInManager
@inject UserManager<User> UserManager

@model LibraryIndexViewModel
<script src="~/js/site.js"></script>
@{
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";
}

<hr />

<form method="post" asp-controller="Search" asp-action="SearchBook">
<section class="search-sec">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<div class="input-group">
<div class="input-group-btn search-panel">

<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" asp-for="SearchCategory">
<span a id="search_concept">Filter by</span> <span class="caret"></span>
</button>

<ul class="dropdown-menu" role="menu">
<li><a href="#contains" value="Title">Title</a></li>
<li><a href="#its_equal" value="Author">Author</a></li>
<li><a href="#its_language" value="Language">Language</a></li>
<li><a href="#its_year" value="Year">Year</a></li>
</ul>
<input asp-for="SearchCategory" type="hidden" id="cater" />
<input type="hidden" name="search_param" id="search_param">
</div>

<input type="text" asp-for="SearchValue" class="form-control" placeholder="Search book...">
<span class="input-group-btn">
<button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button>
</span>
@using Microsoft.AspNetCore.Identity
@using LibraryManager.DAL.Entities

@inject SignInManager<User> SignInManager
@inject UserManager<User> UserManager

@model LibraryIndexViewModel
<script src="~/js/site.js"></script>
@{
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";
}

<hr />

<form method="post" asp-controller="Search" asp-action="SearchBook">
<section class="search-sec">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<div class="input-group">
<div class="input-group-btn search-panel">

<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" asp-for="SearchCategory">
<span a id="search_concept">Filter by</span> <span class="caret"></span>
</button>

<ul class="dropdown-menu" role="menu">
<li><a href="#contains" value="Title">Title</a></li>
<li><a href="#its_equal" value="Author">Author</a></li>
<li><a href="#its_language" value="Language">Language</a></li>
<li><a href="#its_year" value="Year">Year</a></li>
</ul>
<input asp-for="SearchCategory" type="hidden" id="cater" />
<input type="hidden" name="search_param" id="search_param">
</div>

<input type="text" asp-for="SearchValue" class="form-control" placeholder="Search book...">
<span class="input-group-btn">
<button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button>
</span>
</div>
</div>
</div>
</div>
</div>
</section>
</form>

<div class="content">
<div id="genreSection" class="container text-center">
<div class="col-sm-2 alert alert-dark">
<a asp-controller="Library" asp-action="DecreaseValue" class="btn a-item">&#8249;</a>
</div>
<div class="col-sm-8">
@for (var i = genreIndex % numberOfGenres; i != (genreIndex + displayedGenres) % (numberOfGenres); i = (i + 1) % numberOfGenres)
{
<a asp-controller="Library" asp-action="OpenByGenre" asp-route-id="@Model.GenreDTOs.ElementAt(i).Id" class="a-item">
<div class="col-sm-3 a-item">
<img src="~/images/@Model.GenreDTOs.ElementAt(i).ImageName" width="100" , height="100" class="img-rounded mx-auto d-block" alt="@Model.GenreDTOs.ElementAt(i).GenreName">
<h5>@Model.GenreDTOs.ElementAt(i).GenreName</h5>
<h6>@Model.GenreDTOs.ElementAt(i).NumberOfBooks</h6>
</div>
</a>
}
</div>
<div class="col-sm-2 alert alert-dark">
<a asp-controller="Library" asp-action="IncreaseValue" class="btn a-item">&#8250;</a>
</div>
</div>
</section>
</form>

<div id="itemsSection" class="container">
@foreach (var book in Model.BookDTOs)
<div class="content">
<div id="genreSection" class="container text-center">
<div class="col-sm-2 alert alert-dark">
<a asp-controller="Library" asp-action="DecreaseValue" class="btn a-item">&#8249;</a>
</div>
<div class="col-sm-8">
@for (var i = genreIndex % numberOfGenres; i != (genreIndex + displayedGenres) % (numberOfGenres); i = (i + 1) % numberOfGenres)
{
<a asp-controller="Library" asp-action="Open" asp-route-id="@book.Id">
<div class="col-sm-4 text-center a-item book">
@if (book.Title.Length <= 25)
{
<h2>@book.Title</h2>
<br />
<h5>@book.Author.FirstName @book.Author.LastName</h5>
<img src="~/images/@book.ImageName" width="200" height="300" class="img-rounded mx-auto d-block" alt="@book.Title" />
}
else
{
<h2>@book.Title</h2>
<h5>@book.Author.FirstName @book.Author.LastName</h5>
<img src="~/images/@book.ImageName" width="200" height="300" class="img-rounded mx-auto d-block" alt="@book.Title" />
}
<a asp-controller="Library" asp-action="OpenByGenre" asp-route-id="@Model.GenreDTOs.ElementAt(i).Id" class="a-item">
<div class="col-sm-3 a-item">
<img src="~/images/@Model.GenreDTOs.ElementAt(i).ImageName" width="100" , height="100" class="img-rounded mx-auto d-block" alt="@Model.GenreDTOs.ElementAt(i).GenreName">
<h5>@Model.GenreDTOs.ElementAt(i).GenreName</h5>
<h6>@Model.GenreDTOs.ElementAt(i).NumberOfBooks</h6>
</div>
</a>
}
</div>
<div class="col-sm-2 alert alert-dark">
<a asp-controller="Library" asp-action="IncreaseValue" class="btn a-item">&#8250;</a>
</div>
</div>

<div id="itemsSection" class="container">
@foreach (var book in Model.BookDTOs)
{
<a asp-controller="Library" asp-action="Open" asp-route-id="@book.Id">
<div class="col-sm-4 text-center a-item book">
@if (book.Title.Length <= 25)
{
<h2>@book.Title</h2>
<br />
<h5>@book.Author.FirstName @book.Author.LastName</h5>
<img src="~/images/@book.ImageName" width="200" height="300" class="img-rounded mx-auto d-block" alt="@book.Title" />
}
else
{
<h2>@book.Title</h2>
<h5>@book.Author.FirstName @book.Author.LastName</h5>
<img src="~/images/@book.ImageName" width="200" height="300" class="img-rounded mx-auto d-block" alt="@book.Title" />
}
</div>
</a>
}
</div>

</div>
<hr />
<div class="paginationBox">


@if (Model.PaginationViewModel.HasPreviousPage)
{
<a asp-action="Index"
asp-route-page="@(Model.PaginationViewModel.PageNumber - 1)"
class="btn btn-default btn">
<i class="glyphicon glyphicon-chevron-left"></i>

</a>
}
@for (int i = 1; i < Model.PaginationViewModel.TotalPages; i++)
{
@if (i == Model.PaginationViewModel.PageNumber)
{
<a asp-action="Index"
asp-route-page="@(i)"
class="btn btn-default btn" style="background-color:cadetblue;">
@i
</a>
}
else
{
<a asp-action="Index"
asp-route-page="@(i)"
class="btn btn-default btn">
@i
</a>
}
}

@if (Model.PaginationViewModel.HasNextPage)
{
<a asp-action="Index"
asp-route-page="@(Model.PaginationViewModel.PageNumber + 1)"
class="btn btn-default btn">
<i class="glyphicon glyphicon-chevron-right"></i>

</a>
}
</div>


<style>
.paginationBox {
align-items: center;
justify-content: center;
display:flex;
}
</style>