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 async Task <ActionResult <SavingsAmount> > GetSavingsAmount() { // get Id of current user // What does Identity do? NO ONE KNOWS! // Are you ready for the magic?? var user = await _userManager.GetUserAsync(HttpContext.User); string id = user.Id; // get savingsAmount if there is one var savingsAmount = await _context.SavingsAmounts.FindAsync(id); // if there isn't, make one and save it if (savingsAmount == null) { savingsAmount = new SavingsAmount() { Id = id, Amount = 0 }; _context.Add(savingsAmount); _context.SaveChanges(); } return(savingsAmount); }
/// <summary> /// Returns a hash code for this instance. /// </summary> /// <returns> /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// </returns> public override int GetHashCode() { unchecked // only needed if you're compiling with arithmetic checks enabled { // (the default compiler behaviour is *disabled*, so most folks won't need this) int hash = 13; hash = (hash * 7) + Project.GetHashCode(); hash = (hash * 7) + Description.GetHashCode(); hash = (hash * 7) + StartDate.GetHashCode(); hash = (hash * 7) + Category.GetHashCode(); hash = (hash * 7) + Responsible.GetHashCode(); hash = (hash * 7) + SavingsAmount.GetHashCode(); hash = (hash * 7) + Currency.GetHashCode(); hash = (hash * 7) + Complexity.GetHashCode(); return(hash); } }
public async Task <IActionResult> PutSavingsAmount(SavingsAmount savingsAmount) { _context.Entry(savingsAmount).State = EntityState.Modified; var id = savingsAmount.Id; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SavingsAmountExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }