internal CreateBudgetModelValidator(
            Email email,
            string?id,
            IGetBudgets getBudgets,
            IGetCategoryByName getCategoryByName,
            IGetCurrencyByCode getCurrencyByCode)
        {
            this.RuleFor(model => model)
            .MustAsync(async(budget, _) =>
            {
                if (budget?.Recurrence == null)
                {
                    return(true);
                }

                var entities = await getBudgets.GetResult(NewGetByAllParam(
                                                              email,
                                                              NewFilterBuilder()
                                                              .Where("year".Equal(budget.Recurrence.Start.Year)
                                                                     .And("month".Equal(budget.Recurrence.Start.Month))
                                                                     .And("day".Equal(budget.Recurrence.Start.Day))
                                                                     .And("category".Equal(budget.Category)))
                                                              .Build()));

                return(entities
                       .Fold(true)(page => page.Data.All(@try => @try
                                                         .Fold(true)(item => id != default && item.Id.Equals(new Guid(id))))));
            })
            .WithMessage("Budget already exists.");

            this.RuleFor(model => model.Description)
            .NotEmpty()
            .WithMessage("Description is required.");

            this.RuleFor(model => model.Limit)
            .NotNull()
            .WithMessage("Limit is required.")
            .SetValidator(new MoneyModelValidator(email, getCurrencyByCode));

            this.RuleFor(model => model.Recurrence)
            .NotNull()
            .WithMessage("Recurrence is required.")
            .SetValidator(new RecurrenceModelValidator());

            this.RuleFor(model => model.Category)
            .NotEmpty()
            .WithMessage("Category is required.")
            .MustAsync(async(category, _) => await getCategoryByName.GetResult(NewGetByIdParam(email, category.Name)))
            .WithMessage("Category not found.");
        }
Example #2
0
        internal CreateCategoryModelValidator(
            Email email,
            string category,
            IGetCategoryByName getCategoryByName)
        {
            this.RuleFor(model => model.Name)
            .NotEmpty()
            .WithMessage("Category name is required.")
            .MustAsync(async(name, _) => await getCategoryByName.GetResult(NewGetByIdParam(email, category)) || category.Equals(name))
            .WithMessage("Path category must be equal to body category.");

            this.RuleFor(model => model.Icon)
            .NotEmpty()
            .WithMessage("Category icon is required.");
        }
Example #3
0
 public PayeesController(
     IGetPayees getPayees,
     IGetPayeesByCategory getPayeesByCategory,
     IGetPayeeByName getPayeeByName,
     IUpsertPayee upsertPayee,
     IDeletePayee deletePayee,
     IGetCategoryByName getCategoryByName,
     Func <Try <Email>, string, CreatePayeeModel, Task <ValidationResult> > validate)
 {
     this.getPayees           = getPayees;
     this.getPayeesByCategory = getPayeesByCategory;
     this.getPayeeByName      = getPayeeByName;
     this.upsertPayee         = upsertPayee;
     this.deletePayee         = deletePayee;
     this.getCategoryByName   = getCategoryByName;
     this.validate            = validate;
 }
        internal CreateConfigurationModelValidator(
            Email email,
            IGetCategoryByName getCategoryByName,
            IGetCurrencyByCode getCurrencyByCode,
            IGetPayeeByName getPayeeByName,
            IGetPaymentMethodByName getPaymentMethodByName)
        {
            this.RuleFor(model => model.Type)
            .NotEmpty()
            .WithMessage("Type is required.");

            this.RuleFor(model => model.PaymentMethod)
            .NotEmpty()
            .WithMessage("Payment method is required.")
            .MustAsync(async(method, _) => await getPaymentMethodByName.GetResult(NewGetByIdParam(email, method)))
            .WithMessage("Payment method not found.");

            this.RuleFor(model => model.Currency)
            .NotNull()
            .WithMessage("Currency is required.")
            .SetValidator(new CurrencyModelValidator(email, getCurrencyByCode));

            this.RuleFor(model => model.Payee)
            .NotEmpty()
            .WithMessage("Payee is required.")
            .MustAsync(async(payee, _) => await getPayeeByName.GetResult(NewGetByIdParam(email, payee.Name)))
            .WithMessage("Payee not found.");

            this.RuleFor(model => model.Category)
            .NotEmpty()
            .WithMessage("Category is required.")
            .MustAsync(async(category, _) => await getCategoryByName.GetResult(NewGetByIdParam(email, category.Name)))
            .WithMessage("Category not found.");

            this.RuleFor(model => model.Tags)
            .NotNull()
            .NotEmpty()
            .WithMessage("Must be at least one tag for configuration.");
        }
        internal CreateTransactionModelValidator(
            Email email,
            string?id,
            IGetCategoryByName getCategoryByName,
            IGetCurrencyByCode getCurrencyByCode,
            IGetPayeeByName getPayeeByName,
            IGetPaymentMethodByName getPaymentMethodByName,
            IGetTransactions getTransactions)
        {
            this.RuleFor(model => model)
            .MustAsync(async(transaction, _) =>
            {
                if (transaction?.Payment?.Cost == null)
                {
                    return(true);
                }

                var entities = await getTransactions.GetResult(NewGetByAllParam(
                                                                   email,
                                                                   NewFilterBuilder()
                                                                   .Where("year".Equal(transaction.Payment.Date.Year)
                                                                          .And("month".Equal(transaction.Payment.Date.Month))
                                                                          .And("day".Equal(transaction.Payment.Date.Day))
                                                                          .And("value".Equal(transaction.Payment.Cost.Value.ToString(GetCultureInfo("en-US"))))
                                                                          .And("category".Equal(transaction.Category))
                                                                          .And("currency".Equal(transaction.Payment.Cost.Currency.Code))
                                                                          .And("payee".Equal(transaction.Payee.Name))
                                                                          .And("paymentMethod".Equal(transaction.Payment.Method)))
                                                                   .Build()));

                return(entities
                       .Fold(true)(page => page.Data.All(@try => @try
                                                         .Fold(true)(item => id != default && item.Id.Equals(new Guid(id))))));
            })
            .WithMessage("Transaction already exists.");

            this.RuleFor(model => model.Type)
            .NotEmpty()
            .WithMessage("Type is required.");

            this.RuleFor(model => model.Payment)
            .NotNull()
            .WithMessage("Payment is required.")
            .SetValidator(new PaymentModelValidator(email, getCurrencyByCode, getPaymentMethodByName));

            this.RuleFor(model => model.Installment)
            .SetValidator(new CreateInstallmentModelValidator());

            this.RuleFor(model => model.Payee)
            .NotEmpty()
            .WithMessage("Payee is required.")
            .MustAsync(async(payee, _) => await getPayeeByName.GetResult(NewGetByIdParam(email, payee.Name)))
            .WithMessage("Payee not found.");

            this.RuleFor(model => model.Category)
            .NotEmpty()
            .WithMessage("Category is required.")
            .MustAsync(async(category, _) => await getCategoryByName.GetResult(NewGetByIdParam(email, category.Name)))
            .WithMessage("Category not found.");

            this.RuleFor(model => model.Tags)
            .NotNull()
            .NotEmpty()
            .WithMessage("Must be at least one tag for transaction.");
        }