Commit f64990c1 authored by TTrueBenji's avatar TTrueBenji

refactor: сервис пагинации сделал обобщенным, чтобы его можно было…

refactor: сервис пагинации сделал обобщенным, чтобы его можно было переиспользовать для товаров и т.д.
parent 242d1f32
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using PhoneStore.Models;
namespace PhoneStore.Services.Abstractions namespace PhoneStore.Services.Abstractions
{ {
public interface IPaginationService public interface IPaginationService<T>
{ {
Task<(IQueryable<User>, int)> GetABatchOfData(IQueryable<User> users, int page, int pageSize); Task<(IQueryable<T>, int)> GetABatchOfData(IQueryable<T> elements, int page, int pageSize);
} }
} }
\ No newline at end of file
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions; using PhoneStore.Services.Abstractions;
namespace PhoneStore.Services namespace PhoneStore.Services
{ {
public class PaginationService : IPaginationService public class PaginationService<T> : IPaginationService<T>
{ {
public async Task<(IQueryable<User>, int)> GetABatchOfData(IQueryable<User> users, int page, int pageSize) public async Task<(IQueryable<T>, int)> GetABatchOfData(IQueryable<T> elements, int page, int pageSize)
{ {
int count = await users.CountAsync(); int count = await elements.CountAsync();
return (users.Skip((page - 1) * pageSize).Take(pageSize), count); return (elements.Skip((page - 1) * pageSize).Take(pageSize), count);
} }
} }
} }
\ No newline at end of file
...@@ -12,13 +12,13 @@ namespace PhoneStore.Services ...@@ -12,13 +12,13 @@ namespace PhoneStore.Services
private readonly MobileContext _db; private readonly MobileContext _db;
private readonly IUsersSortService _sortService; private readonly IUsersSortService _sortService;
private readonly IUsersFilter _usersFilter; private readonly IUsersFilter _usersFilter;
private readonly IPaginationService _paginationService; private readonly IPaginationService<User> _paginationService;
public UserService( public UserService(
MobileContext db, MobileContext db,
IUsersSortService sortService, IUsersSortService sortService,
IUsersFilter usersFilter, IUsersFilter usersFilter,
IPaginationService paginationService) IPaginationService<User> paginationService)
{ {
_db = db; _db = db;
_sortService = sortService; _sortService = sortService;
......
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