refactor(app): implement year filter for account entries and update related services
This commit is contained in:
@@ -13,14 +13,20 @@ public class EntryService : IEntryService
|
||||
|
||||
public EntryService(IDbContextFactory<FinanceDbContext> dbFactory) => _dbFactory = dbFactory;
|
||||
|
||||
public async Task<List<EntryDto>> GetEntriesAsync(int accountId, bool currentYearOnly)
|
||||
public Task<List<EntryDto>> GetEntriesAsync(int accountId, bool currentYearOnly)
|
||||
{
|
||||
var year = currentYearOnly ? DateTime.Now.Year : (int?)null;
|
||||
return GetEntriesAsync(accountId, year);
|
||||
}
|
||||
|
||||
public async Task<List<EntryDto>> GetEntriesAsync(int accountId, int? year)
|
||||
{
|
||||
await using var db = await _dbFactory.CreateDbContextAsync();
|
||||
|
||||
var query = db.Entries.Where(e => e.AccountId == accountId);
|
||||
|
||||
if (currentYearOnly)
|
||||
query = query.Where(e => e.Date.Year == DateTime.Now.Year);
|
||||
if (year.HasValue)
|
||||
query = query.Where(e => e.Date.Year == year.Value);
|
||||
|
||||
var entries = await query
|
||||
.OrderBy(e => e.Date)
|
||||
@@ -59,6 +65,18 @@ public class EntryService : IEntryService
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<int>> GetEntryYearsAsync(int accountId)
|
||||
{
|
||||
await using var db = await _dbFactory.CreateDbContextAsync();
|
||||
|
||||
return await db.Entries
|
||||
.Where(e => e.AccountId == accountId)
|
||||
.Select(e => e.Date.Year)
|
||||
.Distinct()
|
||||
.OrderByDescending(y => y)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<EntryDto> CreateEntryAsync(int accountId, EntryType type, DateTime date, string title, decimal amount)
|
||||
{
|
||||
await using var db = await _dbFactory.CreateDbContextAsync();
|
||||
|
||||
Reference in New Issue
Block a user