From 6a78887f1d5890ad22e1d5413b4c655eead9c1f4 Mon Sep 17 00:00:00 2001 From: boojack Date: Thu, 29 Jun 2023 22:55:03 +0800 Subject: [PATCH] chore: update store types name (#1862) --- api/v1/auth.go | 2 +- server/memo.go | 2 +- server/telegram.go | 4 ++-- server/user.go | 32 ++++++++++++------------- store/store.go | 2 +- store/system_setting.go | 16 ++++++------- store/user_setting.go | 40 +++++++++++++++---------------- test/store/system_setting_test.go | 14 +++++++---- test/store/user_setting_test.go | 27 +++++++++++++++++++++ 9 files changed, 85 insertions(+), 54 deletions(-) create mode 100644 test/store/user_setting_test.go diff --git a/api/v1/auth.go b/api/v1/auth.go index 310e9a6f..11e881d0 100644 --- a/api/v1/auth.go +++ b/api/v1/auth.go @@ -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 { diff --git a/server/memo.go b/server/memo.go index da6c04f7..3477ef85 100644 --- a/server/memo.go +++ b/server/memo.go @@ -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(), }) diff --git a/server/telegram.go b/server/telegram.go index 5fe95089..cfa20801 100644 --- a/server/telegram.go +++ b/server/telegram.go @@ -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 diff --git a/server/user.go b/server/user.go index 894c08cd..d45c18c7 100644 --- a/server/user.go +++ b/server/user.go @@ -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), diff --git a/store/store.go b/store/store.go index c21049d5..26e579c6 100644 --- a/store/store.go +++ b/store/store.go @@ -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 diff --git a/store/system_setting.go b/store/system_setting.go index 080baf11..94117963 100644 --- a/store/system_setting.go +++ b/store/system_setting.go @@ -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, diff --git a/store/user_setting.go b/store/user_setting.go index 85cd4ecc..19ed6443 100644 --- a/store/user_setting.go +++ b/store/user_setting.go @@ -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 { diff --git a/test/store/system_setting_test.go b/test/store/system_setting_test.go index 6def7814..6dd02224 100644 --- a/test/store/system_setting_test.go +++ b/test/store/system_setting_test.go @@ -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)) } diff --git a/test/store/user_setting_test.go b/test/store/user_setting_test.go new file mode 100644 index 00000000..69bb13d6 --- /dev/null +++ b/test/store/user_setting_test.go @@ -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) +}