namespace BudgetApp.Application.Services; using BudgetApp.Domain.Interfaces; using BudgetApp.Domain.Models; /// /// Application service for budget operations. /// public class BudgetService { private readonly IBudgetRepository _budgetRepository; public BudgetService(IBudgetRepository budgetRepository) { _budgetRepository = budgetRepository ?? throw new ArgumentNullException(nameof(budgetRepository)); } /// /// Creates a new base budget. /// public async Task CreateBaseBudgetAsync(string name, Money initialBalance) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Name cannot be null or empty.", nameof(name)); if (initialBalance == null) throw new ArgumentNullException(nameof(initialBalance)); var budget = new BaseBudget(name, initialBalance); await _budgetRepository.SaveAsync(budget); return budget; } /// /// Creates a new goal-based budget. /// public async Task CreateGoalBudgetAsync( string name, Money initialBalance, Money goalAmount, DateTime targetDate, Money periodicContribution, TimeSpan contributionPeriod) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Name cannot be null or empty.", nameof(name)); if (initialBalance == null) throw new ArgumentNullException(nameof(initialBalance)); if (goalAmount == null) throw new ArgumentNullException(nameof(goalAmount)); if (periodicContribution == null) throw new ArgumentNullException(nameof(periodicContribution)); var budget = new GoalBudget(name, initialBalance, goalAmount, targetDate, periodicContribution, contributionPeriod); await _budgetRepository.SaveAsync(budget); return budget; } /// /// Gets a budget by ID. /// public async Task GetBudgetByIdAsync(Guid budgetId) { return await _budgetRepository.GetByIdAsync(budgetId); } /// /// Gets all budgets. /// public async Task> GetAllBudgetsAsync() { return await _budgetRepository.GetAllAsync(); } /// /// Gets the balance of a specific budget. /// public async Task GetBudgetBalanceAsync(Guid budgetId) { var budget = await _budgetRepository.GetByIdAsync(budgetId); return budget?.Balance; } /// /// Calculates the total balance across all budgets for a specific currency. /// public async Task GetTotalBudgetBalanceAsync(string currency) { var budgets = await _budgetRepository.GetAllAsync(); var total = budgets .Where(b => b.Balance.Currency == currency) .Sum(b => b.Balance.Amount); return new Money(total, currency); } /// /// Deletes a budget. /// public async Task DeleteBudgetAsync(Guid budgetId) { await _budgetRepository.DeleteAsync(budgetId); } }