private async Task <List <CategoryWithAmountModel> > GetCategoryWithAmounts(ReadModelContext db, IKey userKey, List <OutcomeEntity> outcomes) { Dictionary <Guid, Price> totals = new Dictionary <Guid, Price>(); foreach (OutcomeEntity outcome in outcomes) { foreach (OutcomeCategoryEntity category in outcome.Categories) { Price price; if (totals.TryGetValue(category.CategoryId, out price)) { if (!outcome.IsFixed) { price = price + priceConverter.ToDefault(userKey, outcome); } } else { if (!outcome.IsFixed) { price = priceConverter.ToDefault(userKey, outcome); } else { price = priceConverter.ZeroDefault(userKey); } } totals[category.CategoryId] = price; } } List <CategoryWithAmountModel> result = new List <CategoryWithAmountModel>(); foreach (var item in totals) { CategoryEntity entity = await db.Categories.FirstOrDefaultAsync(c => c.Id == item.Key); if (entity == null) { throw Ensure.Exception.InvalidOperation($"Missing category with id '{item.Key}'."); } CategoryModel model = entity.ToModel(false); result.Add(new CategoryWithAmountModel( model.Key, model.Name, model.Description, model.Color, model.Icon, item.Value )); } return(result.OrderBy(m => m.Name).ToList()); }
private Price SumPriceInDefaultCurrency(IKey userKey, IEnumerable <PriceFixed> outcomes) { Price price = priceConverter.ZeroDefault(userKey); foreach (PriceFixed outcome in outcomes) { price += priceConverter.ToDefault(userKey, outcome); } return(price); }
public async Task <Price> HandleAsync(GetTotalMonthOutcome query) { using (ReadModelContext db = readModelContextFactory.Create()) { List <PriceFixed> outcomes = await db.Outcomes .WhereUserKey(query.UserKey) .Where(o => o.When.Month == query.Month.Month && o.When.Year == query.Month.Year) .Select(o => new PriceFixed(new Price(o.Amount, o.Currency), o.When)) .ToListAsync(); Price price = priceConverter.ZeroDefault(query.UserKey); foreach (PriceFixed outcome in outcomes) { price += priceConverter.ToDefault(query.UserKey, outcome); } return(price); } }