public IEnumerable <ExpenseListItem> GetExpenses() { using (var context = new ApplicationDbContext()) { var expenses = context.Expenses.Where(e => e.UserId == _userId).ToList(); var expenseList = new List <ExpenseListItem>(); foreach (var entity in expenses) { // getting Frequency string frequency; switch (entity.ExpenseFreqType) { case ExpenseFreqType.ByWeek: if (entity.FrequencyFactor == 1) { frequency = "Weekly"; } else { frequency = $"Every {entity.FrequencyFactor} weeks"; } break; case ExpenseFreqType.ByMonth: if (entity.FrequencyFactor == 1) { frequency = "Monthly"; } else { frequency = $"Every {entity.FrequencyFactor} months"; } break; default: frequency = "Once"; break; } // getting NextDueDate DueDate[] datesArray = entity.DueDates.OrderBy(d => d.Date).ToArray(); DateTime nextDueDate = (datesArray.FirstOrDefault(d => d.Date >= DateTime.Now)).Date; var expenseListItem = new ExpenseListItem { Id = entity.Id, CategoryType = entity.Category.Type.ToString(), CategoryName = entity.Category.Name, Amount = entity.Amount, Frequency = frequency, NextDueDate = nextDueDate.ToString("D") }; expenseList.Add(expenseListItem); } return(expenseList.OrderBy(e => e.CategoryType)); } }
public async Task <MonthlyBudgetBoardDetail> GetMonthlyBudgetBoardById(int id) { //Get Budget Board Entity Data var board = await _midasContext.MonthlyBudgetBoard.FirstOrDefaultAsync(mb => mb.MonthlyBudgetId == id); //Query the Transaction table to see what Monthly Expenses are associated with a Budget Board var budgetExpenses = await _midasContext.MonthlyBudgetExpense.Where(be => be.MonthlyBudgetBoardId == id).ToArrayAsync(); //New list to store monthly expense data for mapping to the DTO var expenses = new List <ExpenseListItem>(); foreach (MonthlyBudgetExpense entity in budgetExpenses) { //Query the Expense Table to get table data var expense = _midasContext.Expense.FirstOrDefault(e => e.ExpenseId == entity.ExpenseId); var expenseType = _midasContext.ExpenseType.FirstOrDefault(e => e.ExpenseTypeId == expense.ExpenseTypeId); var newExpense = new ExpenseListItem { ExpenseId = expense.ExpenseId, ExpenseName = expense.ExpenseName, ExpenseAmount = expense.ExpenseAmount, ExpenseTypeId = expense.ExpenseTypeId, ExpenseTypeName = expenseType.ExpenseTypeName, ExpenseDate = expense.ExpenseDate }; expenses.Add(newExpense); } var dto = new MonthlyBudgetBoardDetail { MonthlyBudgetBoardId = board.MonthlyBudgetId, BudgetBoardId = board.BudgetBoardId, MonthlyBudgetBoardName = board.MonthlyBudgetName, ExpenseList = expenses }; return(dto); }