コード例 #1
0
        //can this operation be done asyncrounously
        //maybe you should take this method to BudgetService
        public Dictionary <string, string> CheckBugdetLimit(string userId, int expenseCategoryId, DateTime date)
        {
            var category = _expenseCategoryRepository.GetById(expenseCategoryId);
            Dictionary <string, string> budgetDetails = new Dictionary <string, string>
            {
                { "BudgetStatus", "" },
                { "IsBudgetSet", "false" },
                { "IsBudgetBelowExpense", "true" },
                { "Category", $"{category?.Name}" },
                { "Month", $"{date.ToString("MMMM")}" },
                { "Year", $"{date.Year.ToString()}" },
                { "Total Expenses", "" },
                { "BudgetAmount", "" }
            };
            var expenses = _expenseRepository.FindExpensesByCategoryIdByMonth(userId, expenseCategoryId, date);

            var totalExpenseCost = expenses.Sum(x => x.CostOfExpense);

            var budget = _budgetRepository.GetByCategory(userId, expenseCategoryId, date.ToString("MMMM"), date.Year.ToString());

            if (budget != null)
            {
                //come back to this later to fix the extra values you added to the dictionary
                budgetDetails["IsBudgetSet"] = "true";
                string budgetedAmount = budget?.Amount.ToString("0,0.00");
                if (totalExpenseCost > budget?.Amount)
                {
                    budgetDetails["IsBudgetBelowExpense"] = "false";
                    string amountExceeded = (totalExpenseCost - budget.Amount).ToString("0,0.00");
                    budgetDetails["BudgetStatus"] = $"Your expenses for {date.ToString("MMMM yyyy")} has exceeded your " +
                                                    $"budget of \u20A6{budgetedAmount} for {category?.Name} category" +
                                                    $" by \u20A6{amountExceeded} ";
                }
                else if (budget?.Amount == totalExpenseCost)
                {
                    budgetDetails["IsBudgetBelowExpense"] = "false";
                    budgetDetails["BudgetStatus"]         = $"Your expenses for {date.ToString("MMMM yyyy")} has met your budget" +
                                                            $" of \u20A6{budgetedAmount} for {category?.Name} category";
                }
                else
                {
                    //set to true already at initialization
                    //budgetDetails["IsBudgetBelowExpense"] = "true";
                    string amountRemaining = (budget.Amount - totalExpenseCost).ToString("0,0.00");
                    budgetDetails["BudgetStatus"] = $"You have \u20A6{amountRemaining} remaining of your budgeted \u20A6{budgetedAmount} for {category?.Name} in {date.ToString("MMMM yyyy")}.";
                }
            }

            return(budgetDetails);
        }