Commit 681b375f authored by TTrueBenji's avatar TTrueBenji

refactor: вынес значение количества записей на странице пользователей в файл appsettings.json.

parent a2934765
...@@ -17,10 +17,9 @@ namespace PhoneStore.Controllers ...@@ -17,10 +17,9 @@ namespace PhoneStore.Controllers
[HttpGet] [HttpGet]
public async Task<IActionResult> Index(string filterByName, Order order = Order.AgeAsc, int page = 1) public async Task<IActionResult> Index(string filterByName, Order order = Order.AgeAsc, int page = 1)
{ {
int pageSize = 2;
ViewBag.NameSort = order == Order.NameAsc ? Order.NameDesc : Order.NameAsc; ViewBag.NameSort = order == Order.NameAsc ? Order.NameDesc : Order.NameAsc;
ViewBag.AgeSort = order == Order.AgeAsc ? Order.AgeDesc : Order.AgeAsc; ViewBag.AgeSort = order == Order.AgeAsc ? Order.AgeDesc : Order.AgeAsc;
var model = await _userService.GetAll(order, filterByName, pageSize, page); var model = await _userService.GetAll(order, filterByName, page);
return View(model); return View(model);
} }
......
...@@ -6,6 +6,6 @@ namespace PhoneStore.Services.Abstractions ...@@ -6,6 +6,6 @@ namespace PhoneStore.Services.Abstractions
{ {
public interface IUserService public interface IUserService
{ {
Task<UsersViewModel> GetAll(Order order, string filterByName, int pageSize, int page); Task<UsersViewModel> GetAll(Order order, string filterByName, int page);
} }
} }
\ No newline at end of file
...@@ -12,7 +12,5 @@ namespace PhoneStore.Services ...@@ -12,7 +12,5 @@ namespace PhoneStore.Services
int count = await elements.CountAsync(); int count = await elements.CountAsync();
return (elements.Skip((page - 1) * pageSize).Take(pageSize), count); return (elements.Skip((page - 1) * pageSize).Take(pageSize), count);
} }
} }
} }
\ No newline at end of file
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using PhoneStore.Helpers; using PhoneStore.Helpers;
using PhoneStore.Models; using PhoneStore.Models;
using PhoneStore.Services.Abstractions; using PhoneStore.Services.Abstractions;
...@@ -13,27 +14,31 @@ namespace PhoneStore.Services ...@@ -13,27 +14,31 @@ namespace PhoneStore.Services
private readonly IUsersSortService _sortService; private readonly IUsersSortService _sortService;
private readonly IUsersFilter _usersFilter; private readonly IUsersFilter _usersFilter;
private readonly IPaginationService<User> _paginationService; private readonly IPaginationService<User> _paginationService;
private readonly IConfiguration _configuration;
public UserService( public UserService(
MobileContext db, MobileContext db,
IUsersSortService sortService, IUsersSortService sortService,
IUsersFilter usersFilter, IUsersFilter usersFilter,
IPaginationService<User> paginationService) IPaginationService<User> paginationService,
IConfiguration configuration)
{ {
_db = db; _db = db;
_sortService = sortService; _sortService = sortService;
_usersFilter = usersFilter; _usersFilter = usersFilter;
_paginationService = paginationService; _paginationService = paginationService;
_configuration = configuration;
} }
public async Task<UsersViewModel> GetAll(Order order, string filterByName, int pageSize, int page) public async Task<UsersViewModel> GetAll(Order order, string filterByName, int page)
{ {
var pageSize = int.Parse(_configuration["UsersPageSize"]); //Размер страницы берется из appsettings.json
var users = _db.Users.AsQueryable(); var users = _db.Users.AsQueryable();
var filteredUsers = _usersFilter.FilterByName(users, filterByName); var filteredUsers = _usersFilter.FilterByName(users, filterByName);
var sortedUsers = _sortService.Sort(filteredUsers, order); var sortedUsers = _sortService.Sort(filteredUsers, order);
var paginationData = await _paginationService.GetABatchOfData(sortedUsers, page, pageSize); var (queryable, count) = await _paginationService.GetABatchOfData(sortedUsers, page, pageSize);
return paginationData.Item1.MapToUsersViewModel(filterByName, page, paginationData.Item2, pageSize); return queryable.MapToUsersViewModel(filterByName, page, count, pageSize);
} }
} }
} }
\ No newline at end of file
...@@ -12,5 +12,6 @@ ...@@ -12,5 +12,6 @@
"AllowedHosts": "*", "AllowedHosts": "*",
"PathToDefaultAvatar": { "PathToDefaultAvatar": {
"Path": "images\\phoneImages\\default.jpg" "Path": "images\\phoneImages\\default.jpg"
} },
"UsersPageSize" : 2
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment