Ejemplo n.º 1
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                Household household = new Household { HouseholdName = model.HouseholdName, IsDefault = true };
                List<Household> households = new List<Household>();
                households.Add(household);

                var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Households = households, ProfilePicUrl = "~/Images/cascades.jpg" };
                var result = await UserManager.CreateAsync(user, model.Password);
               
                if (result.Succeeded)
                {
                    await db.SaveChangesAsync();

                    Household addedHousehold = db.Households.OrderByDescending(x => x.Id).FirstOrDefault();

                    //add a default budget
                    Budget defaultBudget = new Budget
                    {
                        BudgetName = "Default Budget",
                        HouseholdId = addedHousehold.Id,
                        IsDefault = true
                    };

                    defaultBudget.BudgetItems.Add(new BudgetItem { CategoryId = 5, Amount = 50 });
                    defaultBudget.BudgetItems.Add(new BudgetItem { CategoryId = 4, Amount = 100 });
                    defaultBudget.BudgetItems.Add(new BudgetItem { CategoryId = 3, Amount = 150 });
                    db.Budgets.Add(defaultBudget);
               
                    await db.SaveChangesAsync();
                    addedHousehold.SelectedBudgetId = defaultBudget.Id;
                    db.SaveChanges();
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    //// Send an email with this link
                    //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("AtAGlance", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Ejemplo n.º 2
0
        public async Task<ActionResult> Details(Household household)
        {
            Household house = db.Households.FirstOrDefault(x => x.Id == household.Id);
            ApplicationUser user = db.Users.FirstOrDefault(x => x.UserName == User.Identity.Name);

            if (!house.Members.Contains(user))
            {
                return RedirectToAction("Unauthorized", "Error");
            }

            house.SelectedBudgetId = household.SelectedBudgetId;           
            await db.SaveChangesAsync();

            //pass sum of all account balances through Viewbag
            decimal householdTotalBalance = 0.00M;
            foreach (Account account in house.Accounts)
            {
                householdTotalBalance += account.Balance;
            }

            ViewBag.HouseholdTotal = householdTotalBalance;

            //pass budgets to view for the select budget dropdown

            List<Budget> budgets = house.Budgets.ToList();

            SelectList selectList = new SelectList(budgets, "Id", "BudgetName");

            ViewBag.SelectBudgetDropdown = selectList;

            ViewBag.SelectedBudgetName = db.Budgets.FirstOrDefault(x => x.Id == household.SelectedBudgetId).BudgetName;

            return View(house);
        }