Add infrastructure layer with persistence and services
- EF Core DbContext and entity configurations - Design-time factory for migrations - Initial and soft-delete migrations - Service implementations: Account, AccountYear, Entry, BalanceQuery, FileSave, PdfStatement - Dependency injection registration
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
using Duempelkas.App.Services;
|
||||
using Duempelkas.App.Services.Models;
|
||||
using Duempelkas.Domain.Enums;
|
||||
using Duempelkas.Infrastructure.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Duempelkas.Infrastructure.Services;
|
||||
|
||||
public class BalanceQueryService : IBalanceQueryService
|
||||
{
|
||||
private readonly FinanceDbContext _db;
|
||||
|
||||
public BalanceQueryService(FinanceDbContext db) => _db = db;
|
||||
|
||||
public async Task<AccountBalanceDto> GetAccountBalanceAsync(int accountId)
|
||||
{
|
||||
var account = await _db.Accounts
|
||||
.Include(a => a.Entries)
|
||||
.FirstOrDefaultAsync(a => a.Id == accountId)
|
||||
?? throw new InvalidOperationException($"Konto {accountId} nicht gefunden.");
|
||||
|
||||
var currentYear = DateTime.Now.Year;
|
||||
|
||||
var activeEntries = account.Entries.Where(e => !e.IsDeleted).ToList();
|
||||
var totalIncome = activeEntries.Where(e => e.Type == EntryType.Income).Sum(e => e.Amount);
|
||||
var totalExpense = activeEntries.Where(e => e.Type == EntryType.Expense).Sum(e => e.Amount);
|
||||
var currentYearIncome = activeEntries.Where(e => e.Type == EntryType.Income && e.Date.Year == currentYear).Sum(e => e.Amount);
|
||||
var currentYearExpense = activeEntries.Where(e => e.Type == EntryType.Expense && e.Date.Year == currentYear).Sum(e => e.Amount);
|
||||
var totalBalance = account.CarryoverBalance + totalIncome - totalExpense;
|
||||
|
||||
return new AccountBalanceDto(
|
||||
account.CarryoverBalance,
|
||||
totalIncome,
|
||||
totalExpense,
|
||||
currentYearIncome,
|
||||
currentYearExpense,
|
||||
totalBalance);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user