Refactor. Add data layer
This commit is contained in:
1 parent
3b7e4e6ec5
commit
b6b794f0b4
82 files changed
+2930
-2811
No files matched your search
@@ -1,27 +1,25 @@
|
||||
using BudgetApp.Data;
|
||||
using BudgetApp.Data.Entities;
|
||||
using BudgetApp.Data.Interfaces;
|
||||
using BudgetApp.Services.Models;
|
||||
|
||||
namespace BudgetApp.Services;
|
||||
|
||||
public class DashboardService(TransactionService transactionService, AccountService accountService, CategoryService categoryService)
|
||||
public class DashboardService(ITransactionRepository transactionRepository, IAccountRepository accountRepository, ICategoryRepository categoryRepository)
|
||||
{
|
||||
private readonly TransactionService _transactionService = transactionService;
|
||||
private readonly AccountService _accountService = accountService;
|
||||
private readonly CategoryService _categoryService = categoryService;
|
||||
|
||||
public async Task<DashboardData> GetDashboardDataAsync(string userId, DateTime? from = null, DateTime? to = null)
|
||||
{
|
||||
await _categoryService.EnsureDefaultCategoriesAsync(userId);
|
||||
await categoryRepository.EnsureDefaultCategoriesAsync(userId);
|
||||
var windowEnd = to ?? DateTime.Today.AddDays(30);
|
||||
await _transactionService.MaterializeRecurringTransactionsAsync(userId, windowEnd);
|
||||
await transactionRepository.MaterializeRecurringTransactionsAsync(userId, windowEnd);
|
||||
|
||||
var accounts = await _accountService.GetAccountsAsync(userId);
|
||||
var categories = await _categoryService.GetCategoriesAsync(userId);
|
||||
var accounts = await accountRepository.GetAccountsAsync(userId);
|
||||
var categories = await categoryRepository.GetCategoriesAsync(userId);
|
||||
|
||||
var windowStart = from ?? DateTime.Today.AddDays(-30);
|
||||
|
||||
var fetchStart = windowStart < DateTime.Today ? windowStart : DateTime.Today;
|
||||
var fetchEnd = windowEnd > DateTime.Today ? windowEnd : DateTime.Today;
|
||||
var allTransactions = await _transactionService.GetTransactionsInRangeAsync(userId, fetchStart, fetchEnd);
|
||||
var allTransactions = await transactionRepository.GetTransactionsInRangeAsync(userId, fetchStart, fetchEnd);
|
||||
|
||||
var recent = allTransactions
|
||||
.Where(t => (t.Date.Date >= windowStart.Date && t.Date.Date <= DateTime.Today)
|
||||
@@ -99,10 +97,7 @@ public class DashboardService(TransactionService transactionService, AccountServ
|
||||
for (var d = startDate; d <= endDate; d = d.AddDays(1))
|
||||
{
|
||||
var dayTxns = accountTransactions.Where(t => t.Date.Date == d.Date && !(t.IsPaid && t.Date.Date > today));
|
||||
foreach (var t in dayTxns)
|
||||
{
|
||||
runningBalance += t.Type == TransactionType.Income ? t.Amount : -t.Amount;
|
||||
}
|
||||
runningBalance += dayTxns.Sum(t => t.Type == TransactionType.Income ? t.Amount : -t.Amount);
|
||||
dataPoints.Add(runningBalance);
|
||||
}
|
||||
|
||||
@@ -122,29 +117,4 @@ public class DashboardService(TransactionService transactionService, AccountServ
|
||||
DayTransactionLabels = dayTransactionLabels
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class DashboardData
|
||||
{
|
||||
public List<Account> Accounts { get; set; } = [];
|
||||
public List<Category> Categories { get; set; } = [];
|
||||
public List<Transaction> RecentTransactions { get; set; } = [];
|
||||
public List<Transaction> UpcomingTransactions { get; set; } = [];
|
||||
public ChartData ChartData { get; set; } = new();
|
||||
public string CurrencySymbol { get; set; } = "$";
|
||||
}
|
||||
|
||||
public class ChartData
|
||||
{
|
||||
public List<string> Labels { get; set; } = [];
|
||||
public List<ChartDataset> Datasets { get; set; } = [];
|
||||
public List<List<string>> DayTransactionLabels { get; set; } = [];
|
||||
}
|
||||
|
||||
public class ChartDataset
|
||||
{
|
||||
public string Label { get; set; } = string.Empty;
|
||||
public string Color { get; set; } = "#1b6ec2";
|
||||
public List<decimal> Data { get; set; } = [];
|
||||
public string CurrencySymbol { get; set; } = "$";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user