public StatsViewModel(string userId) { MoneyTrackerContext ctx = new MoneyTrackerContext(); entries = ctx.Entries.Where(x => x.UserID == userId).ToList(); if(entries.Count > 0) { firstEntry = entries.OrderBy(x => x.EntryDate).FirstOrDefault().EntryDate; lastEntry = entries.OrderBy(x => x.EntryDate).LastOrDefault().EntryDate; SetSpendingsByMonth(DateTime.Now.Year); SetSpendingsByCategory(); SetSpendingsByDayOfWeek(DateTime.Now.Year); SetSpendingsByDayOfYear(DateTime.Now.Year); SetMostExpensivePurchases(5); } }
public object GetMostSpentOnCategory() { MoneyTrackerContext ctx = new MoneyTrackerContext(); double max = 0; string name = ""; foreach (var category in ctx.Categories) { var value = GetTotalSpentOnCategory(category.CategoryName); if (value > max) { max = value; name = category.CategoryName; } } return new { CategoryName = name, Total = max }; }
public double GetTotalSpentOnCategory(string categoryName) { MoneyTrackerContext ctx = new MoneyTrackerContext(); var category = ctx.Categories.Where(x => x.CategoryName == categoryName).FirstOrDefault(); if (category == null) throw new Exception("Category Name not found"); var userId = User.Identity.GetUserId(); double sum = 0; var entries = ctx.Entries.Where(x => x.CategoryID == category.CategoryID && x.UserID == userId).ToList(); foreach (var entry in entries) { sum += entry.EntrySum; } return sum; }
private void SetSpendingsByCategory() { SpendingsByCategory = new Dictionary<int, CategoryViewModel>(); foreach (var item in entries) { MoneyTrackerContext ctx = new MoneyTrackerContext(); int categoryId = item.CategoryID; if (categoryId == 0) { continue; } string categoryName = ctx.Categories.Where(x => x.CategoryID == categoryId).FirstOrDefault().CategoryName; if (SpendingsByCategory.ContainsKey(categoryId)) { SpendingsByCategory[categoryId].Sum += item.EntrySum; } else { SpendingsByCategory[categoryId] = new CategoryViewModel { Sum = item.EntrySum, CategoryID = categoryId, CategoryName = categoryName }; } } }