示例#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);
        }
示例#2
0
        public async Task<ActionResult> Create([Bind(Include = "Id,HouseholdName")] Household household)
        {
            ApplicationUser user = db.Users.FirstOrDefault(x => x.UserName == User.Identity.Name);

            if (ModelState.IsValid)
            {
                db.Households.Add(household);
                await db.SaveChangesAsync();
                Household createdHousehold = db.Households.OrderByDescending(x => x.Id).FirstOrDefault();

                //Create a default budget to add to the household
                Budget defaultBudget = new Budget
                {
                    BudgetName = "Default Budget",
                    HouseholdId = createdHousehold.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);
                user.Households.Add(household);
                db.SaveChanges();
                return RedirectToAction("AtAGlance", "Home");
            }
            return RedirectToAction("AtAGlance", "Home");
        }