Browse Source

修改实体结构

yjp 1 year ago
parent
commit
5fc1ad7a06
2 changed files with 180 additions and 82 deletions
  1. 179 81
      convenient/entity/simple.go
  2. 1 1
      examples/binding/main.go

+ 179 - 81
convenient/entity/simple.go

@@ -8,20 +8,23 @@ import (
 	"git.sxidc.com/go-framework/baize/domain"
 )
 
-func BindSimple[O any](binder *binding.Binder, crud *Simple[O]) {
-	crud.bind(binder)
-}
-
 // Simple 实体CRUD的Bind参数
-type Simple[O any] struct {
+// I 为查询相关接口返回的Info类型
+type Simple[I any] struct {
 	// 使用的领域实体,注意是Entity类型
 	Entity domain.Entity
 
+	// 表名
+	TableName string
+
 	// 选择要使用的数据库Executor
 	// DBExecutorOperations operations 数据库操作
 	// DBExecutorDataService data_service 数据服务
 	DBExecutorType string
 
+	// URL领域相对路径,如/class,后面会自动补充
+	DomainPath string
+
 	// 创建使用的请求参数
 	CreateJsonBody request.Params
 
@@ -37,12 +40,104 @@ type Simple[O any] struct {
 	// 根据ID查询使用的请求参数,注意是WithID类型
 	GetByIDQueryParams request.WithID
 
-	// 表名
-	tableName string
+	options *Options[I]
+}
 
-	// URL领域相对路径,如/class,后面会自动补充
-	domainPath string
+func (simple *Simple[I]) bind(binder *binding.Binder) {
+	dbExecutor := binder.ChooseDBExecutor(simple.DBExecutorType)
+	options := simple.options
+
+	// 创建
+	if !options.createNeedTx {
+		binding.PostBind(binder, &binding.SimpleBindItem[string]{
+			Path:          simple.DomainPath + "/create",
+			ResponseFunc:  response.SendIDResponse[string],
+			RequestParams: simple.CreateJsonBody,
+			Objects:       []domain.Object{simple.Entity},
+			ServiceFunc:   Create(simple.TableName, dbExecutor, options.createCallbacks),
+		}, options.createMiddlewares...)
+	} else {
+		binding.PostBind(binder, &binding.SimpleBindItem[string]{
+			Path:          simple.DomainPath + "/create",
+			ResponseFunc:  response.SendIDResponse[string],
+			RequestParams: simple.CreateJsonBody,
+			Objects:       []domain.Object{simple.Entity},
+			ServiceFunc:   CreateTx(simple.TableName, dbExecutor, options.createCallbacks),
+		}, options.createMiddlewares...)
+	}
+
+	// 删除
+	if !options.deleteNeedTx {
+		binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
+			Path:          simple.DomainPath + "/:id/delete",
+			ResponseFunc:  response.SendMsgResponse,
+			RequestParams: simple.DeleteQueryParams,
+			Objects:       []domain.Object{simple.Entity},
+			ServiceFunc:   Delete(simple.TableName, dbExecutor, options.deleteCallbacks),
+		}, options.deleteMiddlewares...)
+	} else {
+		binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
+			Path:          simple.DomainPath + "/:id/delete",
+			ResponseFunc:  response.SendMsgResponse,
+			RequestParams: simple.DeleteQueryParams,
+			Objects:       []domain.Object{simple.Entity},
+			ServiceFunc:   DeleteTx(simple.TableName, dbExecutor, options.deleteCallbacks),
+		}, options.deleteMiddlewares...)
+	}
+
+	// 修改
+	if !options.updateNeedTx {
+		binding.PutBind(binder, &binding.SimpleBindItem[any]{
+			Path:          simple.DomainPath + "/update",
+			ResponseFunc:  response.SendMsgResponse,
+			RequestParams: simple.UpdateJsonBody,
+			Objects:       []domain.Object{simple.Entity},
+			ServiceFunc:   Update(simple.TableName, dbExecutor, options.updateCallbacks),
+		}, options.updateMiddlewares...)
+	} else {
+		binding.PutBind(binder, &binding.SimpleBindItem[any]{
+			Path:          simple.DomainPath + "/update",
+			ResponseFunc:  response.SendMsgResponse,
+			RequestParams: simple.UpdateJsonBody,
+			Objects:       []domain.Object{simple.Entity},
+			ServiceFunc:   UpdateTx(simple.TableName, dbExecutor, options.updateCallbacks),
+		}, options.updateMiddlewares...)
+	}
+
+	// 查询
+	binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
+		Path:          simple.DomainPath + "/query",
+		ResponseFunc:  response.SendInfosResponse[I],
+		RequestParams: simple.QueryParams,
+		Objects:       []domain.Object{simple.Entity},
+		ServiceFunc:   Query[I](simple.TableName, dbExecutor, options.queryCallbacks, options.queryConditionFieldCallback),
+	}, options.queryMiddlewares...)
+
+	// 通过ID获取
+	binding.GetBind(binder, &binding.SimpleBindItem[I]{
+		Path:          simple.DomainPath + "/get",
+		ResponseFunc:  response.SendInfoResponse[I],
+		RequestParams: simple.GetByIDQueryParams,
+		Objects:       []domain.Object{simple.Entity},
+		ServiceFunc:   GetByID[I](simple.TableName, dbExecutor, options.getByIDCallbacks),
+	}, options.getByIDMiddlewares...)
+}
+
+func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...Option[I]) {
+	options := new(Options[I])
+
+	for _, opt := range opts {
+		opt(options)
+	}
+
+	simple.options = options
 
+	simple.bind(binder)
+}
+
+type Option[I any] func(options *Options[I])
+
+type Options[I any] struct {
 	// 创建是否使用事务
 	createNeedTx bool
 
@@ -74,95 +169,98 @@ type Simple[O any] struct {
 	queryConditionFieldCallback ConditionFieldCallback
 
 	// 查询回调
-	queryCallbacks *Callbacks[response.InfosData[O]]
+	queryCallbacks *Callbacks[response.InfosData[I]]
 
 	// 查询中间件
 	queryMiddlewares []api.Handler
 
 	// 根据ID查询回调
-	getByIDCallbacks *Callbacks[O]
+	getByIDCallbacks *Callbacks[I]
 
 	// 根据ID查询中间件
 	getByIDMiddlewares []api.Handler
 }
 
-func (crud *Simple[O]) bind(binder *binding.Binder) {
-	dbExecutor := binder.ChooseDBExecutor(crud.DBExecutorType)
+func WithCreateTx[I any]() Option[I] {
+	return func(options *Options[I]) {
+		options.createNeedTx = true
+	}
+}
 
-	// 创建
-	if !crud.CreateNeedTx {
-		binding.PostBind(binder, &binding.SimpleBindItem[string]{
-			Path:          crud.DomainPath + "/create",
-			ResponseFunc:  response.SendIDResponse[string],
-			RequestParams: crud.CreateJsonBody,
-			Objects:       []domain.Object{crud.Entity},
-			ServiceFunc:   Create(crud.TableName, dbExecutor, crud.CreateCallbacks),
-		})
-	} else {
-		binding.PostBind(binder, &binding.SimpleBindItem[string]{
-			Path:          crud.DomainPath + "/create",
-			ResponseFunc:  response.SendIDResponse[string],
-			RequestParams: crud.CreateJsonBody,
-			Objects:       []domain.Object{crud.Entity},
-			ServiceFunc:   CreateTx(crud.TableName, dbExecutor, crud.CreateCallbacks),
-		})
+func WithCreateCallbacks[I any](callbacks *Callbacks[string]) Option[I] {
+	return func(options *Options[I]) {
+		options.createCallbacks = callbacks
 	}
+}
 
-	// 删除
-	if !crud.DeleteNeedTx {
-		binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
-			Path:          crud.DomainPath + "/:id/delete",
-			ResponseFunc:  response.SendMsgResponse,
-			RequestParams: crud.DeleteQueryParams,
-			Objects:       []domain.Object{crud.Entity},
-			ServiceFunc:   Delete(crud.TableName, dbExecutor, crud.DeleteCallbacks),
-		})
-	} else {
-		binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
-			Path:          crud.DomainPath + "/:id/delete",
-			ResponseFunc:  response.SendMsgResponse,
-			RequestParams: crud.DeleteQueryParams,
-			Objects:       []domain.Object{crud.Entity},
-			ServiceFunc:   DeleteTx(crud.TableName, dbExecutor, crud.DeleteCallbacks),
-		})
+func WithCreateMiddlewares[I any](middlewares []api.Handler) Option[I] {
+	return func(options *Options[I]) {
+		options.createMiddlewares = middlewares
 	}
+}
 
