2022-09-27 17:59:05 +08:00
|
|
|
package config
|
|
|
|
|
2023-05-25 20:13:18 +08:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/answerdev/answer/internal/entity"
|
|
|
|
)
|
2023-05-25 11:17:10 +08:00
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
// ConfigRepo config repository
|
|
|
|
type ConfigRepo interface {
|
2023-05-25 20:13:18 +08:00
|
|
|
GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error)
|
|
|
|
GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error)
|
|
|
|
UpdateConfig(ctx context.Context, key, value string) (err error)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigService user service
|
|
|
|
type ConfigService struct {
|
|
|
|
configRepo ConfigRepo
|
|
|
|
}
|
|
|
|
|
2023-05-25 20:13:18 +08:00
|
|
|
// NewConfigService new config service
|
2022-09-27 17:59:05 +08:00
|
|
|
func NewConfigService(configRepo ConfigRepo) *ConfigService {
|
|
|
|
return &ConfigService{
|
|
|
|
configRepo: configRepo,
|
|
|
|
}
|
|
|
|
}
|
2023-05-25 20:13:18 +08:00
|
|
|
|
|
|
|
// GetIntValue get config int value
|
|
|
|
func (cs *ConfigService) GetIntValue(ctx context.Context, key string) (val int, err error) {
|
|
|
|
cf, err := cs.configRepo.GetConfigByKey(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return cf.GetIntValue(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStringValue get config string value
|
|
|
|
func (cs *ConfigService) GetStringValue(ctx context.Context, key string) (val string, err error) {
|
|
|
|
cf, err := cs.configRepo.GetConfigByKey(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return cf.Value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetArrayStringValue get config array string value
|
|
|
|
func (cs *ConfigService) GetArrayStringValue(ctx context.Context, key string) (val []string, err error) {
|
|
|
|
cf, err := cs.configRepo.GetConfigByKey(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cf.GetArrayStringValue(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ConfigService) GetJsonConfigByIDAndSetToObject(ctx context.Context, id int, obj any) (err error) {
|
|
|
|
cf, err := cs.configRepo.GetConfigByID(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(cf.Value), obj)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[%s] config value is not json format", cf.Key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfigByID get config by id
|
|
|
|
func (cs *ConfigService) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {
|
|
|
|
return cs.configRepo.GetConfigByID(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ConfigService) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
|
|
|
|
return cs.configRepo.GetConfigByKey(ctx, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIDByKey get config id by key
|
|
|
|
func (cs *ConfigService) GetIDByKey(ctx context.Context, key string) (id int, err error) {
|
|
|
|
cf, err := cs.configRepo.GetConfigByKey(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return cf.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ConfigService) UpdateConfig(ctx context.Context, key, value string) (err error) {
|
|
|
|
return cs.configRepo.UpdateConfig(ctx, key, value)
|
|
|
|
}
|