memos/store/user_setting.go

154 lines
3.1 KiB
Go
Raw Normal View History

package store
import (
"context"
"database/sql"
"strings"
)
2023-06-29 17:55:03 +03:00
type UserSetting struct {
UserID int
Key string
Value string
}
2023-06-29 17:55:03 +03:00
type FindUserSetting struct {
UserID *int
Key string
}
2023-06-29 17:55:03 +03:00
func (s *Store) UpsertUserSetting(ctx context.Context, upsert *UserSetting) (*UserSetting, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
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 {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
2023-06-29 17:55:03 +03:00
userSetting := upsert
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserID, userSetting.Key), userSetting)
2023-06-29 17:55:03 +03:00
return userSetting, nil
}
2023-06-29 17:55:03 +03:00
func (s *Store) ListUserSettings(ctx context.Context, find *FindUserSetting) ([]*UserSetting, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
userSettingList, err := listUserSettings(ctx, tx, find)
if err != nil {
return nil, err
}
for _, userSetting := range userSettingList {
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserID, userSetting.Key), userSetting)
}
return userSettingList, nil
}
2023-06-29 17:55:03 +03:00
func (s *Store) GetUserSetting(ctx context.Context, find *FindUserSetting) (*UserSetting, error) {
if find.UserID != nil {
if cache, ok := s.userSettingCache.Load(getUserSettingCacheKey(*find.UserID, find.Key)); ok {
2023-06-29 17:55:03 +03:00
return cache.(*UserSetting), nil
}
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
list, err := listUserSettings(ctx, tx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
2023-06-29 17:55:03 +03:00
userSetting := list[0]
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserID, userSetting.Key), userSetting)
2023-06-29 17:55:03 +03:00
return userSetting, nil
}
2023-06-29 17:55:03 +03:00
func listUserSettings(ctx context.Context, tx *sql.Tx, find *FindUserSetting) ([]*UserSetting, error) {
where, args := []string{"1 = 1"}, []any{}
if v := find.Key; v != "" {
where, args = append(where, "key = ?"), append(args, v)
}
if v := find.UserID; v != nil {
where, args = append(where, "user_id = ?"), append(args, *find.UserID)
}
query := `
SELECT
user_id,
key,
value
FROM user_setting
WHERE ` + strings.Join(where, " AND ")
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
2023-06-29 17:55:03 +03:00
userSettingList := make([]*UserSetting, 0)
for rows.Next() {
2023-06-29 17:55:03 +03:00
var userSetting UserSetting
if err := rows.Scan(
2023-06-29 17:55:03 +03:00
&userSetting.UserID,
&userSetting.Key,
&userSetting.Value,
); err != nil {
return nil, err
}
2023-06-29 17:55:03 +03:00
userSettingList = append(userSettingList, &userSetting)
}
if err := rows.Err(); err != nil {
return nil, err
}
2023-06-29 17:55:03 +03:00
return userSettingList, nil
}
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 {
return err
2022-11-06 07:21:58 +03:00
}
return nil
}