protected override async ValueTask <IResult <IEnumerable <GroupedTransactionsModel> > > HandleImpl(GetAssetGroupedTransactionsQuery request, CancellationToken cancellationToken) { var result = new List <GroupedTransactionsModel>(); IQueryable <Asset> query = _assetRepository.GetAll() .Include(a => a.Group); if (request.Filter != null) { if (request.Filter.AssetId.HasValue) { query = query.Where(t => t.Id == request.Filter.AssetId.Value); } if (request.Filter.GroupId.HasValue) { query = query.Where(t => t.GroupId == request.Filter.GroupId.Value); } } if (request.Order.HasValue) { query = request.Order.Value == General.Models.Order.Ascending ? query.OrderBy(a => a.Name) : query.OrderByDescending(a => a.Name); } var assets = await query .ToListAsync(cancellationToken) .ConfigureAwait(false); var transactions = _transactionRepository.GetAll(); var user = await _userRepository.GetMe(cancellationToken) .ConfigureAwait(false); var toCurrencyId = user.GetSettings()?.PrefferedCurrency; var toCurrency = toCurrencyId.HasValue ? await _currencyRepository.GetById(toCurrencyId.Value, cancellationToken) : null; var toCurrencyCode = toCurrency?.Code ?? _defaults.Currency; foreach (var asset in assets) { if (cancellationToken.IsCancellationRequested) { return(await FromCancellationToken(cancellationToken)); } var assetTransactions = transactions.Where(t => t.AssetId == asset.Id); if (request.Filter != null) { if (request.Filter.From.HasValue) { assetTransactions = assetTransactions.Where(t => t.CreatedAt >= request.Filter.From.Value); } if (request.Filter.To.HasValue) { assetTransactions = assetTransactions.Where(t => t.CreatedAt <= request.Filter.To.Value); } } if (!await assetTransactions.AnyAsync(cancellationToken).ConfigureAwait(false)) { continue; } var priceValue = await _forexService.GetPriceOf(asset.Code, toCurrencyCode, cancellationToken) .ConfigureAwait(false); var model = new GroupedTransactionsModel { Asset = _mapper.Map <Asset, AssetModel>(asset), Amount = await transactions.SumAsync(t => t.Amount, cancellationToken) .ConfigureAwait(false), TotalBought = await transactions.SumAsync(t => t.Amount *t.Rate, cancellationToken) .ConfigureAwait(false), ActualValue = priceValue.IsSuccess ? priceValue.Value.Rate : null }; result.Add(model); } return(result.AsResult()); }
protected override string GenerateKey(GetAssetGroupedTransactionsQuery request) => $"{base.GenerateKey(request)}.{request.Order}.{request.Filter}";