classlib/ExpenseTracker.Application/Accounts/Queries/GetWithPagination/GetAccountsWithPaginationQueryHandler.cs
2024-08-07 21:12:02 +03:00

43 lines
1.6 KiB
C#

using AutoMapper;
using MediatR;
using ExpenseTracker.Application.Common.Extensions;
using ExpenseTracker.Application.Common.Interfaces.Repositories;
using ExpenseTracker.Application.Common.Interfaces.Services;
using ExpenseTracker.Application.Common.Models;
using ExpenseTracker.Domain.Entities;
namespace ExpenseTracker.Application.Accounts.Queries.GetWithPagination;
public class GetAccountssWithPaginationQueryHandler : IRequestHandler<GetAccountsWithPaginationQuery, PaginatedList<AccountDto>>
{
private readonly IMapper _mapper;
private readonly IAccountRepository _accountRepository;
private readonly ITransactionRepository _transactionRepository;
private readonly ISessionUserService _sessionUserService;
public GetAccountssWithPaginationQueryHandler(
IMapper mapper,
IAccountRepository repository,
ITransactionRepository transactionRepository,
ISessionUserService sessionUserService)
{
_mapper = mapper;
_accountRepository = repository;
_sessionUserService = sessionUserService;
_transactionRepository = transactionRepository;
}
public async Task<PaginatedList<AccountDto>> Handle(GetAccountsWithPaginationQuery request, CancellationToken cancellationToken)
{
var entities = _accountRepository.Queryable;
if (!request.GetAll)
{
entities = entities.Where(e => e.UserId == _sessionUserService.Id);
}
return entities
.ProjectToPaginatedList<Account, AccountDto>(request.PageNumber, request.PageSize, _mapper.ConfigurationProvider);
}
}