answer/internal/repo/config/config_repo.go

111 lines
3.0 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package config
import (
"context"
2022-09-27 17:59:05 +08:00
"fmt"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/config"
2022-09-27 17:59:05 +08:00
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
2022-09-27 17:59:05 +08:00
)
// configRepo config repository
type configRepo struct {
data *data.Data
}
// NewConfigRepo new repository
func NewConfigRepo(data *data.Data) config.ConfigRepo {
repo := &configRepo{
data: data,
}
return repo
}
func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {
cacheKey := fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, id)
if cacheData, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && len(cacheData) > 0 {
c = &entity.Config{}
c.BuildByJSON([]byte(cacheData))
if c.ID > 0 {
return c, nil
2022-09-27 17:59:05 +08:00
}
}
c = &entity.Config{}
2023-06-19 18:29:15 +08:00
exist, err := cr.data.DB.Context(ctx).ID(id).Get(c)
2022-10-27 12:00:03 +08:00
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
2022-10-27 12:00:03 +08:00
}
if !exist {
return nil, fmt.Errorf("config not found by id: %d", id)
2022-09-27 17:59:05 +08:00
}
// update cache
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), -1); err != nil {
log.Error(err)
2022-09-27 17:59:05 +08:00
}
return c, nil
2022-09-27 17:59:05 +08:00
}
func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
cacheKey := constant.ConfigKEY2ContentCacheKeyPrefix + key
if cacheData, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && len(cacheData) > 0 {
c = &entity.Config{}
c.BuildByJSON([]byte(cacheData))
if c.ID > 0 {
return c, nil
}
2022-09-27 17:59:05 +08:00
}
c = &entity.Config{Key: key}
exist, err := cr.data.DB.Context(ctx).Get(c)
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !exist {
return nil, fmt.Errorf("config not found by key: %s", key)
2022-09-27 17:59:05 +08:00
}
// update cache
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), -1); err != nil {
log.Error(err)
2022-09-27 17:59:05 +08:00
}
return c, nil
}
2022-09-27 17:59:05 +08:00
func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string) (err error) {
// check if key exists
oldConfig := &entity.Config{}
exist, err := cr.data.DB.Context(ctx).Get(oldConfig)
2022-10-27 12:00:03 +08:00
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
2022-10-27 12:00:03 +08:00
}
if !exist {
return errors.BadRequest(reason.ObjectNotFound)
2022-10-27 12:00:03 +08:00
}
// update database
_, err = cr.data.DB.Context(ctx).ID(oldConfig.ID).Update(&entity.Config{Value: value})
2022-10-27 12:00:03 +08:00
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
2022-10-27 12:00:03 +08:00
}
2022-10-20 16:38:56 +08:00
oldConfig.Value = value
cacheVal := oldConfig.JsonString()
// update cache
if err := cr.data.Cache.SetString(ctx,
constant.ConfigKEY2ContentCacheKeyPrefix+key, cacheVal, -1); err != nil {
log.Error(err)
}
if err := cr.data.Cache.SetString(ctx,
fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, oldConfig.ID), cacheVal, -1); err != nil {
log.Error(err)
2022-10-20 16:38:56 +08:00
}
return
}