Kaynağa Gözat

完成关联实体的接口设计

yjp 1 yıl önce
ebeveyn
işleme
a602aea30f

+ 22 - 0
convenient/relation/many2many/service.go

@@ -0,0 +1,22 @@
+package many2many
+
+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/domain"
+	"git.sxidc.com/go-framework/baize/infrastructure"
+)
+
+func Update(tableName string) binding.ServiceFunc[any] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
+		return nil, nil
+	}
+}
+
+func Query[TI any](tableName string) binding.ServiceFunc[TI] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (TI, error) {
+		var info TI
+		return info, nil
+	}
+}

+ 25 - 25
convenient/relation/many2many/simple.go

@@ -38,7 +38,7 @@ type Simple[LI any, RI any] struct {
 	RightQueryQueryParams request.Query
 
 	// 可选配置项,通过WithXXX配置
-	options *Options[LI, RI]
+	options *Options
 }
 
 func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
@@ -46,8 +46,8 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 
 }
 
-func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option[LI, RI]) {
-	options := new(Options[LI, RI])
+func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
+	options := new(Options)
 
 	for _, opt := range opts {
 		opt(options)
@@ -58,9 +58,9 @@ func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI],
 	simple.bind(binder)
 }
 
-type Option[LI any, RI any] func(options *Options[LI, RI])
+type Option func(options *Options)
 
