Ejemplo n.º 1
0
 private Amount TotalOutgo() => Amount.FromDecimal(Outgos.Sum(i => i.Amount));
Ejemplo n.º 2
0
        protected override void When(object @event)
        {
            Income      income;
            Outgo       outgo;
            Snapshot    snapshot;
            Expenditure expenditure;

            switch (@event)
            {
            case Events.DailyBudgetCreated e:
                Id       = new DailyBudgetId(e.Id);
                Name     = new BudgetName(e.Name);
                snapshot = new Snapshot(Apply);
                ApplyToEntity(snapshot, e);
                Snapshot = snapshot;
                break;

            case Events.IncomeAddedToDailyBudget e:
                income = new Income(Apply);
                ApplyToEntity(income, e);
                Incomes.Add(income);
                e.TotalIncome = TotalIncome();
                ApplyToEntity(Snapshot, e);
                break;

            case Events.IncomeAmountChanged e:
                income = Incomes.FirstOrDefault(i => i.Id == e.IncomeId);
                if (income == null)
                {
                    throw new InvalidOperationException($"Income with id {e.IncomeId} not found");
                }
                ApplyToEntity(income, e);
                e.TotalIncome = TotalIncome();
                ApplyToEntity(Snapshot, e);
                break;

            case Events.IncomeDescriptionChanged e:
                income = Incomes.FirstOrDefault(i => i.Id == e.IncomeId);
                if (income == null)
                {
                    throw new InvalidOperationException($"Income with id {e.IncomeId} not found");
                }
                ApplyToEntity(income, e);
                break;

            case Events.IncomeRemoved e:
                income = Incomes.FirstOrDefault(i => i.Id == e.IncomeId);
                if (income == null)
                {
                    throw new InvalidOperationException($"Income with id {e.IncomeId} not found");
                }
                Incomes.Remove(income);
                e.TotalIncome = TotalIncome();
                ApplyToEntity(Snapshot, e);
                break;

            case Events.OutgoAddedToDailyBudget e:
                outgo = new Outgo(Apply);
                ApplyToEntity(outgo, e);
                Outgos.Add(outgo);
                e.TotalOutgo = TotalOutgo();
                ApplyToEntity(Snapshot, e);
                break;

            case Events.OutgoAmountChanged e:
                outgo = Outgos.FirstOrDefault(i => i.Id == e.OutgoId);
                if (outgo == null)
                {
                    throw new InvalidOperationException($"Outgo with id {e.OutgoId} not found");
                }
                ApplyToEntity(outgo, e);
                e.TotalOutgo = TotalOutgo();
                ApplyToEntity(Snapshot, e);
                break;

            case Events.OutgoDescriptionChanged e:
                outgo = Outgos.FirstOrDefault(i => i.Id == e.OutgoId);
                if (outgo == null)
                {
                    throw new InvalidOperationException($"Outgo with id {e.OutgoId} not found");
                }
                ApplyToEntity(outgo, e);
                break;

            case Events.OutgoRemoved e:
                outgo = Outgos.FirstOrDefault(o => o.Id == e.OutgoId);
                if (outgo == null)
                {
                    throw new InvalidOperationException($"Outgo with id {e.OutgoId} not found");
                }
                Outgos.Remove(outgo);
                e.TotalOutgo = TotalOutgo();
                ApplyToEntity(Snapshot, e);
                break;

            case Events.ExpenditureAdded e:
                expenditure = new Expenditure(Apply);
                ApplyToEntity(expenditure, e);
                Expenditures.Add(expenditure);
                e.TotalExpenditure = TotalExpenditure();
                ApplyToEntity(Snapshot, e);
                break;

            case Events.ExpenditureAmountChanged e:
                expenditure = Expenditures.FirstOrDefault(exp => exp.Id == e.ExpenditureId);
                if (expenditure == null)
                {
                    throw new InvalidOperationException($"Expenditure with id {e.ExpenditureId} not found");
                }
                ApplyToEntity(expenditure, e);
                e.TotalExpenditure = TotalExpenditure();
                ApplyToEntity(Snapshot, e);
                break;

            case Events.PeriodAddedToDailyBudget e:
                if (Period != null)
                {
                    throw new InvalidOperationException($"Period has already been set. Update start or end");
                }
                Period = Period.Create(e.Start, e.End);
                ApplyToEntity(Snapshot, e);
                break;

            case Events.PeriodStartChanged e:
                Period = Period.Create(e.Start, Period.ToB);
                ApplyToEntity(Snapshot, e);
                break;

            case Events.PeriodEndChanged e:
                Period = Period.Create(Period.FromA, e.End);
                ApplyToEntity(Snapshot, e);
                break;
            }
        }