-	// 修改
-	if !crud.UpdateNeedTx {
-		binding.PutBind(binder, &binding.SimpleBindItem[any]{
-			Path:          crud.DomainPath + "/update",
-			ResponseFunc:  response.SendMsgResponse,
-			RequestParams: crud.UpdateJsonBody,
-			Objects:       []domain.Object{crud.Entity},
-			ServiceFunc:   Update(crud.TableName, dbExecutor, crud.UpdateCallbacks),
-		})
-	} else {
-		binding.PutBind(binder, &binding.SimpleBindItem[any]{
-			Path:          crud.DomainPath + "/update",
-			ResponseFunc:  response.SendMsgResponse,
-			RequestParams: crud.UpdateJsonBody,
-			Objects:       []domain.Object{crud.Entity},
-			ServiceFunc:   UpdateTx(crud.TableName, dbExecutor, crud.UpdateCallbacks),
-		})
+func WithDeleteTx[I any]() Option[I] {
+	return func(options *Options[I]) {
+		options.deleteNeedTx = true
 	}
+}
 
-	// 查询
-	binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[O]]{
-		Path:          crud.DomainPath + "/query",
-		ResponseFunc:  response.SendInfosResponse[O],
-		RequestParams: crud.QueryParams,
-		Objects:       []domain.Object{crud.Entity},
-		ServiceFunc:   Query[O](crud.TableName, dbExecutor, crud.QueryCallbacks, crud.QueryConditionFieldCallback),
-	})
+func WithDeleteCallbacks[I any](callbacks *Callbacks[any]) Option[I] {
+	return func(options *Options[I]) {
+		options.deleteCallbacks = callbacks
+	}
+}
 
