Exemple #1
0
        public Buy Create(string email, string address, int portfolioId, int days, int?goalOptionId = null, int?timeframe = null,
                          int?risk = null, double?targetAmount = null, double?startingAmount = null, double?monthlyContribution = null)
        {
            if (string.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentException("Address must be filled.");
            }
            if (days <= 0)
            {
                throw new ArgumentException("Invalid purchase days.");
            }

            var user = UserBusiness.GetValidUser(email, address);

            if (user?.ConfirmationDate == null)
            {
                throw new ArgumentException("User didn't confirmed e-mail.");
            }
            var portfolio = PortfolioBusiness.GetWithDetails(portfolioId);

            if (portfolio == null || !portfolio.Detail.Enabled || !portfolio.Advisor.Detail.Enabled)
            {
                throw new ArgumentException("Invalid portfolio.");
            }
            if (portfolio.Advisor.UserId == user.Id)
            {
                throw new ArgumentException("User is the advisor owner.");
            }

            var purchases = ListPurchases(user.Id);

            if (purchases.Any(c => c.PortfolioId == portfolio.Id))
            {
                throw new ArgumentException("Portfolio already bought.");
            }

            Buy buy;

            using (var transaction = new TransactionalDapperCommand())
            {
                Goal goal = null;
                if (portfolio.Advisor.Type == AdvisorType.Robo.Value)
                {
                    if (!goalOptionId.HasValue || !timeframe.HasValue || !risk.HasValue || !startingAmount.HasValue || !monthlyContribution.HasValue)
                    {
                        throw new ArgumentException("Invalid goal data.");
                    }

                    goal = GoalBusiness.SetNew(user.Id, goalOptionId.Value, timeframe.Value, risk.Value, targetAmount, startingAmount.Value, monthlyContribution.Value);
                    transaction.Insert(goal);
                }
                var price = Math.Floor(portfolio.Detail.Price * (decimal)(1000000.0 * days) / (decimal)30.0) / (decimal)1000000.0;
                buy = SetNew(days, price, portfolio.Id, portfolio.ProjectionId.Value, portfolio.Detail.Id, user.Id, goal?.Id);
                transaction.Insert(buy);
                var trans = TransactionBusiness.SetNew(user.Id);
                transaction.Insert(trans);
                var buyTrans = BuyTransactionBusiness.SetNew(buy.Id, trans.Id);
                transaction.Insert(buyTrans);
                buy.Goal            = goal;
                buy.Portfolio       = portfolio;
                buy.PortfolioDetail = portfolio.Detail;
                transaction.Commit();
            }
            return(buy);
        }
Exemple #2
0
        //public async Task<User> FullRegister(string email, string password, int goalOptionId, int? timeframe, int risk, double? targetAmount, double? startingAmount, double? monthlyContribution)
        //{
        //    return await UserBusiness.FullRegister(email, password, goalOptionId, timeframe, risk, targetAmount, startingAmount, monthlyContribution);
        //}

        public Goal CreateGoal(string email, int goalOptionId, int timeframe, int risk, double?targetAmount, double startingAmount, double monthlyContribution)
        {
            return(GoalBusiness.Create(email, goalOptionId, timeframe, risk, targetAmount, startingAmount, monthlyContribution));
        }