Files
hfb_sys/backend/internal/modules/systemconfig/service.go
T

87 lines
2.1 KiB
Go

package systemconfig
import (
"encoding/json"
"errors"
)
var (
ErrDependencyUnavailable = errors.New("dependency unavailable")
ErrInvalidConfig = errors.New("invalid config")
)
type Service struct {
repo *Repository
}
func NewService(repo *Repository) *Service {
return &Service{repo: repo}
}
func (s *Service) List() ([]ConfigDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.List()
}
func (s *Service) PublishOptions() (*PublishOptionsDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
value, err := s.repo.FindValue(publishOptionsConfigKey)
if err != nil {
return nil, err
}
options := DefaultPublishOptions()
if err := json.Unmarshal([]byte(value), &options); err != nil {
options = DefaultPublishOptions()
}
normalizePublishOptions(&options)
return &options, nil
}
func (s *Service) Update(actorID uint64, key string, req UpdateRequest, meta AuditMeta) (*ConfigDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
if key == "" || req.Value == "" {
return nil, ErrInvalidConfig
}
return s.repo.Update(actorID, key, req, meta)
}
func normalizePublishOptions(options *PublishOptionsDTO) {
defaults := DefaultPublishOptions()
if len(options.ServerOptions) == 0 {
options.ServerOptions = defaults.ServerOptions
}
if len(options.FaceOptions) == 0 {
options.FaceOptions = defaults.FaceOptions
}
if len(options.RankOptions) == 0 {
options.RankOptions = defaults.RankOptions
}
if len(options.InsuranceOptions) == 0 {
options.InsuranceOptions = defaults.InsuranceOptions
}
if len(options.LevelOptions) == 0 {
options.LevelOptions = defaults.LevelOptions
}
if len(options.LoginMethodOptions) == 0 {
options.LoginMethodOptions = defaults.LoginMethodOptions
}
if len(options.RegionOptions) == 0 {
options.RegionOptions = defaults.RegionOptions
}
if len(options.SkinGroups) == 0 {
options.SkinGroups = defaults.SkinGroups
}
if len(options.QuantityItems) == 0 {
options.QuantityItems = defaults.QuantityItems
}
if len(options.ScreenshotSlots) == 0 {
options.ScreenshotSlots = defaults.ScreenshotSlots
}
}