public async Task <IActionResult> Edit(Guid id) { var budget = await _repo.GetBudget(); var categories = await _repo.GetList <Category>(); Dictionary <Category, float> totalsAndCategories = new Dictionary <Category, float>(); foreach (var item in categories) { float totalForCategory = budget.Where(x => x.CategoryId == item.Id) .Select(x => x.Amount) .FirstOrDefault(); totalsAndCategories.Add(item, totalForCategory > 0 ? totalForCategory : 0); } var viewModel = new EditBudgetViewModel() { TotalsForTheMonth = totalsAndCategories, BudgetId = id }; return(View(viewModel)); }
public void CanEditValueAndCommentOfABudget() { IRepository <Client> clientRepository = new LocalRepository <Client>(); IRepository <Budget> budgetRepository = new LocalRepository <Budget>(); var userManager = new Mock <IIdentityMembershipProvider>(); Budget budget1 = new Budget(); budget1.Id = 1; budget1.Value = 1000; budget1.Month = new DateTime(2014, 1, 1); budget1.ClientId = 1; budgetRepository.Add(budget1); Assert.AreEqual(1, budgetRepository.All().Count()); var controller = new BudgetController(clientRepository, budgetRepository, userManager.Object); var createBudgetPostMessage = new EditBudgetViewModel { id = 1, clientId = 1, month = "2014-01-01", value = 3000, comment = "New Comment" }; controller.Edit(createBudgetPostMessage); Assert.AreEqual(1, budgetRepository.All().Count()); Assert.AreEqual(3000, budgetRepository.All().First().Value); Assert.AreEqual("New Comment", budgetRepository.All().First().Comment); }
private ActionResult ReturnViewWithDateError(EditBudgetViewModel vm) { if (vm.EndDate < vm.StartDate) { ModelState.AddModelError("StartEnd", "End Date must be later than Start Date"); } return(PartialView("EditBudget", vm)); }
private ActionResult AddBudgetReturnEditBudgetView(EditBudgetViewModel vm) { var username = User.Identity.Name; var budget = BudgetService.AddBudget(vm, username); ModelState.Clear(); vm.Id = budget.Id; return(PartialView("EditBudget", vm)); }
public PartialViewResult EditBudgetModal(int id) { var householdId = User.Identity.GetHouseholdId(); var budget = _budgetManager.Get(id, householdId.Value); var model = new EditBudgetViewModel() { Duration = budget.Duration, }; return(PartialView(model)); }
public ActionResult Edit(EditBudgetViewModel model) { if (ModelState.IsValid) { var householdId = User.Identity.GetHouseholdId(); var budget = _budgetManager.Get(model.Id, householdId.Value); budget.Duration = model.Duration; _budgetManager.Edit(budget); } return(RedirectToAction("Index")); }
public ActionResult Edit(EditBudgetViewModel editBudget) { var budget = budgetRepository.Where(c => c.Id == editBudget.id).First(); budget.Comment = editBudget.comment; budget.Value = editBudget.value; budget.IsHandled = false; budgetRepository.SaveChanges(); return(RedirectToAction("List", new { clientId = budget.ClientId })); }
public ActionResult AddBudget(EditBudgetViewModel vm, bool isDone = false) { ViewBag.Title = "Add Budget"; ModelState.Remove("Id"); if (!ModelState.IsValid) { return(ReturnViewWithDateError(vm)); } ViewBag.Title = "Edit Budget"; return(vm.Id == 0 ? AddBudgetReturnEditBudgetView(vm, isDone) : EditBudget(vm, isDone)); }
private ActionResult AddBudgetReturnEditBudgetView(EditBudgetViewModel vm, bool isDone = false) { var username = User.Identity.Name; var budget = BudgetService.AddBudget(vm, username); ModelState.Clear(); vm.Id = budget.Id; if (isDone) { return(RedirectToAction("Index", new { budgetId = budget.Id })); } return(PartialView("EditBudget", vm)); }
public Budget AddBudget(EditBudgetViewModel vm, string username) { var budget = new Budget { Name = vm.Name, StartDate = vm.StartDate, EndDate = vm.EndDate, Username = username }; FinancialPlannerRepository.AddBudget(budget); FinancialPlannerRepository.Save(); return(budget); }
public bool EditBudget(EditBudgetViewModel vm) { var budget = FinancialPlannerRepository.GetBudgets().FirstOrDefault(m => m.Id == vm.Id); if (budget == null) { return(false); } budget.Name = vm.Name; budget.StartDate = vm.StartDate; budget.EndDate = vm.EndDate; FinancialPlannerRepository.Save(); return(true); }
public ActionResult EditBudget(EditBudgetViewModel vm) { ViewBag.Title = "Edit Budget"; if (!ModelState.IsValid) { return(ReturnViewWithDateError(vm)); } var saved = BudgetService.EditBudget(vm); if (!saved) { return(View("Error")); } return(PartialView("EditBudget", vm)); }
public EditBudgetViewModel MapEditBudgetViewModel(int id) { var budget = FinancialPlannerRepository.GetBudgets() .FirstOrDefault(m => m.Id == id); if (budget == null) { return(null); } var vm = new EditBudgetViewModel { Id = budget.Id, Name = budget.Name, StartDate = budget.StartDate, EndDate = budget.EndDate }; return(vm); }
public ActionResult AddBudget(EditBudgetViewModel vm) { ViewBag.Title = "Add Budget"; ModelState.Remove("Id"); if (!ModelState.IsValid) { return(ReturnViewWithDateError(vm)); } ViewBag.Title = "Edit Budget"; if (vm.Id == 0) { return(AddBudgetReturnEditBudgetView(vm)); } EditBudget(vm); return(PartialView("EditBudget", vm)); }
public void CanCreateABudgetAndDoesNotCreateOverlapping() { IRepository <Client> clientRepository = new LocalRepository <Client>(); IRepository <Budget> budgetRepository = new LocalRepository <Budget>(); var userManager = new Mock <IIdentityMembershipProvider>(); Budget budget1 = new Budget(); budget1.Id = 1; budget1.Value = 1000; budget1.Month = new DateTime(2014, 1, 1); budget1.ClientId = 1; Budget budget2 = new Budget(); budget2.Id = 2; budget2.Value = 2000; budget2.Month = new DateTime(2014, 2, 1); budget2.ClientId = 2; budgetRepository.Add(budget1); budgetRepository.Add(budget2); Assert.AreEqual(2, budgetRepository.All().Count()); var controller = new BudgetController(clientRepository, budgetRepository, userManager.Object); EditBudgetViewModel createBudgetPostMessage = new EditBudgetViewModel(); createBudgetPostMessage.clientId = 1; createBudgetPostMessage.month = "2014-03"; createBudgetPostMessage.value = 3000; controller.Create(createBudgetPostMessage); Assert.AreEqual(3, budgetRepository.All().Count()); controller.Create(createBudgetPostMessage); Assert.AreEqual(3, budgetRepository.All().Count()); }
public ActionResult Create(EditBudgetViewModel newBudget) { var budget = new Budget(); budget.ClientId = newBudget.clientId; budget.Comment = newBudget.comment; budget.Value = newBudget.value; var year = int.Parse(newBudget.month.Substring(0, 4)); var month = int.Parse(newBudget.month.Substring(5, 2)); budget.Month = new DateTime(year, month, 1); if (budgetRepository.Where(b => b.ClientId == budget.ClientId && b.Month == budget.Month).Any()) { return(RedirectToAction("List", new { clientId = newBudget.clientId, errorMessage = "Finns redan en budget för vald månad." })); } budgetRepository.Add(budget); budgetRepository.SaveChanges(); return(RedirectToAction("List", new { clientId = newBudget.clientId })); }
public ActionResult EditBudget(EditBudgetViewModel vm, bool isDone = false) { ViewBag.Title = "Edit Budget"; if (!ModelState.IsValid) { return(ReturnViewWithDateError(vm)); } var saved = BudgetService.EditBudget(vm); if (!saved) { return(PartialView("Error")); } if (isDone) { return(RedirectToAction("Index", new { budgetId = vm.Id })); } return(PartialView("EditBudget", vm)); }