public static void AddTokensBuying(TokensBuying item) { using (var ctx = new RentooloEntities()) { ctx.TokensBuying.Add(item); ctx.SaveChanges(); } }
protected void ButtonBuyTokens_Click(object sender, EventArgs e) { if (User == null) { Response.Redirect("/Account/Login?ReturnUrl=Tokens"); } string tokensCountBuyString = String.Format("{0}", Request.Form["ctl00$MainContent$tokensCountBuy"]); long tokensCountBuy = 0; try { tokensCountBuy = Int64.Parse(tokensCountBuyString); } catch { Result = "Wrong count"; return; } if (tokensCountBuy == 0) { Result = "Zero count"; return; } if (tokensCountBuy < 0) { Result = "Below zero count value"; return; } DateTime currentDate = DateTime.Now; int hoursCount = currentDate.Hour; double percentsPow = Math.Pow(1.00002897611, hoursCount); double currentHourValue = OneTokenTodayCost * percentsPow; int hoursCountPlus = hoursCount + 1; double percentsPowPlusHour = Math.Pow(1.00002897611, hoursCountPlus); double plusHourValue = OneTokenTodayCost * percentsPowPlusHour; double diffValuePlusHour = plusHourValue - currentHourValue; double secondValue = diffValuePlusHour / 3600; int secondsCount = currentDate.Minute * 60 + currentDate.Second; double diffValue = secondsCount * secondValue; double currentValue = currentHourValue + diffValue; double sum = tokensCountBuy * currentValue; Wallets userWallet = WalletsHelper.GetUserWallet(User.UserId, (int)CurrenciesEnum.RURT); if (userWallet == null || userWallet.Value < sum) { Result = "No balance"; return; } Model.TokensBuying tokensBuying = new Model.TokensBuying { UserId = User.UserId, CostOneToken = currentValue, Count = tokensCountBuy, FullCost = sum, WhenDate = DateTime.Now }; TokensDataHelper.AddTokensBuying(tokensBuying); WalletsHelper.UpdateUserWallet(User.UserId, (int)CurrenciesEnum.RURT, -sum); WalletsHelper.UpdateUserWallet(User.UserId, (int)CurrenciesEnum.RENT, tokensCountBuy); #region Логирование операции { Rentoolo.Model.Operations operation = new Rentoolo.Model.Operations { UserId = User.UserId, Value = tokensCountBuy, Type = (int)OperationTypesEnum.Registration, Comment = string.Format("Покупка {0} токенов на сумму {1}.", tokensCountBuy, sum), WhenDate = DateTime.Now }; DataHelper.AddOperation(operation); } #endregion TokensDataHelper.UpdateAvailableTokensCount(AvailableTokensCount - tokensCountBuy); Response.Redirect("Tokens"); }