classlib/ExpenseTracker.Application/Accounts/Queries/Charts/GetExpenseBarChart/GetExpenseBarChartQueryHandler.cs
2024-08-07 21:12:02 +03:00

44 lines
1.6 KiB
C#

using AutoMapper;
using MediatR;
using ExpenseTracker.Application.Common.Interfaces.Repositories;
using ExpenseTracker.Application.Common.Interfaces.Services;
namespace ExpenseTracker.Application.Accounts.Queries.Charts.GetExpenseBarChart;
public class GetExpenseBarChartQueryHandler : IRequestHandler<GetExpenseBarChartQuery, DailyTransactionsBarChart>
{
private readonly IMapper _mapper;
private readonly IAccountRepository _accountRepository;
private readonly ITransactionRepository _transactionRepository;
private readonly ISessionUserService _sessionUserService;
public GetExpenseBarChartQueryHandler(
IMapper mapper,
IAccountRepository repository,
ITransactionRepository transactionRepository,
ISessionUserService sessionUserService)
{
_mapper = mapper;
_accountRepository = repository;
_sessionUserService = sessionUserService;
_transactionRepository = transactionRepository;
}
public async Task<DailyTransactionsBarChart> Handle(GetExpenseBarChartQuery request, CancellationToken cancellationToken)
{
var entities = _transactionRepository.Queryable
.Where(e =>
e.AccountId == request.AccountId &&
e.Amount < 0)
.ToList()
.Where(e =>
DateOnly.FromDateTime(e.Time.DateTime) >= request.FromDate &&
DateOnly.FromDateTime(e.Time.DateTime) < request.ToDate)
.OrderBy(e => e.Time);
var groupsByDate = entities.GroupBy(e => e.Time.Date, t => t.Amount);
return new DailyTransactionsBarChart(groupsByDate, request.FromDate, request.ToDate);
}
}