public IActionResult Update(int id)
        {
            var dto = Db.Get <MonthlyBudget>(id);

            var model = new UpdateMonthlyBudgetViewModel {
                ID          = dto.ID,
                AccountID   = dto.AccountID,
                AccountName = AccountName(accountID: dto.AccountID),
                StartDate   = dto.StartDate,
                EndDate     = dto.EndDate,
                Categories  = Categories(accountID: dto.AccountID, monthlyBudgetID: dto.ID)
            };

            return(View(model));
        }
        public IActionResult Update(UpdateMonthlyBudgetViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.AccountName = AccountName(accountID: model.AccountID);
                model.Categories  = Categories(accountID: model.AccountID, monthlyBudgetID: model.ID);

                return(View(model));
            }

            var dto = Db.Get <MonthlyBudget>(model.ID);

            var updated = dto.WithUpdates(
                startDate: model.StartDate.WithZeroedTime(),
                endDate: model.EndDate.SetTime(23, 59, 59)
                );

            Db.InsertOrUpdate(updated);

            Db.Execute((conn, tran) => conn.Execute("DELETE FROM Categories_MonthlyBudgets WHERE MonthlyBudgetID = @ID", new { model.ID }, tran));

            var categories = model.Categories.Where(c => c.Amount != 0).Select(c => new Category_MonthlyBudget(
                                                                                   monthlyBudgetId: model.ID,
                                                                                   categoryId: c.CategoryID,
                                                                                   amount: c.Amount < 0 ? c.Amount : -c.Amount
                                                                                   ));

            foreach (var category in categories)
            {
                Db.Execute((conn, tran) => conn.Insert(category, tran));
            }

            UnitOfWork.CommitChanges();

            return(RedirectToAction(nameof(Index), new { id = model.AccountID }));
        }