Example #1
0
 public PaymentMethodsController(
     IGetPaymentMethods getPaymentMethods,
     IGetPaymentMethodsByPayee getPaymentMethodsByPayee,
     IGetPaymentMethodByName getPaymentMethodByName,
     IUpsertPaymentMethod upsertPaymentMethod,
     IDeletePaymentMethod deletePaymentMethod,
     IGetPayeeByName getPayeeByName,
     Func <Try <Email>, string, CreatePaymentMethodModel, Task <ValidationResult> > validate)
 {
     this.getPaymentMethods        = getPaymentMethods;
     this.getPaymentMethodsByPayee = getPaymentMethodsByPayee;
     this.getPaymentMethodByName   = getPaymentMethodByName;
     this.upsertPaymentMethod      = upsertPaymentMethod;
     this.deletePaymentMethod      = deletePaymentMethod;
     this.getPayeeByName           = getPayeeByName;
     this.validate = validate;
 }
        internal PaymentModelValidator(
            Email email,
            IGetCurrencyByCode getCurrencyByCode,
            IGetPaymentMethodByName getPaymentMethodByName)
        {
            this.RuleFor(model => model.Cost)
            .NotNull()
            .WithMessage("Cost is required.")
            .SetValidator(new MoneyModelValidator(email, getCurrencyByCode));

            this.RuleFor(model => model.Date)
            .NotEqual(default(DateTime))
            .WithMessage("Payment date is required.");

            this.RuleFor(model => model.Method)
            .NotEmpty()
            .WithMessage("Payment method is required.")
            .MustAsync(async(method, _) => await getPaymentMethodByName.GetResult(NewGetByIdParam(email, method)))
            .WithMessage("Payment method not found.");
        }
        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.");
        }
 internal CreatePaymentMethodModelValidator(
     Email email,
     string paymentMethod,
     IGetPaymentMethodByName getPaymentMethodByName) => this.RuleFor(model => model.Name)