Browse Source

添加回调

yjp 1 year ago
parent
commit
7325431da3

+ 1 - 1
binding/entity_crud/service.go

@@ -138,7 +138,7 @@ func CommonEntityUpdate(tableName string, dbExecutor database.Executor, callback
 	}
 }
 
-func CommonEntityQuery[O any](tableName string, dbExecutor database.Executor, callbacks *Callbacks[response.InfosData[O]], conditionFieldCallback domain.ConditionFieldCallback) binding.ServiceFunc[response.InfosData[O]] {
+func CommonEntityQuery[O any](tableName string, dbExecutor database.Executor, callbacks *Callbacks[response.InfosData[O]], conditionFieldCallback *domain.ConditionFieldCallback) binding.ServiceFunc[response.InfosData[O]] {
 	return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
 		queryDTO, ok := dto.(request.Query)
 		if !ok {

+ 0 - 0
binding/entity_crud/callbacks.go → binding/entity_crud/service_callbacks.go


+ 26 - 8
binding/entity_crud/simple.go

@@ -33,23 +33,41 @@ type Simple[O any] struct {
 	// 创建是否使用事务
 	CreateNeedTx bool
 
+	// 创建回调
+	CreateCallbacks *Callbacks[string]
+
 	// 删除使用的DTO,注意是WithID类型
 	DeleteDTO request.WithID
 
 	// 删除是否使用事务
 	DeleteNeedTx bool
 
+	// 删除回调
+	DeleteCallbacks *Callbacks[any]
+
 	// 更新使用的DTO,注意是WithID类型
 	UpdateDTO request.WithID
 
 	// 根性是否使用事务
 	UpdateNeedTx bool
 
+	// 更新回调
+	UpdateCallbacks *Callbacks[any]
+
 	// 查询使用的DTO,注意是Query类型
 	QueryDTO request.Query
 
+	// 查询条件构造回调
+	QueryConditionFieldCallback *domain.ConditionFieldCallback
+
+	// 查询回调
+	QueryCallbacks *Callbacks[response.InfosData[O]]
+
 	// 根据ID查询使用的DTO,注意是WithID类型
 	QueryByIDDTO request.WithID
+
+	// 根据ID查询回调
+	QueryByIDCallbacks *Callbacks[O]
 }
 
 func (crud *Simple[O]) bind(binder *binding.Binder) {
@@ -62,7 +80,7 @@ func (crud *Simple[O]) bind(binder *binding.Binder) {
 			ResponseFunc: response.SendIDResponse[string],
 			DTO:          crud.CreateDTO,
 			Objects:      []domain.Object{crud.Entity},
-			ServiceFunc:  CommonEntityCreate(crud.TableName, dbExecutor, nil),
+			ServiceFunc:  CommonEntityCreate(crud.TableName, dbExecutor, crud.CreateCallbacks),
 		})
 	} else {
 		binding.PostBind(binder, &binding.SimpleBindItem[string]{
@@ -70,7 +88,7 @@ func (crud *Simple[O]) bind(binder *binding.Binder) {
 			ResponseFunc: response.SendIDResponse[string],
 			DTO:          crud.CreateDTO,
 			Objects:      []domain.Object{crud.Entity},
-			ServiceFunc:  CommonEntityCreateTx(crud.TableName, dbExecutor, nil),
+			ServiceFunc:  CommonEntityCreateTx(crud.TableName, dbExecutor, crud.CreateCallbacks),
 		})
 	}
 
@@ -81,7 +99,7 @@ func (crud *Simple[O]) bind(binder *binding.Binder) {
 			ResponseFunc: response.SendMsgResponse,
 			DTO:          crud.DeleteDTO,
 			Objects:      []domain.Object{crud.Entity},
-			ServiceFunc:  CommonEntityDelete(crud.TableName, dbExecutor, nil),
+			ServiceFunc:  CommonEntityDelete(crud.TableName, dbExecutor, crud.DeleteCallbacks),
 		})
 	} else {
 		binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
@@ -89,7 +107,7 @@ func (crud *Simple[O]) bind(binder *binding.Binder) {
 			ResponseFunc: response.SendMsgResponse,
 			DTO:          crud.DeleteDTO,
 			Objects:      []domain.Object{crud.Entity},
-			ServiceFunc:  CommonEntityDeleteTx(crud.TableName, dbExecutor, nil),
+			ServiceFunc:  CommonEntityDeleteTx(crud.TableName, dbExecutor, crud.DeleteCallbacks),
 		})
 	}
 
@@ -100,7 +118,7 @@ func (crud *Simple[O]) bind(binder *binding.Binder) {
 			ResponseFunc: response.SendMsgResponse,
 			DTO:          crud.UpdateDTO,
 			Objects:      []domain.Object{crud.Entity},
-			ServiceFunc:  CommonEntityUpdate(crud.TableName, dbExecutor, nil),
+			ServiceFunc:  CommonEntityUpdate(crud.TableName, dbExecutor, crud.UpdateCallbacks),
 		})
 	} else {
 		binding.PutBind(binder, &binding.SimpleBindItem[any]{
@@ -108,7 +126,7 @@ func (crud *Simple[O]) bind(binder *binding.Binder) {
 			ResponseFunc: response.SendMsgResponse,
 			DTO:          crud.UpdateDTO,
 			Objects:      []domain.Object{crud.Entity},
-			ServiceFunc:  CommonEntityUpdateTx(crud.TableName, dbExecutor, nil),
+			ServiceFunc:  CommonEntityUpdateTx(crud.TableName, dbExecutor, crud.UpdateCallbacks),
 		})
 	}
 
@@ -118,7 +136,7 @@ func (crud *Simple[O]) bind(binder *binding.Binder) {
 		ResponseFunc: response.SendInfosResponse[O],
 		DTO:          crud.QueryDTO,
 		Objects:      []domain.Object{crud.Entity},
-		ServiceFunc:  CommonEntityQuery[O](crud.TableName, dbExecutor, nil, nil),
+		ServiceFunc:  CommonEntityQuery[O](crud.TableName, dbExecutor, crud.QueryCallbacks, crud.QueryConditionFieldCallback),
 	})
 
 	// 通过ID获取班级
@@ -127,6 +145,6 @@ func (crud *Simple[O]) bind(binder *binding.Binder) {
 		ResponseFunc: response.SendInfoResponse[O],
 		DTO:          crud.QueryByIDDTO,
 		Objects:      []domain.Object{crud.Entity},
-		ServiceFunc:  CommonEntityQueryByID[O](crud.TableName, dbExecutor, nil),
+		ServiceFunc:  CommonEntityQueryByID[O](crud.TableName, dbExecutor, crud.QueryByIDCallbacks),
 	})
 }