-	// 通过ID获取
-	binding.GetBind(binder, &binding.SimpleBindItem[O]{
-		Path:          crud.DomainPath + "/get",
-		ResponseFunc:  response.SendInfoResponse[O],
-		RequestParams: crud.GetByIDQueryParams,
-		Objects:       []domain.Object{crud.Entity},
-		ServiceFunc:   GetByID[O](crud.TableName, dbExecutor, crud.GetByIDCallbacks),
-	})
+func WithDeleteMiddlewares[I any](middlewares []api.Handler) Option[I] {
+	return func(options *Options[I]) {
+		options.deleteMiddlewares = middlewares
+	}
+}
+
+func WithUpdateTx[I any]() Option[I] {
+	return func(options *Options[I]) {
+		options.updateNeedTx = true
+	}
+}
+
+func WithUpdateCallbacks[I any](callbacks *Callbacks[any]) Option[I] {
+	return func(options *Options[I]) {
+		options.updateCallbacks = callbacks
+	}
+}
+
+func WithUpdateMiddlewares[I any](middlewares []api.Handler) Option[I] {
+	return func(options *Options[I]) {
+		options.updateMiddlewares = middlewares
+	}
+}
+
+func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) Option[I] {
+	return func(options *Options[I]) {
+		options.queryConditionFieldCallback = callback
+	}
+}
+
+func WithQueryCallbacks[I any](callbacks *Callbacks[response.InfosData[I]]) Option[I] {
+	return func(options *Options[I]) {
+		options.queryCallbacks = callbacks
+	}
+}
+
+func WithQueryMiddlewares[I any](middlewares []api.Handler) Option[I] {
+	return func(options *Options[I]) {
+		options.queryMiddlewares = middlewares
+	}
+}
+
+func WithGetByIDCallbacks[I any](callbacks *Callbacks[I]) Option[I] {
+	return func(options *Options[I]) {
+		options.getByIDCallbacks = callbacks
+	}
 }
 
-type Option[O any] func(simple Simple[O])
+func WithGetByIDMiddlewares[I any](middlewares []api.Handler) Option[I] {
+	return func(options *Options[I]) {
+		options.getByIDMiddlewares = middlewares
+	}
+}

+ 1 - 1
examples/binding/main.go

@@ -159,7 +159,7 @@ func main() {
 		UpdateNeedTx:       false,
 		QueryParams:        &QueryClassesQueryParams{},
 		GetByIDQueryParams: &GetClassQueryParams{},
-	})
+	}, entity.WithCreateTx[ClassInfo]())
 
 	go func() {
 		err := app.Start()