api.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package query_rule
  2. import (
  3. "git.sxidc.com/go-framework/baize/convenient/value_object_crud"
  4. "git.sxidc.com/go-framework/baize/framework/binding"
  5. "git.sxidc.com/go-framework/baize/framework/core/api"
  6. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  7. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  8. "git.sxidc.com/go-framework/baize/framework/core/application"
  9. "git.sxidc.com/go-framework/baize/framework/core/domain"
  10. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  11. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  12. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  13. )
  14. // Simple Bind参数
  15. type Simple struct {
  16. // schema
  17. Schema string
  18. }
  19. func (simple *Simple) bind(binder *binding.Binder) {
  20. queryRuleTableName := domain.TableName(simple.Schema, &ValueObject{})
  21. value_object_crud.BindSimple(binder, &value_object_crud.Simple[any]{
  22. ValueObject: &ValueObject{},
  23. Schema: simple.Schema,
  24. CreateJsonBody: &AddQueryRuleJsonBody{},
  25. DeleteJsonBody: &RemoveQueryRuleJsonBody{},
  26. }, value_object_crud.WithDisableQuery[any]())
  27. binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
  28. Path: "/queryRuleDefinition/get",
  29. SendResponseFunc: response.SendMapResponse,
  30. RequestParams: &GetQueryRuleDefinitionQueryParams{},
  31. Objects: []domain.Object{&ValueObject{}},
  32. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  33. errResponse := map[string]any{
  34. "definition": make([]QueryRuleDefinitionItem, 0),
  35. }
  36. valueObject, err := domain.ToConcrete[*ValueObject](objects[0])
  37. if err != nil {
  38. return errResponse, err
  39. }
  40. return map[string]any{
  41. "definition": GetQueryRuleDefinition(valueObject.RuleDomainName),
  42. }, nil
  43. },
  44. })
  45. binding.PostBind(binder, &binding.SimpleBindItem[Info]{
  46. Path: "/queryRule/get",
  47. SendResponseFunc: response.SendInfoResponse[Info],
  48. RequestParams: &GetQueryRuleQueryParams{},
  49. Objects: []domain.Object{&ValueObject{}},
  50. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (Info, error) {
  51. dbExecutor := i.DBExecutor()
  52. valueObject, err := domain.ToConcrete[*ValueObject](objects[0])
  53. if err != nil {
  54. return Info{}, err
  55. }
  56. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  57. TableName: queryRuleTableName,
  58. Conditions: sql.NewConditions().
  59. Equal(ColumnScope, valueObject.Scope).
  60. Equal(ColumnDomainName, valueObject.RuleDomainName),
  61. })
  62. if err != nil {
  63. return Info{}, err
  64. }
  65. info := new(Info)
  66. err = sql.ParseSqlResult(result, info)
  67. if err != nil {
  68. return Info{}, err
  69. }
  70. return *info, nil
  71. },
  72. })
  73. }
  74. func Bind(app *application.App, simple *Simple) {
  75. binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
  76. simple.bind(binder)
  77. }