public static void AddTokensSelling(TokensSelling item) { using (var ctx = new RentooloEntities()) { ctx.TokensSelling.Add(item); ctx.SaveChanges(); } }
protected void ButtonSellTokens_Click(object sender, EventArgs e) { if (User == null) { Response.Redirect("/Account/Login?ReturnUrl=Tokens"); } string tokensCountSellString = String.Format("{0}", Request.Form["ctl00$MainContent$tokensCountSell"]); long tokensCountSell = 0; try { tokensCountSell = Int64.Parse(tokensCountSellString); } catch { Result = "Wrong count"; return; } if (tokensCountSell == 0) { Result = "Zero tokens"; return; } if (tokensCountSell < 0) { Result = "Below zero tokens 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 = tokensCountSell * currentValue; Wallets userWalletRent = WalletsHelper.GetUserWallet(User.UserId, (int)CurrenciesEnum.RENT); if (userWalletRent.Value < tokensCountSell) { Result = "No tokens"; return; } Model.TokensSelling tokensSelling = new Model.TokensSelling { UserId = User.UserId, CostOneToken = currentValue, Count = tokensCountSell, FullCost = sum, WhenDate = DateTime.Now }; TokensDataHelper.AddTokensSelling(tokensSelling); WalletsHelper.UpdateUserWallet(User.UserId, (int)CurrenciesEnum.RURT, sum); WalletsHelper.UpdateUserWallet(User.UserId, (int)CurrenciesEnum.RENT, -tokensCountSell); #region Логирование операции { Rentoolo.Model.Operations operation = new Rentoolo.Model.Operations { UserId = User.UserId, Value = tokensCountSell, Type = (int)OperationTypesEnum.Registration, Comment = string.Format("Продажа {0} токенов на сумму {1}.", tokensCountSell, sum), WhenDate = DateTime.Now }; DataHelper.AddOperation(operation); } #endregion TokensDataHelper.UpdateAvailableTokensCount(AvailableTokensCount + tokensCountSell); Response.Redirect("Tokens"); }