46 lines
908 B
Go
46 lines
908 B
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.nobla.cn/golang/aeus-admin/models"
|
|
"git.nobla.cn/golang/aeus-admin/pb"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type (
|
|
settingOptions struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
SettingOption func(o *settingOptions)
|
|
)
|
|
type SettingService struct {
|
|
opts *settingOptions
|
|
}
|
|
|
|
func (s *SettingService) GetSetting(ctx context.Context, req *pb.GetSettingRequest) (res *pb.GetSettingResponse, err error) {
|
|
tx := s.opts.db.WithContext(ctx)
|
|
values := make([]*models.Setting, 0)
|
|
if err = tx.Find(&values).Error; err != nil {
|
|
return
|
|
}
|
|
res = &pb.GetSettingResponse{
|
|
Data: make([]*pb.SettingItem, 0, len(values)),
|
|
}
|
|
for _, v := range values {
|
|
res.Data = append(res.Data, &pb.SettingItem{
|
|
Name: v.Name,
|
|
Value: v.Value,
|
|
})
|
|
}
|
|
return
|
|
}
|
|
func NewSettingService(cbs ...SettingOption) *SettingService {
|
|
opts := &settingOptions{}
|
|
for _, cb := range cbs {
|
|
cb(opts)
|
|
}
|
|
return &SettingService{}
|
|
}
|