private byte[] SerializeObject(UserStatisticSettings setting)
 {
     var serializer = new DataContractSerializer(typeof(UserStatisticSettings));
     using (var stream = new MemoryStream())
     {
         using (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream))
         {
             serializer.WriteObject(writer, setting);
         }
         return stream.ToArray();
     }
 }
 public void UpdateUserStatisticSettings(UserStatisticSettings settings)
 {
     using (var ctx = new forexBox2Entities())
     {
         var entity = ctx.StatisticConfigurations.FirstOrDefault(x => x.UserId == settings.UserId);
         if (entity != null)
         {
             var data = SerializeObject(settings);
             entity.Data = data;
             ctx.SaveChanges();
         }
     }
 }
        public UserStatisticSettings GetAccountSettings(int accountId)
        {
            var settings = new UserStatisticSettings();

             var accountRepository = new AccountRepository();
             var account = accountRepository.GetAccountById(accountId);

             if (account != null)
             {
            var userRepository = new UsersRepository();
            var user = userRepository.GetUserById(account.UserId);

            settings = GetOrCreateUserSettings(user.LoweredUserName);
             }

             return settings;
        }
        public bool AddUserStatisticSettings(UserStatisticSettings settings)
        {
            using (var ctx = new forexBox2Entities())
            {
                var data = SerializeObject(settings);
                ctx.StatisticConfigurations.AddObject(new StatisticConfiguration
                {
                    Data = data,
                    UserId = settings.UserId,
                });

                try
                {
                    ctx.SaveChanges();
                }
                catch { return false; }
            }

            return true;
        }
        public UserStatisticSettings GetOrCreateUserSettings(string loginName)
        {
            UserStatisticSettings settings = new UserStatisticSettings();
             var userRepository = new UsersRepository();
             var user = userRepository.GetUserByLoginName(loginName);

             if (user != null)
             {
            var settingsRepository = new StatisticConfigurationRepository();
            settings = settingsRepository.GetUserStatisticSettingsByUserId(user.UserId);

            if (settings == null)
            {
               var newSettings = new UserStatisticSettings();
               newSettings.UserId = user.UserId;

               bool isSuccess = settingsRepository.AddUserStatisticSettings(newSettings);
               settings = newSettings;
            }

             }
             return settings;
        }
 public void UpdateUserSettings(UserStatisticSettings settings)
 {
     StatisticConfigurationRepository repository = new StatisticConfigurationRepository();
      repository.UpdateUserStatisticSettings(settings);
 }
            public ActionResult UpdateSettings(UserStatisticSettings settings)
            {
            StatsManager statsManager = new StatsManager();
            statsManager.UpdateUserSettings(settings);
            ViewData["IsPostSuccess"] = true;

            return InitiateAccountView();
            }