2022-08-13 09:35:33 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2022-08-18 19:00:47 +03:00
|
|
|
"strings"
|
2022-08-13 09:35:33 +03:00
|
|
|
)
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
type UserSetting struct {
|
2022-08-13 09:35:33 +03:00
|
|
|
UserID int
|
2023-06-26 18:06:53 +03:00
|
|
|
Key string
|
2022-08-13 09:35:33 +03:00
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
type FindUserSetting struct {
|
2023-06-26 18:06:53 +03:00
|
|
|
UserID *int
|
|
|
|
Key string
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
func (s *Store) UpsertUserSetting(ctx context.Context, upsert *UserSetting) (*UserSetting, error) {
|
2022-08-13 09:35:33 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
query := `
|
|
|
|
INSERT INTO user_setting (
|
|
|
|
user_id, key, value
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
ON CONFLICT(user_id, key) DO UPDATE
|
|
|
|
SET value = EXCLUDED.value
|
|
|
|
`
|
|
|
|
if _, err := tx.ExecContext(ctx, query, upsert.UserID, upsert.Key, upsert.Value); err != nil {
|
2022-08-13 09:35:33 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
userSetting := upsert
|
2023-07-02 13:56:25 +03:00
|
|
|
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserID, userSetting.Key), userSetting)
|
2023-06-29 17:55:03 +03:00
|
|
|
return userSetting, nil
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
func (s *Store) ListUserSettings(ctx context.Context, find *FindUserSetting) ([]*UserSetting, error) {
|
2022-08-13 09:35:33 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
userSettingList, err := listUserSettings(ctx, tx, find)
|
2022-08-13 09:35:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
for _, userSetting := range userSettingList {
|
2023-07-02 13:56:25 +03:00
|
|
|
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserID, userSetting.Key), userSetting)
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
2023-06-26 18:06:53 +03:00
|
|
|
return userSettingList, nil
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
func (s *Store) GetUserSetting(ctx context.Context, find *FindUserSetting) (*UserSetting, error) {
|
2023-06-26 18:06:53 +03:00
|
|
|
if find.UserID != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
if cache, ok := s.userSettingCache.Load(getUserSettingCacheKey(*find.UserID, find.Key)); ok {
|
2023-06-29 17:55:03 +03:00
|
|
|
return cache.(*UserSetting), nil
|
2023-02-11 09:19:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 19:00:47 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-18 19:00:47 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
list, err := listUserSettings(ctx, tx, find)
|
2022-08-18 19:00:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-07-02 13:56:25 +03:00
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
userSetting := list[0]
|
2023-07-02 13:56:25 +03:00
|
|
|
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserID, userSetting.Key), userSetting)
|
2023-06-29 17:55:03 +03:00
|
|
|
return userSetting, nil
|
2022-08-18 19:00:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
func listUserSettings(ctx context.Context, tx *sql.Tx, find *FindUserSetting) ([]*UserSetting, error) {
|
2023-03-18 17:34:22 +03:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2022-08-18 19:00:47 +03:00
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
if v := find.Key; v != "" {
|
2023-02-27 17:16:33 +03:00
|
|
|
where, args = append(where, "key = ?"), append(args, v)
|
2022-08-18 19:00:47 +03:00
|
|
|
}
|
2023-05-26 04:43:51 +03:00
|
|
|
if v := find.UserID; v != nil {
|
|
|
|
where, args = append(where, "user_id = ?"), append(args, *find.UserID)
|
|
|
|
}
|
2022-08-18 19:00:47 +03:00
|
|
|
|
2022-08-13 09:35:33 +03:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
user_id,
|
|
|
|
key,
|
|
|
|
value
|
|
|
|
FROM user_setting
|
2022-08-18 19:00:47 +03:00
|
|
|
WHERE ` + strings.Join(where, " AND ")
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
2022-08-13 09:35:33 +03:00
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
userSettingList := make([]*UserSetting, 0)
|
2022-08-13 09:35:33 +03:00
|
|
|
for rows.Next() {
|
2023-06-29 17:55:03 +03:00
|
|
|
var userSetting UserSetting
|
2022-08-13 09:35:33 +03:00
|
|
|
if err := rows.Scan(
|
2023-06-29 17:55:03 +03:00
|
|
|
&userSetting.UserID,
|
|
|
|
&userSetting.Key,
|
|
|
|
&userSetting.Value,
|
2022-08-13 09:35:33 +03:00
|
|
|
); err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
2023-06-29 17:55:03 +03:00
|
|
|
userSettingList = append(userSettingList, &userSetting)
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
return userSettingList, nil
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
2022-11-06 07:21:58 +03:00
|
|
|
|
|
|
|
func vacuumUserSetting(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
stmt := `
|
|
|
|
DELETE FROM
|
|
|
|
user_setting
|
|
|
|
WHERE
|
|
|
|
user_id NOT IN (
|
|
|
|
SELECT
|
|
|
|
id
|
|
|
|
FROM
|
|
|
|
user
|
|
|
|
)`
|
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return err
|
2022-11-06 07:21:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|