Esempio n. 1
0
        public (BudgetFile plannedBudgetFile, BudgetFile realBudgetFile) Create(YearOption yearOption, MonthOption monthOption,
                                                                                CurrencyOption currencyOption)
        {
            var month    = Month.Create(yearOption.Value, MonthName.Create(monthOption.Value));
            var currency = Currency.Create(currencyOption.Value);

            var budget = new Budget(month, currency);

            var realBudgetFileName    = new FileName($"budget-{budget.Month.Year}-{budget.Month.MonthName.Index}");
            var plannedBudgetFileName = new FileName($"budget-{budget.Month.Year}-{budget.Month.MonthName.Index}-plan");

            return(plannedBudgetFile : new BudgetFile(budget, plannedBudgetFileName),
                   realBudgetFile : new BudgetFile(budget, realBudgetFileName));
        }
Esempio n. 2
0
        private static int Run(string[] args)
        {
            var app    = new CommandLineApplication();
            var budget = app.Command("budget", config =>
            {
                config.HelpOption(HelpFlagTemplate.Value);
            });

            budget.Command("create", config =>
            {
                config.Name        = "create";
                config.Description = "create budget providing year, month and currency";
                config.HelpOption(HelpFlagTemplate.Value);

                var yearOption     = YearOption.Create(config);
                var monthOption    = MonthOption.Create(config);
                var currencyOption = CurrencyOption.Create(config);

                config.OnExecute(() =>
                {
                    if (yearOption.NotExists || monthOption.NotExists || currencyOption.NotExists)
                    {
                        config.ShowHelp();
                        return(ErrorCode.Value);
                    }

                    var budgetFilesFactory = new BudgetFilesFactory();
                    var(plannedBudgetFile, realBudgetFile) = budgetFilesFactory.Create(yearOption, monthOption, currencyOption);

                    if (plannedBudgetFile.NotExists || realBudgetFile.NotExists)
                    {
                        plannedBudgetFile.Save();
                        realBudgetFile.Save();

                        Console.WriteLine($"Budget {plannedBudgetFile.Budget} has been created.");
                        return(OkCode.Value);
                    }

                    Console.WriteLine($"Budget {plannedBudgetFile.Budget} already exists.");

                    return(OkCode.Value);
                });
            });

            budget.Command("add", config =>
            {
                config.Name        = "add";
                config.Description = "add revenue or expense to budget";
                config.HelpOption(HelpFlagTemplate.Value);

                var yearOption        = YearOption.Create(config);
                var monthOption       = MonthOption.Create(config);
                var currencyOption    = CurrencyOption.Create(config);
                var amountOption      = AmountOption.Create(config);
                var categoryOption    = CategoryOption.Create(config);
                var descriptionOption = DescriptionOption.Create(config);

                config.OnExecute(() =>
                {
                    if (yearOption.NotExists ||
                        monthOption.NotExists ||
                        currencyOption.NotExists ||
                        amountOption.NotExists ||
                        categoryOption.NotExists ||
                        descriptionOption.NotExists)
                    {
                        config.ShowHelp();
                        return(ErrorCode.Value);
                    }

                    var budgetFilesFactory = new BudgetFilesFactory();
                    var(plannedBudgetFile, realBudgetFile) = budgetFilesFactory.Create(yearOption, monthOption, currencyOption);

                    if (realBudgetFile.NotExists)
                    {
                        Console.WriteLine($"Budget {realBudgetFile.Budget} does not exit. Created new budget first.");
                        return(ErrorCode.Value);
                    }

                    var budgetFile = realBudgetFile.Load();

                    var category    = Category.Create(categoryOption.Value);
                    var description = new Description(descriptionOption.Value);
                    var currency    = Currency.Create(currencyOption.Value);
                    var amount      = Amount.Create(amountOption.Value, currency);

                    var newBudget     = budgetFile.Budget.WithExpense(new Money(amount, category, DateTimeOffset.UtcNow, description));
                    var newBudgetFile = new BudgetFile(newBudget, budgetFile.FileName);

                    newBudgetFile.Save();

                    Console.WriteLine("Expense added.");
                    return(OkCode.Value);
                });
            });


//            var budget = new BudgetCommandBuilder(app).Build();
//
//            new CreateBudgetCommandBuilder(budget).Build();
//            new BudgetsListCommandBuilder(budget).Build();
//            new AddCommandBuilder(budget).Build();

            return(app.Execute(args));
        }