public ActionResult Index(int?id) { if (id == null) { return(RedirectToAction("Index", "Errors", new { errorMessage = "Account not found" })); } Account account = db.Accounts.Find(id); if (account == null) { return(RedirectToAction("Index", "Errors", new { errorMessage = "Account not found" })); } //check if the user is authorized to view this account var helper = new AccountUserHelper(); var user = User.Identity.GetUserId(); if (helper.CanUserAccessAccount(user, (int)id) == false) { return(RedirectToAction("Index", "Errors", new { errorMessage = "Not authorized" })); } var createTransactionModel = new TransactionCreateViewModel(); createTransactionModel.AccountId = account.Id; var categories = db.Categories.ToList(); createTransactionModel.CategoryList = new SelectList(categories, "Id", "Name"); var householdId = User.Identity.GetHouseholdId(); var householdUsers = db.Users.Where(x => x.HouseholdId == (int)householdId).ToList(); createTransactionModel.HouseholdUsersList = new SelectList(householdUsers, "Id", "UserName"); //pass a model to create a new transaction through the ViewBag ViewBag.CreateModel = createTransactionModel; //get all the transactions for this account var transactions = db.Transactions.Where(x => x.AccountId == id).ToList(); //transform the transactions so we can show them in the index page var transactionsToShow = new List <TransactionsIndexViewModel>(); foreach (var t in transactions) { var transToShow = new TransactionsIndexViewModel(t); transactionsToShow.Add(transToShow); } //pass the account Id to the model ViewBag.AccountId = account.Id; //get the account name and put it in the ViewBag ViewBag.AccountName = account.Name; //update the account balance account.UpdateAccountBalance(); //update the reconciled balance account.UpdateReconciledAccountBalance(); //pass the balances in the ViewBag ViewBag.Balance = account.Balance; ViewBag.Reconciled = account.ReconciledBalance; ViewBag.StartingBalance = account.StartingBalance; return(View(transactionsToShow)); }
public ActionResult Details(int?id) { if (id == null) { return(RedirectToAction("Index", "Errors", new { errorMessage = "Account not found" })); } Account account = db.Accounts.Find(id); if (account == null) { return(RedirectToAction("Index", "Errors", new { errorMessage = "Account not found" })); } //check if the user is authorized to view this account var helper = new AccountUserHelper(); var user = User.Identity.GetUserId(); if (helper.CanUserAccessAccount(user, (int)id) == false) { return(RedirectToAction("Index", "Errors", new { errorMessage = "Not authorized" })); } return(View(account)); }
public ActionResult RestoreConfirmed(int id) { if (ModelState.IsValid) { Account account = db.Accounts.Find(id); if (account == null) { return(RedirectToAction("Index", "Errors", new { errorMessage = "Account not found" })); } //check if the user is authorized to delete this account var helper = new AccountUserHelper(); var user = User.Identity.GetUserId(); if (helper.CanUserAccessAccount(user, id) == false) { return(RedirectToAction("Index", "Errors", new { errorMessage = "Not authorized" })); } account.IsActive = true; db.Entry(account).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(id)); }