Skip to content

Commit

Permalink
fix: 更换Token时不刷新share的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
nianhua99 committed Jun 6, 2024
1 parent ed44904 commit ab4a89b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
9 changes: 9 additions & 0 deletions internal/repository/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ShareRepository interface {
SearchShare(ctx context.Context, email string, uniqueName string) ([]*model.Share, error)
DeleteShare(ctx context.Context, id int64) error
GetShareByUniqueName(ctx context.Context, uniqueName string) (*model.Share, error)
GetSharesByAccountId(ctx context.Context, accountId int) ([]*model.Share, error)
}

func NewShareRepository(
Expand All @@ -26,6 +27,14 @@ type shareRepository struct {
*Repository
}

func (r *shareRepository) GetSharesByAccountId(ctx context.Context, accountId int) ([]*model.Share, error) {
var shares []*model.Share
if err := r.DB(ctx).Where("account_id = ?", accountId).Find(&shares).Error; err != nil {
return nil, err
}
return shares, nil
}

func (r *shareRepository) GetShareByUniqueName(ctx context.Context, uniqueName string) (*model.Share, error) {
var share model.Share
if err := r.DB(ctx).Where("unique_name = ?", uniqueName).First(&share).Error; err != nil {
Expand Down
9 changes: 7 additions & 2 deletions internal/service/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *accountService) RefreshAccount(ctx context.Context, id int64) error {
return err
}
// 刷新此Account的所有ShareToken
shares, err := s.shareService.SearchShare(ctx, account.Email, "")
shares, err := s.shareService.GetSharesByAccountId(ctx, int(account.ID))
if err != nil {
return err
}
Expand All @@ -90,7 +90,12 @@ func (s *accountService) RefreshAccount(ctx context.Context, id int64) error {
}

func (s *accountService) Update(ctx context.Context, account *model.Account) error {
err := s.accountRepository.Update(ctx, account)
// 刷新所有share
err := s.RefreshAccount(ctx, int64(account.ID))
if err != nil {
return err
}
err = s.accountRepository.Update(ctx, account)
if err != nil {
return err
}
Expand Down
14 changes: 9 additions & 5 deletions internal/service/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"PandoraHelper/internal/repository"
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
"github.com/spf13/viper"
"go.uber.org/zap"
Expand All @@ -24,8 +23,9 @@ type ShareService interface {
SearchShare(ctx context.Context, email string, uniqueName string) ([]*model.Share, error)
DeleteShare(ctx context.Context, id int64) error
LoginShareByPassword(ctx context.Context, username string, password string) (string, error)
ShareStatistic(ctx *gin.Context, accountId int) (interface{}, interface{})
ShareResetPassword(ctx *gin.Context, uniqueName string, password string, newPassword string, confirmNewPassword string) error
ShareStatistic(ctx context.Context, accountId int) (interface{}, interface{})
ShareResetPassword(ctx context.Context, uniqueName string, password string, newPassword string, confirmNewPassword string) error
GetSharesByAccountId(ctx context.Context, accountId int) ([]*model.Share, error)
}

func NewShareService(service *Service, shareRepository repository.ShareRepository, viper *viper.Viper, coordinator *Coordinator) ShareService {
Expand All @@ -44,7 +44,11 @@ type shareService struct {
accountService AccountService
}

func (s *shareService) ShareResetPassword(ctx *gin.Context, uniqueName string, password string, newPassword string, confirmNewPassword string) error {
func (s *shareService) GetSharesByAccountId(ctx context.Context, accountId int) ([]*model.Share, error) {
return s.shareRepository.GetSharesByAccountId(ctx, accountId)
}

func (s *shareService) ShareResetPassword(ctx context.Context, uniqueName string, password string, newPassword string, confirmNewPassword string) error {
share, err := s.shareRepository.GetShareByUniqueName(ctx, uniqueName)
if err != nil {
return err
Expand All @@ -64,7 +68,7 @@ func (s *shareService) ShareResetPassword(ctx *gin.Context, uniqueName string, p
}

// ShareStatistic 转换为Go语言
func (s *shareService) ShareStatistic(ctx *gin.Context, accountId int) (interface{}, interface{}) {
func (s *shareService) ShareStatistic(ctx context.Context, accountId int) (interface{}, interface{}) {
account, err := s.accountService.GetAccount(ctx, int64(accountId))
if err != nil {
return nil, err
Expand Down

0 comments on commit ab4a89b

Please sign in to comment.