public async Task <List <CategoryWithAmountModel> > HandleAsync(ListMonthCategoryWithOutcome query) { using (ReadModelContext db = dbFactory.Create()) { 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(); return(await GetCategoryWithAmounts(db, query.UserKey, outcomes)); } }
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()); } }
public async Task <IEnumerable <CategoryWithAmountModel> > HandleAsync(ListMonthCategoryWithOutcome query) { using (ReadModelContext db = new ReadModelContext()) { Dictionary <Guid, Price> totals = new Dictionary <Guid, Price>(); List <OutcomeEntity> outcomes = await db.Outcomes .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 outcome.Categories) { Price price; if (totals.TryGetValue(category.CategoryId, out price)) { price = price + new Price(outcome.Amount, outcome.Currency); } else { price = new Price(outcome.Amount, outcome.Currency); } 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, item.Value )); } return(result.OrderBy(m => m.Name)); } }