Prechádzať zdrojové kódy

完成配置的开发

yjp 1 rok pred
rodič
commit
bee32a2128

+ 8 - 0
binding/binder.go

@@ -16,3 +16,11 @@ func NewBinder(router api.Router, i *infrastructure.Infrastructure) *Binder {
 		i:      i,
 	}
 }
+
+func (b *Binder) Router() api.Router {
+	return b.router
+}
+
+func (b *Binder) Infrastructure() *infrastructure.Infrastructure {
+	return b.i
+}

+ 3 - 3
binding/simple_bind_item.go

@@ -34,6 +34,9 @@ type SimpleBindItem[O any] struct {
 	// URL相对路径
 	Path string
 
+	// 响应泛型函数,如果不响应,需要使用NoResponse零值占位
+	ResponseFunc ResponseFunc[O]
+
 	// 使用的请求参数,非必传,当请求参数为nil时,说明该接口没有参数
 	RequestParams request.Params
 
@@ -60,9 +63,6 @@ type SimpleBindItem[O any] struct {
 
 	// 应用服务泛型函数
 	ServiceFunc ServiceFunc[O]
-
-	// 响应泛型函数,如果不响应,需要使用NoResponse零值占位
-	ResponseFunc ResponseFunc[O]
 }
 
 func (item *SimpleBindItem[O]) bind(binder *Binder, method string, middlewares ...api.Handler) {

+ 40 - 17
convenient/domain/configuration/api.go

@@ -1,11 +1,17 @@
 package configuration
 
 import (
+	"git.sxidc.com/go-framework/baize/api"
 	"git.sxidc.com/go-framework/baize/binding"
+	"git.sxidc.com/go-framework/baize/binding/request"
 	"git.sxidc.com/go-framework/baize/binding/response"
 	"git.sxidc.com/go-framework/baize/convenient/value_object"
 	"git.sxidc.com/go-framework/baize/domain"
+	"git.sxidc.com/go-framework/baize/infrastructure"
+	"git.sxidc.com/go-framework/baize/infrastructure/database"
+	"git.sxidc.com/go-framework/baize/infrastructure/database/sql"
 	"git.sxidc.com/go-tools/utils/strutils"
+	"git.sxidc.com/service-supports/fserr"
 )
 
 func BindConfiguration(binder *binding.Binder, opts ...Option) {
@@ -20,15 +26,9 @@ func BindConfiguration(binder *binding.Binder, opts ...Option) {
 		configurationTableName = tableName + "." + options.schema
 	}
 
-	dbExecutorType := binding.DBExecutorOperations
-	if strutils.IsStringNotEmpty(options.dbExecutorType) {
-		dbExecutorType = options.dbExecutorType
-	}
-
 	value_object.BindSimple(binder, &value_object.Simple[any]{
 		ValueObject:       &Entity{},
 		TableName:         configurationTableName,
-		DBExecutorType:    dbExecutorType,
 		DomainPath:        "/configuration",
 		CreateJsonBody:    &AddConfigurationJsonBody{},
 		DeleteQueryParams: &RemoveConfigurationQueryParams{},
@@ -36,19 +36,48 @@ func BindConfiguration(binder *binding.Binder, opts ...Option) {
 
 	binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
 		Path:           "/configuration/values",
+		ResponseFunc:   response.SendMapResponse,
 		RequestParams:  &GetConfigurationValuesQueryParams{},
 		Objects:        []domain.Object{&Entity{}},
-		Infrastructure: binder.ChooseDBExecutor(dbExecutorType),
-		ServiceFunc:    nil,
-		ResponseFunc:   response.SendMapResponse,
+		Infrastructure: binder.Infrastructure(),
+		ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
+			dbExecutor := i.DBExecutor()
+
+			e, ok := objects[0].(*Entity)
+			if !ok {
+				return map[string]any{
+					"values": make([]string, 0),
+				}, fserr.New("传递的实体不是该领域的实体")
+			}
+
+			results, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
+				TableName:     configurationTableName,
+				SelectColumns: []string{ColumnValue},
+				Conditions: sql.NewConditions().
+					Equal(ColumnGroup, e.Group),
+			})
+			if err != nil {
+				return map[string]any{
+					"values": make([]string, 0),
+				}, err
+			}
+
+			values := make([]string, 0)
+			for _, result := range results {
+				values = append(values, result.ColumnValueString(ColumnValue))
+			}
+
+			return map[string]any{
+				"values": values,
+			}, nil
+		},
 	})
 }
 
 type Option func(options *Options)
 
 type Options struct {
-	schema         string
-	dbExecutorType string
+	schema string
 }
 
 func WithSchema(schema string) Option {
@@ -56,9 +85,3 @@ func WithSchema(schema string) Option {
 		options.schema = schema
 	}
 }
-
-func WithDBExecutorType(dbExecutorType string) Option {
-	return func(options *Options) {
-		options.dbExecutorType = dbExecutorType
-	}
-}

+ 5 - 0
convenient/domain/configuration/entity.go

@@ -9,6 +9,11 @@ const (
 	tableName = "configurations"
 )
 
+const (
+	ColumnGroup = "group"
+	ColumnValue = "value"
+)
+
 type Entity struct {
 	Group string `sqlmapping:"column:group;key;notUpdate;" sqlresult:"column:group;"`
 	Value string `sqlmapping:"column:value;notUpdate;" sqlresult:"column:value;"`

+ 0 - 3
convenient/domain/configuration/request_params.go

@@ -1,7 +1,5 @@
 package configuration
 
-import "git.sxidc.com/go-framework/baize/binding/request"
-
 type (
 	AddConfigurationJsonBody struct {
 		GroupName string `json:"group" binding:"required" assign:"toField:Group"`
@@ -13,7 +11,6 @@ type (
 	}
 
 	GetConfigurationValuesQueryParams struct {
-		request.BaseQuery
 		GroupName string `form:"group" binding:"required" assign:"toField:Group"`
 	}
 )