| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package entity
- import (
- "git.sxidc.com/go-framework/baize/binding"
- "git.sxidc.com/go-framework/baize/binding/request"
- "git.sxidc.com/go-framework/baize/binding/response"
- "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 {
- // 使用的领域实体,注意是Entity类型
- Entity domain.Entity
- // 表名
- TableName string
- // 选择要使用的数据库Executor
- // DBExecutorOperations operations 数据库操作
- // DBExecutorDataService data_service 数据服务
- DBExecutorType string
- // URL领域相对路径,如/class,后面会自动补充
- DomainPath string
- // 创建使用的DTO
- CreateDTO request.DTO
- // 创建是否使用事务
- 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 ConditionFieldCallback
- // 查询回调
- QueryCallbacks *Callbacks[response.InfosData[O]]
- // 根据ID查询使用的DTO,注意是WithID类型
- GetByIDDTO request.WithID
- // 根据ID查询回调
- GetByIDCallbacks *Callbacks[O]
- }
- func (crud *Simple[O]) bind(binder *binding.Binder) {
- dbExecutor := binder.ChooseDBExecutor(crud.DBExecutorType)
- // 创建
- 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),
- })
- } 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),
- })
- }
- // 删除
- 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),
- })
- } 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),
- })
- }
- // 修改
- 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),
- })
- } 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),
- })
- }
- // 查询
- 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),
- })
- // 通过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),
- })
- }
|