Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix onboarding ysk card only init once #58

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -46,6 +46,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 @@ -173,6 +188,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 @@ -60,11 +62,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
Loading