chore: update store types name (#1862)

This commit is contained in:
boojack 2023-06-29 22:55:03 +08:00 committed by GitHub
parent 7226a9ad47
commit 6a78887f1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 85 additions and 54 deletions

View File

@ -179,7 +179,7 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) {
// Change the default role to host if there is no host user.
userCreate.Role = store.Host
} else {
allowSignUpSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSettingMessage{
allowSignUpSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
Name: SystemSettingAllowSignUpName.String(),
})
if err != nil && common.ErrorCode(err) != common.NotFound {

View File

@ -38,7 +38,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}
if createMemoRequest.Visibility == "" {
userMemoVisibilitySetting, err := s.Store.GetUserSetting(ctx, &store.FindUserSettingMessage{
userMemoVisibilitySetting, err := s.Store.GetUserSetting(ctx, &store.FindUserSetting{
UserID: &userID,
Key: apiv1.UserSettingMemoVisibilityKey.String(),
})

View File

@ -39,13 +39,13 @@ func (t *telegramHandler) MessageHandle(ctx context.Context, bot *telegram.Bot,
}
var creatorID int
userSettingMessageList, err := t.store.ListUserSettings(ctx, &store.FindUserSettingMessage{
userSettingList, err := t.store.ListUserSettings(ctx, &store.FindUserSetting{
Key: apiv1.UserSettingTelegramUserIDKey.String(),
})
if err != nil {
return errors.Wrap(err, "Failed to find userSettingList")
}
for _, userSetting := range userSettingMessageList {
for _, userSetting := range userSettingList {
var value string
if err := json.Unmarshal([]byte(userSetting.Value), &value); err != nil {
continue

View File

@ -94,7 +94,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
}
userSettingUpsert.UserID = userID
userSettingMessage, err := s.Store.UpsertUserSettingV1(ctx, &store.UserSettingMessage{
userSetting, err := s.Store.UpsertUserSetting(ctx, &store.UserSetting{
UserID: userID,
Key: userSettingUpsert.Key.String(),
Value: userSettingUpsert.Value,
@ -102,8 +102,8 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert user setting").SetInternal(err)
}
userSetting := convertUserSettingFromStore(userSettingMessage)
return c.JSON(http.StatusOK, composeResponse(userSetting))
userSettingMessage := convertUserSettingFromStore(userSetting)
return c.JSON(http.StatusOK, composeResponse(userSettingMessage))
})
// GET /api/user/me is used to check if the user is logged in.
@ -122,19 +122,19 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
}
userSettingMessageList, err := s.Store.ListUserSettings(ctx, &store.FindUserSettingMessage{
list, err := s.Store.ListUserSettings(ctx, &store.FindUserSetting{
UserID: &userID,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find userSettingList").SetInternal(err)
}
userSettingList := []*api.UserSetting{}
for _, userSettingMessage := range userSettingMessageList {
userSettingV1 := convertUserSettingFromStore(userSettingMessage)
for _, item := range list {
userSetting := convertUserSettingFromStore(item)
userSettingList = append(userSettingList, &api.UserSetting{
UserID: userSettingV1.UserID,
Key: api.UserSettingKey(userSettingV1.Key),
Value: userSettingV1.Value,
UserID: userSetting.UserID,
Key: api.UserSettingKey(userSetting.Key),
Value: userSetting.Value,
})
}
user.UserSettingList = userSettingList
@ -218,19 +218,19 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch user").SetInternal(err)
}
userSettingMessageList, err := s.Store.ListUserSettings(ctx, &store.FindUserSettingMessage{
list, err := s.Store.ListUserSettings(ctx, &store.FindUserSetting{
UserID: &userID,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find userSettingList").SetInternal(err)
}
userSettingList := []*api.UserSetting{}
for _, userSettingMessage := range userSettingMessageList {
userSettingV1 := convertUserSettingFromStore(userSettingMessage)
for _, item := range list {
userSetting := convertUserSettingFromStore(item)
userSettingList = append(userSettingList, &api.UserSetting{
UserID: userSettingV1.UserID,
Key: api.UserSettingKey(userSettingV1.Key),
Value: userSettingV1.Value,
UserID: userSetting.UserID,
Key: api.UserSettingKey(userSetting.Key),
Value: userSetting.Value,
})
}
user.UserSettingList = userSettingList
@ -297,7 +297,7 @@ func (s *Server) createUserCreateActivity(c echo.Context, user *api.User) error
return err
}
func convertUserSettingFromStore(userSetting *store.UserSettingMessage) *apiv1.UserSetting {
func convertUserSettingFromStore(userSetting *store.UserSetting) *apiv1.UserSetting {
return &apiv1.UserSetting{
UserID: userSetting.UserID,
Key: apiv1.UserSettingKey(userSetting.Key),

View File

@ -14,7 +14,7 @@ type Store struct {
db *sql.DB
systemSettingCache sync.Map // map[string]*systemSettingRaw
userCache sync.Map // map[int]*userRaw
userSettingCache sync.Map // map[string]*UserSettingMessage
userSettingCache sync.Map // map[string]*UserSetting
shortcutCache sync.Map // map[int]*shortcutRaw
idpCache sync.Map // map[int]*IdentityProvider
resourceCache sync.Map // map[int]*resourceRaw

View File

@ -10,17 +10,17 @@ import (
"github.com/usememos/memos/common"
)
type SystemSettingMessage struct {
type SystemSetting struct {
Name string
Value string
Description string
}
type FindSystemSettingMessage struct {
type FindSystemSetting struct {
Name string
}
func (s *Store) ListSystemSettings(ctx context.Context, find *FindSystemSettingMessage) ([]*SystemSettingMessage, error) {
func (s *Store) ListSystemSettings(ctx context.Context, find *FindSystemSetting) ([]*SystemSetting, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@ -38,10 +38,10 @@ func (s *Store) ListSystemSettings(ctx context.Context, find *FindSystemSettingM
return list, nil
}
func (s *Store) GetSystemSetting(ctx context.Context, find *FindSystemSettingMessage) (*SystemSettingMessage, error) {
func (s *Store) GetSystemSetting(ctx context.Context, find *FindSystemSetting) (*SystemSetting, error) {
if find.Name != "" {
if cache, ok := s.systemSettingCache.Load(find.Name); ok {
return cache.(*SystemSettingMessage), nil
return cache.(*SystemSetting), nil
}
}
@ -65,7 +65,7 @@ func (s *Store) GetSystemSetting(ctx context.Context, find *FindSystemSettingMes
return systemSettingMessage, nil
}
func listSystemSettings(ctx context.Context, tx *sql.Tx, find *FindSystemSettingMessage) ([]*SystemSettingMessage, error) {
func listSystemSettings(ctx context.Context, tx *sql.Tx, find *FindSystemSetting) ([]*SystemSetting, error) {
where, args := []string{"1 = 1"}, []any{}
if find.Name != "" {
where, args = append(where, "name = ?"), append(args, find.Name)
@ -85,9 +85,9 @@ func listSystemSettings(ctx context.Context, tx *sql.Tx, find *FindSystemSetting
}
defer rows.Close()
list := []*SystemSettingMessage{}
list := []*SystemSetting{}
for rows.Next() {
systemSettingMessage := &SystemSettingMessage{}
systemSettingMessage := &SystemSetting{}
if err := rows.Scan(
&systemSettingMessage.Name,
&systemSettingMessage.Value,

View File

@ -6,18 +6,18 @@ import (
"strings"
)
type UserSettingMessage struct {
type UserSetting struct {
UserID int
Key string
Value string
}
type FindUserSettingMessage struct {
type FindUserSetting struct {
UserID *int
Key string
}
func (s *Store) UpsertUserSettingV1(ctx context.Context, upsert *UserSettingMessage) (*UserSettingMessage, error) {
func (s *Store) UpsertUserSetting(ctx context.Context, upsert *UserSetting) (*UserSetting, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@ -40,12 +40,12 @@ func (s *Store) UpsertUserSettingV1(ctx context.Context, upsert *UserSettingMess
return nil, err
}
userSettingMessage := upsert
s.userSettingCache.Store(getUserSettingCacheKeyV1(userSettingMessage.UserID, userSettingMessage.Key), userSettingMessage)
return userSettingMessage, nil
userSetting := upsert
s.userSettingCache.Store(getUserSettingCacheKeyV1(userSetting.UserID, userSetting.Key), userSetting)
return userSetting, nil
}
func (s *Store) ListUserSettings(ctx context.Context, find *FindUserSettingMessage) ([]*UserSettingMessage, error) {
func (s *Store) ListUserSettings(ctx context.Context, find *FindUserSetting) ([]*UserSetting, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@ -63,10 +63,10 @@ func (s *Store) ListUserSettings(ctx context.Context, find *FindUserSettingMessa
return userSettingList, nil
}
func (s *Store) GetUserSetting(ctx context.Context, find *FindUserSettingMessage) (*UserSettingMessage, error) {
func (s *Store) GetUserSetting(ctx context.Context, find *FindUserSetting) (*UserSetting, error) {
if find.UserID != nil {
if cache, ok := s.userSettingCache.Load(getUserSettingCacheKeyV1(*find.UserID, find.Key)); ok {
return cache.(*UserSettingMessage), nil
return cache.(*UserSetting), nil
}
}
@ -84,12 +84,12 @@ func (s *Store) GetUserSetting(ctx context.Context, find *FindUserSettingMessage
if len(list) == 0 {
return nil, nil
}
userSettingMessage := list[0]
s.userSettingCache.Store(getUserSettingCacheKeyV1(userSettingMessage.UserID, userSettingMessage.Key), userSettingMessage)
return userSettingMessage, nil
userSetting := list[0]
s.userSettingCache.Store(getUserSettingCacheKeyV1(userSetting.UserID, userSetting.Key), userSetting)
return userSetting, nil
}
func listUserSettings(ctx context.Context, tx *sql.Tx, find *FindUserSettingMessage) ([]*UserSettingMessage, error) {
func listUserSettings(ctx context.Context, tx *sql.Tx, find *FindUserSetting) ([]*UserSetting, error) {
where, args := []string{"1 = 1"}, []any{}
if v := find.Key; v != "" {
@ -112,24 +112,24 @@ func listUserSettings(ctx context.Context, tx *sql.Tx, find *FindUserSettingMess
}
defer rows.Close()
userSettingMessageList := make([]*UserSettingMessage, 0)
userSettingList := make([]*UserSetting, 0)
for rows.Next() {
var userSettingMessage UserSettingMessage
var userSetting UserSetting
if err := rows.Scan(
&userSettingMessage.UserID,
&userSettingMessage.Key,
&userSettingMessage.Value,
&userSetting.UserID,
&userSetting.Key,
&userSetting.Value,
); err != nil {
return nil, FormatError(err)
}
userSettingMessageList = append(userSettingMessageList, &userSettingMessage)
userSettingList = append(userSettingList, &userSetting)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
return userSettingMessageList, nil
return userSettingList, nil
}
func vacuumUserSetting(ctx context.Context, tx *sql.Tx) error {

View File

@ -7,29 +7,33 @@ import (
"github.com/stretchr/testify/require"
"github.com/usememos/memos/api"
"github.com/usememos/memos/store"
)
func TestSystemSettingStore(t *testing.T) {
ctx := context.Background()
store := NewTestingStore(ctx, t)
_, err := store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
ts := NewTestingStore(ctx, t)
_, err := ts.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: api.SystemSettingServerIDName,
Value: "test_server_id",
})
require.NoError(t, err)
_, err = store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
_, err = ts.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: api.SystemSettingSecretSessionName,
Value: "test_secret_session_name",
})
require.NoError(t, err)
_, err = store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
_, err = ts.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: api.SystemSettingAllowSignUpName,
Value: "true",
})
require.NoError(t, err)
_, err = store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
_, err = ts.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: api.SystemSettingLocalStoragePathName,
Value: "/tmp/memos",
})
require.NoError(t, err)
list, err := ts.ListSystemSettings(ctx, &store.FindSystemSetting{})
require.NoError(t, err)
require.Equal(t, 4, len(list))
}

View File

@ -0,0 +1,27 @@
package teststore
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/store"
)
func TestUserSettingStore(t *testing.T) {
ctx := context.Background()
ts := NewTestingStore(ctx, t)
user, err := createTestingHostUser(ctx, ts)
require.NoError(t, err)
_, err = ts.UpsertUserSetting(ctx, &store.UserSetting{
UserID: user.ID,
Key: "test_key",
Value: "test_value",
})
require.NoError(t, err)
list, err := ts.ListUserSettings(ctx, &store.FindUserSetting{})
require.NoError(t, err)
require.Equal(t, 1, len(list))
require.Equal(t, "test_key", list[0].Key)
require.Equal(t, "test_value", list[0].Value)
}