瀏覽代碼

网关工具实体crud和值对象crud添加成功回调选项

yjp 4 月之前
父節點
當前提交
87a6457bba
共有 2 個文件被更改,包括 125 次插入20 次删除
  1. 86 20
      convenient/gwtools/entity_crud/entity_crud.go
  2. 39 0
      convenient/gwtools/value_object_crud/value_object_crud.go

+ 86 - 20
convenient/gwtools/entity_crud/entity_crud.go

@@ -81,29 +81,34 @@ func (simple *Simple) bind(builder *gateway.Builder) {
 
 		builder.PostRouteWithTenantIDAndCreateUserInfo("/"+simple.ServiceShortName+domainPath+"/create",
 			func(requestBuilder *gateway.RequestBuilder) {
-				requestBuilder.Post(
-					&gateway.PostRequest{
-						Url: simple.ServiceVersionedUrl + domainPath + "/create",
-					},
-					gateway.WithBeforeRequestCallback(
-						func(requestBuilder *gateway.RequestBuilder) error {
-							if createOptions.beforeBuilderCallback != nil {
-								return createOptions.beforeBuilderCallback(requestBuilder)
-							}
+				if createOptions.responseSuccessCallback != nil {
+					requestBuilder.ResponseSuccessCallback(createOptions.responseSuccessCallback)
+				}
 
-							return nil
-						}),
-					gateway.WithRequestResponseCallback(
-						func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
-							if createOptions.afterBuilderCallback != nil {
-								err := createOptions.afterBuilderCallback(requestBuilder)
-								if err != nil {
-									return err
+				requestBuilder.
+					Post(
+						&gateway.PostRequest{
+							Url: simple.ServiceVersionedUrl + domainPath + "/create",
+						},
+						gateway.WithBeforeRequestCallback(
+							func(requestBuilder *gateway.RequestBuilder) error {
+								if createOptions.beforeBuilderCallback != nil {
+									return createOptions.beforeBuilderCallback(requestBuilder)
 								}
-							}
 
-							return nil
-						})).
+								return nil
+							}),
+						gateway.WithRequestResponseCallback(
+							func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
+								if createOptions.afterBuilderCallback != nil {
+									err := createOptions.afterBuilderCallback(requestBuilder)
+									if err != nil {
+										return err
+									}
+								}
+
+								return nil
+							})).
 					ResponseErrorCallback(func(requestBuilder *gateway.RequestBuilder, err error) {
 						response.SendIDResponse(requestBuilder.Context(), http.StatusOK, "", err)
 					}).
@@ -117,6 +122,10 @@ func (simple *Simple) bind(builder *gateway.Builder) {
 
 		builder.DeleteRouteWithDeleteUserInfo("/"+simple.ServiceShortName+domainPath+"/delete",
 			func(requestBuilder *gateway.RequestBuilder) {
+				if deleteOptions.responseSuccessCallback != nil {
+					requestBuilder.ResponseSuccessCallback(deleteOptions.responseSuccessCallback)
+				}
+
 				requestBuilder.
 					Delete(
 						&gateway.DeleteRequest{
@@ -154,6 +163,10 @@ func (simple *Simple) bind(builder *gateway.Builder) {
 
 		builder.PutRouteWithUpdateUserInfo("/"+simple.ServiceShortName+domainPath+"/update",
 			func(requestBuilder *gateway.RequestBuilder) {
+				if updateOptions.responseSuccessCallback != nil {
+					requestBuilder.ResponseSuccessCallback(updateOptions.responseSuccessCallback)
+				}
+
 				requestBuilder.
 					Put(
 						&gateway.PutRequest{
@@ -190,6 +203,10 @@ func (simple *Simple) bind(builder *gateway.Builder) {
 
 		builder.GetRouteWithTenantID("/"+simple.ServiceShortName+domainPath+"/query",
 			func(requestBuilder *gateway.RequestBuilder) {
+				if queryOptions.responseSuccessCallback != nil {
+					requestBuilder.ResponseSuccessCallback(queryOptions.responseSuccessCallback)
+				}
+
 				requestBuilder.
 					Get(
 						&gateway.GetRequest{
@@ -226,6 +243,10 @@ func (simple *Simple) bind(builder *gateway.Builder) {
 
 		builder.GetRoute("/"+simple.ServiceShortName+domainPath+"/get",
 			func(requestBuilder *gateway.RequestBuilder) {
+				if getByIDOptions.responseSuccessCallback != nil {
+					requestBuilder.ResponseSuccessCallback(getByIDOptions.responseSuccessCallback)
+				}
+
 				requestBuilder.
 					Get(
 						&gateway.GetRequest{
@@ -281,6 +302,9 @@ type CreateOptions struct {
 	// 创建请求后回调
 	afterBuilderCallback RequestBuilderCallback
 
+	// responseSuccessCallback 成功响应回调
+	responseSuccessCallback gateway.ResponseSuccessCallback
+
 	// 创建中间件
 	middlewares []gateway.Handler
 }
@@ -295,6 +319,9 @@ type DeleteOptions struct {
 	// 删除请求后回调
 	afterBuilderCallback RequestBuilderCallback
 
+	// responseSuccessCallback 成功响应回调
+	responseSuccessCallback gateway.ResponseSuccessCallback
+
 	// 删除中间件
 	middlewares []gateway.Handler
 }
@@ -309,6 +336,9 @@ type UpdateOptions struct {
 	// 更新请求后回调
 	afterBuilderCallback RequestBuilderCallback
 
+	// responseSuccessCallback 成功响应回调
+	responseSuccessCallback gateway.ResponseSuccessCallback
+
 	// 更新中间件
 	middlewares []gateway.Handler
 }
@@ -323,6 +353,9 @@ type QueryOptions struct {
 	// 查询请求后回调
 	afterBuilderCallback RequestBuilderCallback
 
+	// responseSuccessCallback 成功响应回调
+	responseSuccessCallback gateway.ResponseSuccessCallback
+
 	// 查询中间件
 	middlewares []gateway.Handler
 }
@@ -337,6 +370,9 @@ type GetByIDOptions struct {
 	// 根据ID请求后回调
 	afterBuilderCallback RequestBuilderCallback
 
+	// responseSuccessCallback 成功响应回调
+	responseSuccessCallback gateway.ResponseSuccessCallback
+
 	// 根据ID查询中间件
 	middlewares []gateway.Handler
 }
@@ -359,6 +395,12 @@ func WithAfterCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOpti
 	}
 }
 
+func WithCreateResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) CreateOption {
+	return func(options *CreateOptions) {
+		options.responseSuccessCallback = responseSuccessCallback
+	}
+}
+
 func WithCreateMiddlewares(middlewares ...gateway.Handler) CreateOption {
 	return func(options *CreateOptions) {
 		options.middlewares = middlewares
@@ -383,6 +425,12 @@ func WithAfterDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOpti
 	}
 }
 
+func WithDeleteResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) DeleteOption {
+	return func(options *DeleteOptions) {
+		options.responseSuccessCallback = responseSuccessCallback
+	}
+}
+
 func WithDeleteMiddlewares(middlewares ...gateway.Handler) DeleteOption {
 	return func(options *DeleteOptions) {
 		options.middlewares = middlewares
@@ -407,6 +455,12 @@ func WithAfterUpdateBuilderCallback(callbacks RequestBuilderCallback) UpdateOpti
 	}
 }
 
+func WithUpdateResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) UpdateOption {
+	return func(options *UpdateOptions) {
+		options.responseSuccessCallback = responseSuccessCallback
+	}
+}
+
 func WithUpdateMiddlewares(middlewares ...gateway.Handler) UpdateOption {
 	return func(options *UpdateOptions) {
 		options.middlewares = middlewares
@@ -431,6 +485,12 @@ func WithAfterQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption
 	}
 }
 
+func WithQueryResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) QueryOption {
+	return func(options *QueryOptions) {
+		options.responseSuccessCallback = responseSuccessCallback
+	}
+}
+
 func WithQueryMiddlewares(middlewares ...gateway.Handler) QueryOption {
 	return func(options *QueryOptions) {
 		options.middlewares = middlewares
@@ -455,6 +515,12 @@ func WithAfterGetByIDBuilderCallback(callbacks RequestBuilderCallback) GetByIDOp
 	}
 }
 
+func WithGetByIDResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) GetByIDOption {
+	return func(options *GetByIDOptions) {
+		options.responseSuccessCallback = responseSuccessCallback
+	}
+}
+
 func WithGetByIDMiddlewares(middlewares ...gateway.Handler) GetByIDOption {
 	return func(options *GetByIDOptions) {
 		options.middlewares = middlewares

+ 39 - 0
convenient/gwtools/value_object_crud/value_object_crud.go

@@ -67,6 +67,10 @@ func (simple *Simple) bind(builder *gateway.Builder) {
 
 		builder.PostRouteWithTenantIDAndCreateUserID("/"+simple.ServiceShortName+domainPath+"/create",
 			func(requestBuilder *gateway.RequestBuilder) {
+				if createOptions.responseSuccessCallback != nil {
+					requestBuilder.ResponseSuccessCallback(createOptions.responseSuccessCallback)
+				}
+
 				requestBuilder.
 					Post(
 						&gateway.PostRequest{
@@ -104,6 +108,10 @@ func (simple *Simple) bind(builder *gateway.Builder) {
 
 		builder.PostRoute("/"+simple.ServiceShortName+domainPath+"/delete",
 			func(requestBuilder *gateway.RequestBuilder) {
+				if deleteOptions.responseSuccessCallback != nil {
+					requestBuilder.ResponseSuccessCallback(deleteOptions.responseSuccessCallback)
+				}
+
 				requestBuilder.
 					Post(
 						&gateway.PostRequest{
@@ -141,6 +149,10 @@ func (simple *Simple) bind(builder *gateway.Builder) {
 
 		builder.GetRouteWithTenantID("/"+simple.ServiceShortName+domainPath+"/query",
 			func(requestBuilder *gateway.RequestBuilder) {
+				if queryOptions.responseSuccessCallback != nil {
+					requestBuilder.ResponseSuccessCallback(queryOptions.responseSuccessCallback)
+				}
+
 				requestBuilder.
 					Get(
 						&gateway.GetRequest{
@@ -194,6 +206,9 @@ type CreateOptions struct {
 	// 创建请求后回调
 	afterBuilderCallback RequestBuilderCallback
 
+	// responseSuccessCallback 成功响应回调
+	responseSuccessCallback gateway.ResponseSuccessCallback
+
 	// 创建中间件
 	middlewares []gateway.Handler
 }
@@ -208,6 +223,9 @@ type DeleteOptions struct {
 	// 删除请求后回调
 	afterBuilderCallback RequestBuilderCallback
 
+	// responseSuccessCallback 成功响应回调
+	responseSuccessCallback gateway.ResponseSuccessCallback
+
 	// 删除中间件
 	middlewares []gateway.Handler
 }
@@ -222,6 +240,9 @@ type QueryOptions struct {
 	// 查询请求后回调
 	afterBuilderCallback RequestBuilderCallback
 
+	// responseSuccessCallback 成功响应回调
+	responseSuccessCallback gateway.ResponseSuccessCallback
+
 	// 查询中间件
 	middlewares []gateway.Handler
 }
@@ -244,6 +265,12 @@ func WithAfterCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOpti
 	}
 }
 
+func WithCreateResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) CreateOption {
+	return func(options *CreateOptions) {
+		options.responseSuccessCallback = responseSuccessCallback
+	}
+}
+
 func WithCreateMiddlewares(middlewares ...gateway.Handler) CreateOption {
 	return func(options *CreateOptions) {
 		options.middlewares = middlewares
@@ -268,6 +295,12 @@ func WithAfterDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOpti
 	}
 }
 
+func WithDeleteResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) DeleteOption {
+	return func(options *DeleteOptions) {
+		options.responseSuccessCallback = responseSuccessCallback
+	}
+}
+
 func WithDeleteMiddlewares(middlewares ...gateway.Handler) DeleteOption {
 	return func(options *DeleteOptions) {
 		options.middlewares = middlewares
@@ -292,6 +325,12 @@ func WithAfterQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption
 	}
 }
 
+func WithQueryResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) QueryOption {
+	return func(options *QueryOptions) {
+		options.responseSuccessCallback = responseSuccessCallback
+	}
+}
+
 func WithQueryMiddlewares(middlewares ...gateway.Handler) QueryOption {
 	return func(options *QueryOptions) {
 		options.middlewares = middlewares