From 8dfae8a6aa80541eb7d4b72ef387e009ced82bc0 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 23 Jun 2023 00:54:54 +0800 Subject: [PATCH] fix: variable type in store --- api/v1/shortcut.go | 35 +++++++++++++++++++---------------- store/db/migration_history.go | 4 ++-- store/shortcut.go | 12 +++++------- store/user.go | 4 ++-- store/user_setting.go | 4 ++-- 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/api/v1/shortcut.go b/api/v1/shortcut.go index a09ee4d..e0f6429 100644 --- a/api/v1/shortcut.go +++ b/api/v1/shortcut.go @@ -127,13 +127,19 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err) } - shortcut, err = s.Store.UpdateShortcut(ctx, &store.UpdateShortcut{ - ID: shortcutID, - RowStatus: (*store.RowStatus)(patch.RowStatus), - Name: patch.Name, - Link: patch.Link, - Visibility: (*store.Visibility)(patch.Visibility), - }) + shortcutUpdate := &store.UpdateShortcut{ + ID: shortcutID, + Name: patch.Name, + Link: patch.Link, + Description: patch.Description, + } + if patch.RowStatus != nil { + shortcutUpdate.RowStatus = (*store.RowStatus)(patch.RowStatus) + } + if patch.Visibility != nil { + shortcutUpdate.Visibility = (*store.Visibility)(patch.Visibility) + } + shortcut, err = s.Store.UpdateShortcut(ctx, shortcutUpdate) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err) } @@ -227,16 +233,13 @@ func (s *APIV1Service) composeShortcut(ctx context.Context, shortcut *Shortcut) return nil, nil } - if shortcut.CreatorID != 0 { - user, err := s.Store.GetUser(ctx, &store.FindUser{ - ID: &shortcut.CreatorID, - }) - if err != nil { - return nil, err - } - shortcut.Creator = convertUserFromStore(user) + user, err := s.Store.GetUser(ctx, &store.FindUser{ + ID: &shortcut.CreatorID, + }) + if err != nil { + return nil, err } - + shortcut.Creator = convertUserFromStore(user) return shortcut, nil } diff --git a/store/db/migration_history.go b/store/db/migration_history.go index cbda344..0c0e4db 100644 --- a/store/db/migration_history.go +++ b/store/db/migration_history.go @@ -106,7 +106,7 @@ func upsertMigrationHistory(ctx context.Context, tx *sql.Tx, upsert *MigrationHi version=EXCLUDED.version RETURNING version, created_ts ` - var migrationHistory MigrationHistory + migrationHistory := &MigrationHistory{} if err := tx.QueryRowContext(ctx, query, upsert.Version).Scan( &migrationHistory.Version, &migrationHistory.CreatedTs, @@ -114,5 +114,5 @@ func upsertMigrationHistory(ctx context.Context, tx *sql.Tx, upsert *MigrationHi return nil, err } - return &migrationHistory, nil + return migrationHistory, nil } diff --git a/store/shortcut.go b/store/shortcut.go index 54d56d7..3c0e02b 100644 --- a/store/shortcut.go +++ b/store/shortcut.go @@ -111,9 +111,8 @@ func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Sh defer tx.Rollback() set, args := []string{}, []any{} - if update.RowStatus != nil { - set, args = append(set, "row_status = ?"), append(args, *update.RowStatus) + set, args = append(set, "row_status = ?"), append(args, update.RowStatus.String()) } if update.Name != nil { set, args = append(set, "name = ?"), append(args, *update.Name) @@ -125,7 +124,7 @@ func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Sh set, args = append(set, "description = ?"), append(args, *update.Description) } if update.Visibility != nil { - set, args = append(set, "visibility = ?"), append(args, *update.Visibility) + set, args = append(set, "visibility = ?"), append(args, update.Visibility.String()) } if len(set) == 0 { return nil, fmt.Errorf("no update specified") @@ -140,7 +139,7 @@ func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Sh id = ? RETURNING id, creator_id, created_ts, updated_ts, row_status, name, link, description, visibility ` - var shortcut *Shortcut + shortcut := &Shortcut{} if err := tx.QueryRowContext(ctx, query, args...).Scan( &shortcut.ID, &shortcut.CreatorID, @@ -275,7 +274,7 @@ func listShortcuts(ctx context.Context, tx *sql.Tx, find *FindShortcut) ([]*Shor list := make([]*Shortcut, 0) for rows.Next() { - var shortcut Shortcut + shortcut := &Shortcut{} if err := rows.Scan( &shortcut.ID, &shortcut.CreatorID, @@ -289,8 +288,7 @@ func listShortcuts(ctx context.Context, tx *sql.Tx, find *FindShortcut) ([]*Shor ); err != nil { return nil, err } - - list = append(list, &shortcut) + list = append(list, shortcut) } if err := rows.Err(); err != nil { diff --git a/store/user.go b/store/user.go index 50cb4ed..a2b7120 100644 --- a/store/user.go +++ b/store/user.go @@ -272,7 +272,7 @@ func listUsers(ctx context.Context, tx *sql.Tx, find *FindUser) ([]*User, error) list := make([]*User, 0) for rows.Next() { - user := User{} + user := &User{} if err := rows.Scan( &user.ID, &user.CreatedTs, @@ -286,7 +286,7 @@ func listUsers(ctx context.Context, tx *sql.Tx, find *FindUser) ([]*User, error) ); err != nil { return nil, err } - list = append(list, &user) + list = append(list, user) } if err := rows.Err(); err != nil { diff --git a/store/user_setting.go b/store/user_setting.go index 51bd690..f21b60b 100644 --- a/store/user_setting.go +++ b/store/user_setting.go @@ -115,7 +115,7 @@ func listUserSettings(ctx context.Context, tx *sql.Tx, find *FindUserSetting) ([ userSettingMessageList := make([]*UserSetting, 0) for rows.Next() { - var userSettingMessage UserSetting + userSettingMessage := &UserSetting{} if err := rows.Scan( &userSettingMessage.UserID, &userSettingMessage.Key, @@ -123,7 +123,7 @@ func listUserSettings(ctx context.Context, tx *sql.Tx, find *FindUserSetting) ([ ); err != nil { return nil, err } - userSettingMessageList = append(userSettingMessageList, &userSettingMessage) + userSettingMessageList = append(userSettingMessageList, userSettingMessage) } if err := rows.Err(); err != nil {