Beispiel #1
0
        public async Task <BudgetBoardDetailDTO> GetBudgetBoardById(int budgetBoardId)
        {
            //Get Budget Board Entity Data
            var budgetBoard = await _midasContext.BudgetBoard.FirstOrDefaultAsync(bb => bb.BudgetBoardId == budgetBoardId);

            //Query the Transaction table to see what Monthly Expenses are associated with a Budget Board
            var budgetBoardMonthlyExpenses = await _midasContext.BudgetBoardMonthlyExpense.Where(bbme => bbme.BudgetBoardId == budgetBoardId).ToArrayAsync();

            //New list to store monthly expense data for mapping to the DTO
            var monthlyExpenses = new List <MonthlyExpense>();

            foreach (BudgetBoardMonthlyExpense entity in budgetBoardMonthlyExpenses)
            {
                //Query the Monthly Expense Table to get table data
                var expense = _midasContext.MonthlyExpense.FirstOrDefault(me => me.MonthlyExpenseId == entity.MonthlyExpenseId);

                var newExpense = new MonthlyExpense
                {
                    MonthlyExpenseId = expense.MonthlyExpenseId,
                    BillName         = expense.BillName,
                    BillAmount       = expense.BillAmount
                };

                monthlyExpenses.Add(newExpense);
            }

            var dto = new BudgetBoardDetailDTO
            {
                BudgetBoardId   = budgetBoard.BudgetBoardId,
                BudgetBoardName = budgetBoard.BudgetBoardName,
                LivingAmount    = budgetBoard.LivingAmount,
                SavingsAmount   = budgetBoard.SavingsAmount,
                LeisureAmount   = budgetBoard.LeisureAmount,
                MonthlyExpenses = monthlyExpenses
            };

            return(dto);
        }
        public async Task <IActionResult> AddExpense(BudgetBoardDetailDTO request)
        {
            try
            {
                // TODO: Add insert logic here

                var expense = new MonthlyExpenseModel
                {
                    BillName   = request.MonthlyExpenseModel.BillName,
                    BillAmount = request.MonthlyExpenseModel.BillAmount
                };

                var expenseId = await _monthlyExpenseService.AddMonthlyExpenseToBudgetBoard(expense);

                var result = await _budgetBoardService.BoardExpenseTransaction(request.BudgetBoardId, expenseId);

                return(RedirectToAction("Details", new { id = request.BudgetBoardId }));
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
                return(View(request));
            }
        }