-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
722 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package consts | ||
|
||
const ( | ||
// SettingSignPrivateKey is the private key for signing | ||
SettingSignPrivateKey = "signing.private_key" | ||
// SettingSignPublicKey is the public key for signing | ||
SettingSignPublicKey = "signing.public_key" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package dao | ||
|
||
import ( | ||
"context" | ||
|
||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
|
||
"github.com/go-sigma/sigma/pkg/dal/models" | ||
"github.com/go-sigma/sigma/pkg/dal/query" | ||
) | ||
|
||
//go:generate mockgen -destination=mocks/setting.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao SettingService | ||
//go:generate mockgen -destination=mocks/setting_factory.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao SettingServiceFactory | ||
|
||
// SettingService is the interface that provides methods to operate on setting model | ||
type SettingService interface { | ||
// Save save a new cache record in the database | ||
Save(ctx context.Context, key string, val []byte) error | ||
// Delete get a cache record | ||
Delete(ctx context.Context, key string) error | ||
// Get get a cache record | ||
Get(ctx context.Context, key string) (*models.Setting, error) | ||
} | ||
|
||
type settingService struct { | ||
tx *query.Query | ||
} | ||
|
||
// SettingServiceFactory is the interface that provides the setting service factory methods. | ||
type SettingServiceFactory interface { | ||
New(txs ...*query.Query) SettingService | ||
} | ||
|
||
type settingServiceFactory struct{} | ||
|
||
// NewSettingServiceFactory creates a new setting service factory. | ||
func NewSettingServiceFactory() SettingServiceFactory { | ||
return &settingServiceFactory{} | ||
} | ||
|
||
func (f *settingServiceFactory) New(txs ...*query.Query) SettingService { | ||
tx := query.Q | ||
if len(txs) > 0 { | ||
tx = txs[0] | ||
} | ||
return &settingService{ | ||
tx: tx, | ||
} | ||
} | ||
|
||
// Create creates a new setting record in the database | ||
func (s settingService) Save(ctx context.Context, key string, val []byte) error { | ||
var setting = models.Setting{Key: key, Val: val} | ||
return s.tx.Setting.WithContext(ctx).Clauses(clause.OnConflict{UpdateAll: true}).Create(&setting) | ||
} | ||
|
||
// Delete get a cache record | ||
func (s settingService) Delete(ctx context.Context, key string) error { | ||
matched, err := s.tx.Setting.WithContext(ctx).Unscoped().Where(s.tx.Setting.Key.Eq(key)).Delete() | ||
if err != nil { | ||
return err | ||
} | ||
if matched.RowsAffected == 0 { | ||
return gorm.ErrRecordNotFound | ||
} | ||
return nil | ||
} | ||
|
||
// Get get a cache record | ||
func (s settingService) Get(ctx context.Context, key string) (*models.Setting, error) { | ||
return s.tx.Setting.WithContext(ctx).Where(s.tx.Setting.Key.Eq(key)).First() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
"gorm.io/plugin/soft_delete" | ||
) | ||
|
||
// Setting setting | ||
type Setting struct { | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
DeletedAt soft_delete.DeletedAt `gorm:"softDelete:milli"` | ||
ID int64 `gorm:"primaryKey"` | ||
|
||
Key string `gorm:"uniqueIndex,size:256"` | ||
Val []byte | ||
} |
Oops, something went wrong.