public async Task <IActionResult> Index() { // get a user manager and the id of the current user var user = await _userManager.GetUserAsync(HttpContext.User); string id = user.Id; //get the savings amount of the current user var savingsAmount = new SavingsAmount(); var userSavingsAmount = savingsAmount.GetSavingsAmount(id); if (_context.Goals.Where(g => g.UserId == user.Id).Count() == 0) { // Create a new Goal if collection is empty, // which means you can't delete all Goals. _context.Goals.Add( new Goal { UserId = user.Id, Name = "Emergency Fund, your 1st goal", Amount = 1000.00, Description = "One of the most important things to save for is an emergency. This is recommended as a first step." } ); _context.SaveChanges(); } GoalIndexViewModel viewModel = new GoalIndexViewModel { UserTotalSavings = savingsAmount.Amount, Goals = _context.Goals .Where(g => g.UserId == user.Id) }; return(View(viewModel)); }
public ActionResult Index() { // Query all goals in database List <Goal> goals = _context.Goals.ToList(); // Query all logs for that specific goal foreach (var goal in goals) { goal.GoalLogs = _context.GoalLogs.Where(log => log.GoalId == goal.Id).ToList(); } var model = new GoalIndexViewModel { Goals = goals }; return(View(model)); }