Skip to content

Commit

Permalink
fix: fix onboarding ysk card only init once (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Nov 4, 2024
1 parent 875c74e commit efadf51
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 59 deletions.
113 changes: 57 additions & 56 deletions codegen/message_bus_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions model/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model

type Settings struct {
Key string `gorm:"primaryKey"`
Value string
}
3 changes: 3 additions & 0 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ type Repository interface {
UpsertYSKCard(card ysk.YSKCard) error
DeleteYSKCard(id string) error

GetSettings(key string) (*model.Settings, error)
UpsertSettings(settings model.Settings) error

Close()
}
16 changes: 16 additions & 0 deletions repository/repository_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ func (r *DatabaseRepository) GetEventTypes() ([]model.EventType, error) {
return eventTypes, nil
}

func (r *DatabaseRepository) GetSettings(key string) (*model.Settings, error) {
var settings model.Settings
if err := r.persistDB.Where(&model.Settings{Key: key}).First(&settings).Error; err != nil {
return nil, err
}
return &settings, nil
}

func (r *DatabaseRepository) UpsertSettings(settings model.Settings) error {
return r.persistDB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "key"}},
UpdateAll: true,
}).Create(&settings).Error
}

func (r *DatabaseRepository) RegisterEventType(eventType model.EventType) (*model.EventType, error) {
// upsert
if err := r.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&eventType).Error; err != nil {
Expand Down Expand Up @@ -175,6 +190,7 @@ func NewDatabaseRepository(databaseFilePath string, persistDatabaseFilePath stri

if err := persistDB.AutoMigrate(
&ysk.YSKCard{},
&model.Settings{},
); err != nil {
return nil, err
}
Expand Down
17 changes: 14 additions & 3 deletions service/ysk.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type YSKService struct {
eventTypeService *EventTypeService
}

const YSKOnboardingFinishedKey = "ysk_onboarding_finished"

func NewYSKService(
repository *repository.Repository,
ws *EventServiceWS,
Expand Down Expand Up @@ -55,11 +57,20 @@ func (s *YSKService) DeleteYSKCard(ctx context.Context, id string) error {
}

func (s *YSKService) Start(init bool) {
// 判断数据库
if init {
// only run once
settings, err := (*s.repository).GetSettings(YSKOnboardingFinishedKey)

s.UpsertYSKCard(context.Background(), utils.ZimaOSDataStationNotice)
s.UpsertYSKCard(context.Background(), utils.ZimaOSFileManagementNotice)
s.UpsertYSKCard(context.Background(), utils.ZimaOSRemoteAccessNotice)
if settings == nil && err.Error() == "record not found" {
s.UpsertYSKCard(context.Background(), utils.ZimaOSDataStationNotice)
s.UpsertYSKCard(context.Background(), utils.ZimaOSFileManagementNotice)
s.UpsertYSKCard(context.Background(), utils.ZimaOSRemoteAccessNotice)
(*s.repository).UpsertSettings(model.Settings{
Key: YSKOnboardingFinishedKey,
Value: "true",
})
}
}
// register event
s.eventTypeService.RegisterEventType(model.EventType{
Expand Down

0 comments on commit efadf51

Please sign in to comment.