Пример #1
0
        public TransactionsController(
            IGetTransactions getTransactions,
            IGetTransactionById getTransactionById,
            IUpsertTransaction upsertTransaction,
            IDeleteTransaction deleteTransaction,
            Func <Try <Email>, string?, CreateTransactionModel, Task <ValidationResult> > validate)
        {
            this.getTransactions    = getTransactions;
            this.getTransactionById = getTransactionById;
            this.upsertTransaction  = upsertTransaction;
            this.deleteTransaction  = deleteTransaction;

            this.validate = validate;
        }
        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.");
        }