-type Options[LI any, RI any] struct {
+type Options struct {
 	// 左实体中指向右实体的字段
 	leftRelationField string
 
@@ -92,62 +92,62 @@ type Options[LI any, RI any] struct {
 	disableRightQuery bool
 }
 
-func WithLeftRelationField[LI any, RI any](leftRelationField string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithLeftRelationField(leftRelationField string) Option {
+	return func(options *Options) {
 		options.leftRelationField = leftRelationField
 	}
 }
 
-func WithRightRelationField[LI any, RI any](rightRelationField string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithRightRelationField(rightRelationField string) Option {
+	return func(options *Options) {
 		options.rightRelationField = rightRelationField
 	}
 }
 
-func WithLeftRelationColumn[LI any, RI any](leftRelationColumn string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithLeftRelationColumn(leftRelationColumn string) Option {
+	return func(options *Options) {
 		options.leftRelationColumn = leftRelationColumn
 	}
 }
 
-func WithRightRelationColumn[LI any, RI any](rightRelationColumn string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithRightRelationColumn(rightRelationColumn string) Option {
+	return func(options *Options) {
 		options.rightRelationColumn = rightRelationColumn
 	}
 }
 
-func WithDisableLeft[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableLeft() Option {
+	return func(options *Options) {
 		options.disableLeft = true
 	}
 }
 
-func WithDisableRight[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRight() Option {
+	return func(options *Options) {
 		options.disableRight = true
 	}
 }
 
-func WithDisableLeftUpdate[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableLeftUpdate() Option {
+	return func(options *Options) {
 		options.disableLeftUpdate = true
 	}
 }
 
-func WithDisableLeftQuery[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableLeftQuery() Option {
+	return func(options *Options) {
 		options.disableLeftQuery = true
 	}
 }
 
-func WithDisableRightUpdate[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRightUpdate() Option {
+	return func(options *Options) {
 		options.disableRightUpdate = true
 	}
 }
 
-func WithDisableRightQuery[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRightQuery() Option {
+	return func(options *Options) {
 		options.disableRightQuery = true
 	}
 }

+ 58 - 0
convenient/relation/one2many/service.go

@@ -0,0 +1,58 @@
+package one2many
+
+import (
+	"encoding/json"
+	"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/domain"
+	"git.sxidc.com/go-framework/baize/infrastructure"
+)
+
+func UpdateLeft(tableName string) binding.ServiceFunc[any] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
+		return nil, nil
+	}
+}
+
+func QueryLeft[RI any](tableName string) binding.ServiceFunc[RI] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (RI, error) {
+		var info RI
+		return info, nil
+	}
+}
+
+func UpdateRight(tableName string) binding.ServiceFunc[any] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
+		return nil, nil
+	}
+}
+
+func QueryRight[LI any](tableName string) binding.ServiceFunc[LI] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (LI, error) {
+		var info LI
+		return info, nil
+	}
+}
+
+func QueryRightWithLeftInfo[RI any, LI any](tableName string) binding.ServiceFunc[map[string]any] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
+		info := new(struct {
+			Self RI `json:"self"`
+			With LI `json:"with"`
+		})
+
+		infoJson, err := json.Marshal(info)
+		if err != nil {
+			return nil, err
+		}
+
+		retMap := make(map[string]any)
+		err = json.Unmarshal(infoJson, &retMap)
+		if err != nil {
+			return nil, err
+		}
+
+		return retMap, nil
+	}
+}

+ 23 - 23
convenient/relation/one2many/simple.go

@@ -44,7 +44,7 @@ type Simple[LI any, RI any] struct {
 	RightQueryWithLeftQueryParams request.Query
 
 	// 可选配置项,通过WithXXX配置
-	options *Options[LI, RI]
+	options *Options
 }
 
 func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
@@ -52,8 +52,8 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 
 }
 
-func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option[LI, RI]) {
-	options := new(Options[LI, RI])
+func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
+	options := new(Options)
 
 	for _, opt := range opts {
 		opt(options)
@@ -64,9 +64,9 @@ func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI],
 	simple.bind(binder)
 }
 
-type Option[LI any, RI any] func(options *Options[LI, RI])
+type Option func(options *Options)
 
-type Options[LI any, RI any] struct {
+type Options struct {
 	// 右实体中指向左实体的字段
 	rightRelationField string
 
@@ -95,56 +95,56 @@ type Options[LI any, RI any] struct {
 	disableRightWithLeftQuery bool
 }
 
-func WithRightRelationField[LI any, RI any](rightRelationField string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithRightRelationField(rightRelationField string) Option {
+	return func(options *Options) {
 		options.rightRelationField = rightRelationField
 	}
 }
 
-func WithRightRelationColumn[LI any, RI any](rightRelationColumn string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithRightRelationColumn(rightRelationColumn string) Option {
+	return func(options *Options) {
 		options.rightRelationColumn = rightRelationColumn
 	}
 }
 
-func WithDisableLeft[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableLeft() Option {
+	return func(options *Options) {
 		options.disableLeft = true
 	}
 }
 
-func WithDisableRight[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRight() Option {
+	return func(options *Options) {
 		options.disableRight = true
 	}
 }
 
-func WithDisableLeftUpdate[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableLeftUpdate() Option {
+	return func(options *Options) {
 		options.disableLeftUpdate = true
 	}
 }
 
-func WithDisableLeftQuery[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableLeftQuery() Option {
+	return func(options *Options) {
 		options.disableLeftQuery = true
 	}
 }
 
-func WithDisableRightUpdate[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRightUpdate() Option {
+	return func(options *Options) {
 		options.disableRightUpdate = true
 	}
 }
 
-func WithDisableRightQuery[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRightQuery() Option {
+	return func(options *Options) {
 		options.disableRightQuery = true
 	}
 }
 
-func WithDisableRightWithLeftQuery[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRightWithLeftQuery() Option {
+	return func(options *Options) {
 		options.disableRightWithLeftQuery = true
 	}
 }

+ 45 - 0
convenient/relation/one2one/service.go

@@ -0,0 +1,45 @@
+package one2one
+
+import (
+	"encoding/json"
+	"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/domain"
+	"git.sxidc.com/go-framework/baize/infrastructure"
+)
+
+func Update(tableName string) binding.ServiceFunc[any] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
+		return nil, nil
+	}
+}
+
+func Query[TI any](tableName string) binding.ServiceFunc[TI] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (TI, error) {
+		var info TI
+		return info, nil
+	}
+}
+
+func QueryWithOtherInfo[FI any, TI any](tableName string) binding.ServiceFunc[map[string]any] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
+		info := new(struct {
+			Self FI `json:"self"`
+			With TI `json:"with"`
+		})
+
+		infoJson, err := json.Marshal(info)
+		if err != nil {
+			return nil, err
+		}
+
+		retMap := make(map[string]any)
+		err = json.Unmarshal(infoJson, &retMap)
+		if err != nil {
+			return nil, err
+		}
+
+		return retMap, nil
+	}
+}

+ 49 - 25
convenient/relation/one2one/simple.go

@@ -40,16 +40,22 @@ type Simple[LI any, RI any] struct {
 	// 查询右实体关联使用的请求参数,注意是Query类型
 	RightQueryQueryParams request.Query
 
+	// 查询左实体带右实体信息使用的请求参数,注意是Query类型
+	LeftQueryWithRightQueryParams request.Query
+
+	// 查询右实体带左实体信息使用的请求参数,注意是Query类型
+	RightQueryWithLeftQueryParams request.Query
+
 	// 可选配置项,通过WithXXX配置
-	options *Options[LI, RI]
+	options *Options
 }
 
 func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 	//options := simple.options
 }
 
-func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option[LI, RI]) {
-	options := new(Options[LI, RI])
+func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
+	options := new(Options)
 
 	for _, opt := range opts {
 		opt(options)
@@ -60,9 +66,9 @@ func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI],
 	simple.bind(binder)
 }
 
-type Option[LI any, RI any] func(options *Options[LI, RI])
+type Option func(options *Options)
 
-type Options[LI any, RI any] struct {
+type Options struct {
 	// 左实体中指向右实体的字段
 	leftRelationField string
 
@@ -92,64 +98,82 @@ type Options[LI any, RI any] struct {
 
 	// 关闭右侧查询
 	disableRightQuery bool
+
+	// 关闭左实体带右实体信息查询
+	disableLeftWithRightQuery bool
+
+	// 关闭右实体带左实体信息查询
+	disableRightWithLeftQuery bool
 }
 
-func WithLeftRelationField[LI any, RI any](leftRelationField string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithLeftRelationField(leftRelationField string) Option {
+	return func(options *Options) {
 		options.leftRelationField = leftRelationField
 	}
 }
 
-func WithRightRelationField[LI any, RI any](rightRelationField string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithRightRelationField(rightRelationField string) Option {
+	return func(options *Options) {
 		options.rightRelationField = rightRelationField
 	}
 }
 
-func WithLeftRelationColumn[LI any, RI any](leftRelationColumn string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithLeftRelationColumn(leftRelationColumn string) Option {
+	return func(options *Options) {
 		options.leftRelationColumn = leftRelationColumn
 	}
 }
 
-func WithRightRelationColumn[LI any, RI any](rightRelationColumn string) Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithRightRelationColumn(rightRelationColumn string) Option {
+	return func(options *Options) {
 		options.rightRelationColumn = rightRelationColumn
 	}
 }
 
-func WithDisableLeft[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableLeft() Option {
+	return func(options *Options) {
 		options.disableLeft = true
 	}
 }
 
-func WithDisableRight[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRight() Option {
+	return func(options *Options) {
 		options.disableRight = true
 	}
 }
 
-func WithDisableLeftUpdate[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableLeftUpdate() Option {
+	return func(options *Options) {
 		options.disableLeftUpdate = true
 	}
 }
 
-func WithDisableLeftQuery[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableLeftQuery[LI any, RI any]() Option {
+	return func(options *Options) {
 		options.disableLeftQuery = true
 	}
 }
 
-func WithDisableRightUpdate[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRightUpdate() Option {
+	return func(options *Options) {
 		options.disableRightUpdate = true
 	}
 }
 
-func WithDisableRightQuery[LI any, RI any]() Option[LI, RI] {
-	return func(options *Options[LI, RI]) {
+func WithDisableRightQuery[LI any, RI any]() Option {
+	return func(options *Options) {
 		options.disableRightQuery = true
 	}
 }
+
+func WithDisableLeftWithRightQuery[LI any, RI any]() Option {
+	return func(options *Options) {
+		options.disableLeftWithRightQuery = true
+	}
+}
+
+func WithDisableRightWithLeftQuery() Option {
+	return func(options *Options) {
+		options.disableRightWithLeftQuery = true
+	}
+}

+ 35 - 0
convenient/relation/remote/service.go

@@ -0,0 +1,35 @@
+package many2many
+
+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/domain"
+	"git.sxidc.com/go-framework/baize/infrastructure"
+)
+
+func Update(tableName string) binding.ServiceFunc[any] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
+		return nil, nil
+	}
+}
+
+func Query[TI any](tableName string) binding.ServiceFunc[TI] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (TI, error) {
+		var info TI
+		return info, nil
+	}
+}
+
+func UpdateRemote(tableName string) binding.ServiceFunc[any] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
+		return nil, nil
+	}
+}
+
+func QueryRemote(tableName string) binding.ServiceFunc[[]string] {
+	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) ([]string, error) {
+		ids := make([]string, 0)
+		return ids, nil
+	}
+}

+ 159 - 0
convenient/relation/remote/simple.go

@@ -0,0 +1,159 @@
+package many2many
+
+import (
+	"git.sxidc.com/go-framework/baize/binding"
+	"git.sxidc.com/go-framework/baize/binding/request"
+	"git.sxidc.com/go-framework/baize/domain"
+)
+
+// Simple 关联的Bind参数
+// LI 为左边实体的Info类型
+// RI 为右边实体的Info类型
+type Simple[LI any, RI any] struct {
+	// 左领域实体,注意是Entity类型
+	Left domain.Entity
+
+	// 右领域实体,注意是Entity类型
+	Right domain.Entity
+
+	// 左领域实体不是本地的
+	LeftRemote bool
+
+	// 右领域实体不是本地的
+	RightRemote bool
+
+	// 中间表表名
+	MiddleTableName string
+
+	// URL领域相对路径,如/person/identities,后面会自动补充,如/person/identities/update
+	LeftDomainPath string
+
+	// URL领域相对路径,如/identity/persons,后面会自动补充,如/identity/persons/update
+	RightDomainPath string
+
+	// 更新左实体关联使用的请求参数
+	LeftUpdateJsonBody request.Params
+
+	// 查询左实体关联使用的请求参数,注意是Query类型
+	LeftQueryQueryParams request.Query
+
+	// 更新右实体关联使用的请求参数
+	RightUpdateJsonBody request.Params
+
+	// 查询右实体关联使用的请求参数,注意是Query类型
+	RightQueryQueryParams request.Query
+
+	// 可选配置项,通过WithXXX配置
+	options *Options
+}
+
+func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
+	//options := simple.options
+
+}
+
+func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
+	options := new(Options)
+
+	for _, opt := range opts {
+		opt(options)
+	}
+
+	simple.options = options
+
+	simple.bind(binder)
+}
+
+type Option func(options *Options)
+
+type Options struct {
+	// 左实体中指向右实体的字段
+	leftRelationField string
+
+	// 右实体中指向左实体的字段
+	rightRelationField string
+
+	// 左实体中指向右实体的列名
+	leftRelationColumn string
+
+	// 右实体中指向左实体的列名
+	rightRelationColumn string
+
+	// 关闭左侧到右侧关联
+	disableLeft bool
+
+	// 关闭右侧到左侧关联
+	disableRight bool
+
+	// 关闭左侧更新
+	disableLeftUpdate bool
+
+	// 关闭左侧查询
+	disableLeftQuery bool
+
+	// 关闭右侧更新
+	disableRightUpdate bool
+
+	// 关闭右侧查询
+	disableRightQuery bool
+}
+
+func WithLeftRelationField(leftRelationField string) Option {
+	return func(options *Options) {
+		options.leftRelationField = leftRelationField
+	}
+}
+
+func WithRightRelationField(rightRelationField string) Option {
+	return func(options *Options) {
+		options.rightRelationField = rightRelationField
+	}
+}
+
+func WithLeftRelationColumn(leftRelationColumn string) Option {
+	return func(options *Options) {
+		options.leftRelationColumn = leftRelationColumn
+	}
+}
+
+func WithRightRelationColumn(rightRelationColumn string) Option {
+	return func(options *Options) {
+		options.rightRelationColumn = rightRelationColumn
+	}
+}
+
+func WithDisableLeft() Option {
+	return func(options *Options) {
+		options.disableLeft = true
+	}
+}
+
+func WithDisableRight() Option {
+	return func(options *Options) {
+		options.disableRight = true
+	}
+}
+
+func WithDisableLeftUpdate() Option {
+	return func(options *Options) {
+		options.disableLeftUpdate = true
+	}
+}
+
+func WithDisableLeftQuery() Option {
+	return func(options *Options) {
+		options.disableLeftQuery = true
+	}
+}
+
+func WithDisableRightUpdate() Option {
+	return func(options *Options) {
+		options.disableRightUpdate = true
+	}
+}
+
+func WithDisableRightQuery() Option {
+	return func(options *Options) {
+		options.disableRightQuery = true
+	}
+}