private void InternalCreate(TransactionalDapperCommand transaction, int userId, int buyId, string transactionHash) { var trans = TransactionBusiness.SetNew(userId, transactionHash); transaction.Insert(trans); var buyTrans = BuyTransactionBusiness.SetNew(buyId, trans.Id); transaction.Insert(buyTrans); }
public void Cancel(string email, int buyId) { var user = UserBusiness.GetValidUser(email); var buy = BuyBusiness.Get(buyId); if (buy == null || buy.UserId != user.Id || buy.ExpirationDate.HasValue) { throw new ArgumentException("Invalid purchase."); } if (buy.LastTransaction.TransactionStatus == TransactionStatus.Pending.Value) { if (string.IsNullOrEmpty(buy.LastTransaction.TransactionHash)) { TransactionBusiness.Process(buy.LastTransaction, TransactionStatus.Cancel); } else { try { var transaction = Web3.Web3Business.CheckTransaction(buy.LastTransaction.TransactionHash); throw new ArgumentException(transaction.BlockNumber.HasValue ? "The transaction is already minted." : "Pending transaction cannot be canceled."); } catch (DomainObjects.Web3.Web3Exception ex) { if (ex.Code == 404) { TransactionBusiness.Process(buy.LastTransaction, TransactionStatus.Cancel); } else { throw; } } } } else if (buy.LastTransaction.TransactionStatus == TransactionStatus.Error.Value) { using (var transaction = new TransactionalDapperCommand()) { var trans = TransactionBusiness.SetCanceled(user.Id); transaction.Insert(trans); var buyTrans = BuyTransactionBusiness.SetNew(buyId, trans.Id); transaction.Insert(buyTrans); transaction.Commit(); } } else { throw new ArgumentException("Invalid transaction status."); } }
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); }