public UserSettings GetUserSettings(Guid userId)
        {
            var found = _cache.TryGetValue($"{userId}:settings", out UserSettings userSettings);

            if (!found)
            {
                var query = DatabaseQueryMaker.CreateSelectUserSettings(userId);
                userSettings = DatabaseQueryExecutor.Query <UserSettings>(_connectionString, query);

                if (userSettings == null)
                {
                    throw new Exception("user not found");
                }

                _cache.Set($"{userId}:settings",
                           userSettings,
                           new MemoryCacheEntryOptions()
                           .SetSlidingExpiration(TimeSpan.FromHours(1)));

                return(userSettings);
            }
            else
            {
                return(userSettings);
            }
        }
        public void SaveUserSettings(UserSettings userSettings)
        {
            // Last minute feature/hack.
            // Sounds familiar ?
            var alias = userSettings.Alias;

            if (alias.Length < 5 || !Regex.IsMatch(alias, "^[a-zA-Z][a-zA-Z0-9]*$"))
            {
                alias = AliasGenerator.New();
            }

            var userId               = userSettings.UserId;
            var selectQuery          = DatabaseQueryMaker.CreateSelectUserSettings(userId);
            var selectedUserSettings = DatabaseQueryExecutor.Query <UserSettings>(_connectionString, selectQuery);

            if (selectedUserSettings != null)
            {
                var updateCommand = DatabaseQueryMaker.CreateUpdateUserSettings(userId,
                                                                                alias,
                                                                                selectedUserSettings.SendNewsletter);
                DatabaseQueryExecutor.Update <UserSettings>(_connectionString, updateCommand);
            }
            else
            {
                var insertCommand = DatabaseQueryMaker.CreateInsertUserSettings(userId,
                                                                                alias,
                                                                                userSettings.SendNewsletter);
                DatabaseQueryExecutor.Insert <UserSettings>(_connectionString, insertCommand);
            }
        }
コード例 #3
0
        public virtual void Update(UserSettings item)
        {
            var updateCommand = DatabaseQueryMaker.CreateUpdateUserSettings(item.UserId,
                                                                            item.Alias,
                                                                            item.SendNewsletter);

            DatabaseQueryExecutor.Update <UserSettings>(_connectionString, updateCommand);
        }
コード例 #4
0
        public virtual void Add(UserSettings item)
        {
            var insertCommand = DatabaseQueryMaker.CreateInsertUserSettings(item.UserId,
                                                                            item.Alias,
                                                                            item.SendNewsletter);

            DatabaseQueryExecutor.Insert <UserSettings>(_connectionString, insertCommand);
        }
コード例 #5
0
        public UserSettings Get(Guid id)
        {
            var query = DatabaseQueryMaker.CreateSelectUserSettings(id);

            return(DatabaseQueryExecutor.Query <UserSettings>(_connectionString, query));
        }