/// <summary> /// Aggregates all transactions of a single account. /// </summary> /// <param name="accountId">Id of the account.</param> /// <returns>A task that completes with the result of the aggregation.</returns> public async Task <AggregationResult> AggregateTransactionsOfAccount(int accountId) { // Query all transactions of the account. var contextFreeTransactions = await _entityQuerySource.Query(q => q.Get <ContextFreeTransaction>().Where(x => x.AccountId == accountId)); var stockTransactions = await _entityQuerySource.Query(q => q.Get <StockTransaction>().Where(x => x.AccountId == accountId)); // Map those transactions to AggregationItems. var aggregationItems = contextFreeTransactions.Select(_mapper.Map) .Concat(stockTransactions.Select(_mapper.Map)); // Aggregate the items to the final result. return(AggregateItems(aggregationItems)); }
/// <summary> /// Imports the list of currently available stocks. /// </summary> /// <returns>A task that will complete when the import has completed.</returns> public async Task ImportStocks() { // Collect both list of stocks: stored and currently available. var storedStocks = (await _entityQuerySource.Query(q => q.Get <Stock>())).ToDictionary(x => x.Isin); var currentStocks = await _stockQueryService.QueryAll(); await _entityRepositoryFactory.Use(async repository => { foreach (var stock in currentStocks) { // Create stock in storage if not yet present. if (!storedStocks.TryGetValue(stock.Isin, out Stock matchingStoredStock)) { repository.Add(stock); } // Update name if this has changed. else if (stock.Name != matchingStoredStock.Name) { await repository.Change <Stock>(matchingStoredStock.Id, s => { s.Name = stock.Name; }); } } // For now, do not delete obsolete stocks... }); }
/// <summary> /// Searches for stocks matching the specified search term. /// </summary> /// <param name="searchTerm">Term to use for matching stocks.</param> /// <returns>A task that will complete with the matched stocks.</returns> public async Task <IEnumerable <Stock> > SearchStocks(string searchTerm) { return(await _entityQuerySource.Query(q => q.Get <Stock>().Where(s => s.Name.Contains(searchTerm)))); }