|
|
@@ -1,6 +1,7 @@
|
|
|
package entity
|
|
|
|
|
|
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"
|
|
|
@@ -16,58 +17,73 @@ type Simple[O any] struct {
|
|
|
// 使用的领域实体,注意是Entity类型
|
|
|
Entity domain.Entity
|
|
|
|
|
|
- // 表名
|
|
|
- TableName string
|
|
|
-
|
|
|
// 选择要使用的数据库Executor
|
|
|
// DBExecutorOperations operations 数据库操作
|
|
|
// DBExecutorDataService data_service 数据服务
|
|
|
DBExecutorType string
|
|
|
|
|
|
- // URL领域相对路径,如/class,后面会自动补充
|
|
|
- DomainPath string
|
|
|
+ // 创建使用的请求参数
|
|
|
+ CreateJsonBody request.Params
|
|
|
+
|
|
|
+ // 删除使用的请求参数,注意是WithID类型
|
|
|
+ DeleteQueryParams request.WithID
|
|
|
+
|
|
|
+ // 更新使用的请求参数,注意是WithID类型
|
|
|
+ UpdateJsonBody request.WithID
|
|
|
+
|
|
|
+ // 查询使用的请求参数,注意是Query类型
|
|
|
+ QueryParams request.Query
|
|
|
|
|
|
- // 创建使用的DTO
|
|
|
- CreateDTO request.DTO
|
|
|
+ // 根据ID查询使用的请求参数,注意是WithID类型
|
|
|
+ GetByIDQueryParams request.WithID
|
|
|
+
|
|
|
+ // 表名
|
|
|
+ tableName string
|
|
|
+
|
|
|
+ // URL领域相对路径,如/class,后面会自动补充
|
|
|
+ domainPath string
|
|
|
|
|
|
// 创建是否使用事务
|
|
|
- CreateNeedTx bool
|
|
|
+ createNeedTx bool
|
|
|
|
|
|
// 创建回调
|
|
|
- CreateCallbacks *Callbacks[string]
|
|
|
+ createCallbacks *Callbacks[string]
|
|
|
|
|
|
- // 删除使用的DTO,注意是WithID类型
|
|
|
- DeleteDTO request.WithID
|
|
|
+ // 创建中间件
|
|
|
+ createMiddlewares []api.Handler
|
|
|
|
|
|
// 删除是否使用事务
|
|
|
- DeleteNeedTx bool
|
|
|
+ deleteNeedTx bool
|
|
|
|
|
|
// 删除回调
|
|
|
- DeleteCallbacks *Callbacks[any]
|
|
|
+ deleteCallbacks *Callbacks[any]
|
|
|
|
|
|
- // 更新使用的DTO,注意是WithID类型
|
|
|
- UpdateDTO request.WithID
|
|
|
+ // 删除中间件
|
|
|
+ deleteMiddlewares []api.Handler
|
|
|
|
|
|
- // 根性是否使用事务
|
|
|
- UpdateNeedTx bool
|
|
|
+ // 更新是否使用事务
|
|
|
+ updateNeedTx bool
|
|
|
|
|
|
// 更新回调
|
|
|
- UpdateCallbacks *Callbacks[any]
|
|
|
+ updateCallbacks *Callbacks[any]
|
|
|
|
|
|
- // 查询使用的DTO,注意是Query类型
|
|
|
- QueryDTO request.Query
|
|
|
+ // 更新中间件
|
|
|
+ updateMiddlewares []api.Handler
|
|
|
|
|
|
// 查询条件构造回调
|
|
|
- QueryConditionFieldCallback ConditionFieldCallback
|
|
|
+ queryConditionFieldCallback ConditionFieldCallback
|
|
|
|
|
|
// 查询回调
|
|
|
- QueryCallbacks *Callbacks[response.InfosData[O]]
|
|
|
+ queryCallbacks *Callbacks[response.InfosData[O]]
|
|
|
|
|
|
- // 根据ID查询使用的DTO,注意是WithID类型
|
|
|
- GetByIDDTO request.WithID
|
|
|
+ // 查询中间件
|
|
|
+ queryMiddlewares []api.Handler
|
|
|
|
|
|
// 根据ID查询回调
|
|
|
- GetByIDCallbacks *Callbacks[O]
|
|
|
+ getByIDCallbacks *Callbacks[O]
|
|
|
+
|
|
|
+ // 根据ID查询中间件
|
|
|
+ getByIDMiddlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
func (crud *Simple[O]) bind(binder *binding.Binder) {
|
|
|
@@ -76,75 +92,77 @@ func (crud *Simple[O]) bind(binder *binding.Binder) {
|
|
|
// 创建
|
|
|
if !crud.CreateNeedTx {
|
|
|
binding.PostBind(binder, &binding.SimpleBindItem[string]{
|
|
|
- Path: crud.DomainPath + "/create",
|
|
|
- ResponseFunc: response.SendIDResponse[string],
|
|
|
- DTO: crud.CreateDTO,
|
|
|
- Objects: []domain.Object{crud.Entity},
|
|
|
- ServiceFunc: Create(crud.TableName, dbExecutor, crud.CreateCallbacks),
|
|
|
+ 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],
|
|
|
- DTO: crud.CreateDTO,
|
|
|
- Objects: []domain.Object{crud.Entity},
|
|
|
- ServiceFunc: CreateTx(crud.TableName, dbExecutor, crud.CreateCallbacks),
|
|
|
+ Path: crud.DomainPath + "/create",
|
|
|
+ ResponseFunc: response.SendIDResponse[string],
|
|
|
+ RequestParams: crud.CreateJsonBody,
|
|
|
+ Objects: []domain.Object{crud.Entity},
|
|
|
+ ServiceFunc: CreateTx(crud.TableName, dbExecutor, crud.CreateCallbacks),
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// 删除
|
|
|
if !crud.DeleteNeedTx {
|
|
|
binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
|
|
|
- Path: crud.DomainPath + "/:id/delete",
|
|
|
- ResponseFunc: response.SendMsgResponse,
|
|
|
- DTO: crud.DeleteDTO,
|
|
|
- Objects: []domain.Object{crud.Entity},
|
|
|
- ServiceFunc: Delete(crud.TableName, dbExecutor, crud.DeleteCallbacks),
|
|
|
+ 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,
|
|
|
- DTO: crud.DeleteDTO,
|
|
|
- Objects: []domain.Object{crud.Entity},
|
|
|
- ServiceFunc: DeleteTx(crud.TableName, dbExecutor, crud.DeleteCallbacks),
|
|
|
+ Path: crud.DomainPath + "/:id/delete",
|
|
|
+ ResponseFunc: response.SendMsgResponse,
|
|
|
+ RequestParams: crud.DeleteQueryParams,
|
|
|
+ Objects: []domain.Object{crud.Entity},
|
|
|
+ ServiceFunc: DeleteTx(crud.TableName, dbExecutor, crud.DeleteCallbacks),
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// 修改
|
|
|
if !crud.UpdateNeedTx {
|
|
|
binding.PutBind(binder, &binding.SimpleBindItem[any]{
|
|
|
- Path: crud.DomainPath + "/update",
|
|
|
- ResponseFunc: response.SendMsgResponse,
|
|
|
- DTO: crud.UpdateDTO,
|
|
|
- Objects: []domain.Object{crud.Entity},
|
|
|
- ServiceFunc: Update(crud.TableName, dbExecutor, crud.UpdateCallbacks),
|
|
|
+ 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,
|
|
|
- DTO: crud.UpdateDTO,
|
|
|
- Objects: []domain.Object{crud.Entity},
|
|
|
- ServiceFunc: UpdateTx(crud.TableName, dbExecutor, crud.UpdateCallbacks),
|
|
|
+ Path: crud.DomainPath + "/update",
|
|
|
+ ResponseFunc: response.SendMsgResponse,
|
|
|
+ RequestParams: crud.UpdateJsonBody,
|
|
|
+ Objects: []domain.Object{crud.Entity},
|
|
|
+ ServiceFunc: UpdateTx(crud.TableName, dbExecutor, crud.UpdateCallbacks),
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// 查询
|
|
|
binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[O]]{
|
|
|
- Path: crud.DomainPath + "/query",
|
|
|
- ResponseFunc: response.SendInfosResponse[O],
|
|
|
- DTO: crud.QueryDTO,
|
|
|
- Objects: []domain.Object{crud.Entity},
|
|
|
- ServiceFunc: Query[O](crud.TableName, dbExecutor, crud.QueryCallbacks, crud.QueryConditionFieldCallback),
|
|
|
+ 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),
|
|
|
})
|
|
|
|
|
|
// 通过ID获取
|
|
|
binding.GetBind(binder, &binding.SimpleBindItem[O]{
|
|
|
- Path: crud.DomainPath + "/get",
|
|
|
- ResponseFunc: response.SendInfoResponse[O],
|
|
|
- DTO: crud.GetByIDDTO,
|
|
|
- Objects: []domain.Object{crud.Entity},
|
|
|
- ServiceFunc: GetByID[O](crud.TableName, dbExecutor, crud.GetByIDCallbacks),
|
|
|
+ Path: crud.DomainPath + "/get",
|
|
|
+ ResponseFunc: response.SendInfoResponse[O],
|
|
|
+ RequestParams: crud.GetByIDQueryParams,
|
|
|
+ Objects: []domain.Object{crud.Entity},
|
|
|
+ ServiceFunc: GetByID[O](crud.TableName, dbExecutor, crud.GetByIDCallbacks),
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+type Option[O any] func(simple Simple[O])
|