public async Task <List <CategoryWithAmountModel> > HandleAsync(ListMonthCategoryWithOutcome query) { using (ReadModelContext db = readModelContextFactory.Create()) { Dictionary <Guid, Price> totals = new Dictionary <Guid, Price>(); List <OutcomeEntity> outcomes = await db.Outcomes .WhereUserKey(query.UserKey) .Where(o => o.When.Month == query.Month.Month && o.When.Year == query.Month.Year) .Include(o => o.Categories) .ToListAsync(); foreach (OutcomeEntity outcome in outcomes) { foreach (OutcomeCategoryEntity category in db.OutcomeCategories.Where(oc => oc.OutcomeId == outcome.Id)) { Price price; if (totals.TryGetValue(category.CategoryId, out price)) { price = price + priceConverter.ToDefault(query.UserKey, outcome); } else { price = priceConverter.ToDefault(query.UserKey, outcome); } totals[category.CategoryId] = price; } } List <CategoryWithAmountModel> result = new List <CategoryWithAmountModel>(); foreach (var item in totals) { CategoryModel model = (await db.Categories.FindAsync(item.Key)).ToModel(); result.Add(new CategoryWithAmountModel( model.Key, model.Name, model.Description, model.Color, model.Icon, item.Value )); } return(result.OrderBy(m => m.Name).ToList()); } }
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)) { price = price + priceConverter.ToDefault(userKey, outcome); } else { price = priceConverter.ToDefault(userKey, outcome); } 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(); 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); }
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)) { price = price + priceConverter.ToDefault(userKey, outcome); } else { price = priceConverter.ToDefault(userKey, outcome); } totals[category.CategoryId] = price; } } List <CategoryWithAmountModel> result = new List <CategoryWithAmountModel>(); foreach (var item in totals) { CategoryModel model = (await db.Categories.FindAsync(item.Key)).ToModel(); result.Add(new CategoryWithAmountModel( model.Key, model.Name, model.Description, model.Color, model.Icon, item.Value )); } return(result.OrderBy(m => m.Name).ToList()); }