Beispiel #1
0
        public async Task <IActionResult> Create([Bind("BudgetID,CategoryID,Description,Amount")] Expense expense)
        {
            if (ModelState.IsValid)
            {
                _context.Add(expense);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", new { budgetId = expense.BudgetID }));
            }

            return(View(expense));
        }
Beispiel #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var aspUser = await _userManager.GetUserAsync(User);

            if (aspUser == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var email = await _userManager.GetEmailAsync(aspUser);

            var user = _context.Users.FirstOrDefault(u => u.Email == email);

            user.FirstName = Input.FirstName;
            user.LastName  = Input.LastName;

            _context.Update(user);
            await _context.SaveChangesAsync();

            await _signInManager.RefreshSignInAsync(aspUser);

            StatusMessage = "Your profile has been updated";
            return(RedirectToPage());
        }
Beispiel #3
0
        public async Task <IActionResult> Create([Bind("CategoryName,UserID")] Category category)
        {
            var user = GetUser();

            if (user.UserID != category.UserID)
            {
                return(NotFound());
            }

            var viewModel = new CategoryViewModel
            {
                UserID     = user.UserID,
                Categories = _context.Categories.Where(c => c.UserID == user.UserID)
            };

            ViewData["DuplicateNameErrorMessage"] = "";

            try
            {
                category.CategoryName = category.CategoryName.Trim();
            }
            catch (NullReferenceException)
            {
                ViewData["DuplicateNameErrorMessage"] = "Category name cannot be empty";
                return(View("Index", viewModel));
            }

            var existingCategory = await _context.Categories
                                   .SingleOrDefaultAsync(c => c.CategoryName.ToLower() == category.CategoryName.ToLower() && c.UserID == category.UserID);

            if (existingCategory != null)
            {
                ViewData["DuplicateNameErrorMessage"] = "This category already exists";
                return(View("Index", viewModel));
            }

            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View("Index", viewModel));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            RequirePassword = await _userManager.HasPasswordAsync(user);

            if (RequirePassword)
            {
                if (!await _userManager.CheckPasswordAsync(user, Input.Password))
                {
                    ModelState.AddModelError(string.Empty, "Password not correct.");
                    return(Page());
                }
            }

            var result = await _userManager.DeleteAsync(user);

            var userId = await _userManager.GetUserIdAsync(user);

            if (!result.Succeeded)
            {
                throw new InvalidOperationException($"Unexpected error occurred deleteing user with ID '{userId}'.");
            }

            // Delete user from app Users table

            var appUser = await _context.Users.SingleOrDefaultAsync(u => u.Email == user.Email);

            var categories = await _context.Categories.Where(c => c.UserID == appUser.UserID).ToListAsync();

            foreach (var category in categories)
            {
                _context.Categories.Remove(category);
            }

            var budgets = await _context.Budgets.Where(b => b.UserID == appUser.UserID).ToListAsync();

            foreach (var budget in budgets)
            {
                _context.Budgets.Remove(budget);
            }

            _context.Users.Remove(appUser);

            await _context.SaveChangesAsync();

            await _signInManager.SignOutAsync();

            _logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId);

            return(Redirect("~/"));
        }
Beispiel #5
0
        public async Task <IActionResult> Create([Bind("UserID,BudgetName,BudgetMax,BudgetTotal,CreationDate")] Budget budget)
        {
            var user = GetUser();

            if (user.UserID != budget.UserID)
            {
                return(NotFound());
            }

            ViewData["DuplicateNameErrorMessage"] = "";

            try
            {
                budget.BudgetName = budget.BudgetName.Trim();
            }
            catch (NullReferenceException)
            {
                ViewData["DuplicateNameErrorMessage"] = "Budget name cannot be empty";
                return(View(budget));
            }

            var existingBudget = await _context.Budgets
                                 .SingleOrDefaultAsync(b => b.BudgetName.ToLower() == budget.BudgetName.ToLower() && b.UserID == budget.UserID);

            if (existingBudget != null)
            {
                ViewData["DuplicateNameErrorMessage"] = "This budget already exists";
                return(View(budget));
            }

            if (ModelState.IsValid)
            {
                _context.Add(budget);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(budget));
        }
Beispiel #6
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var aspUser = new IdentityUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(aspUser, Input.Password);

                if (result.Succeeded)
                {
                    var user = new User
                    {
                        FirstName    = Input.FirstName,
                        LastName     = Input.LastName,
                        Email        = Input.Email,
                        TotalExpense = 0
                    };

                    _context.Add(user);
                    await _context.SaveChangesAsync();

                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(aspUser);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = aspUser.Id, code = code },
                        protocol: Request.Scheme);

                    var emailMessage = $"Welcome to ExpTrackr!<br><br>To finish the registration process, please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.";

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm Your Account", emailMessage);

                    return(RedirectToPage("/Account/ThankYou"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }