api.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package configuration
  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. "git.sxidc.com/go-tools/utils/strutils"
  14. "github.com/pkg/errors"
  15. )
  16. // Simple Bind参数
  17. type Simple struct {
  18. // schema
  19. Schema string
  20. // 可选配置项,通过WithXXX配置
  21. options *Options
  22. }
  23. func (simple *Simple) bind(binder *binding.Binder) {
  24. options := simple.options
  25. configurationTableName := domain.TableName(simple.Schema, &ValueObject{})
  26. valueObjectOptions := []any{value_object_crud.WithDisableQuery[any]()}
  27. if options.disableCreate {
  28. valueObjectOptions = append(valueObjectOptions, value_object_crud.WithDisableCreate())
  29. }
  30. if options.disableDelete {
  31. valueObjectOptions = append(valueObjectOptions, value_object_crud.WithDisableDelete())
  32. }
  33. value_object_crud.BindSimple(binder, &value_object_crud.Simple[any]{
  34. ValueObject: &ValueObject{},
  35. Schema: simple.Schema,
  36. CreateJsonBody: &AddConfigurationJsonBody{},
  37. DeleteJsonBody: &RemoveConfigurationJsonBody{},
  38. }, valueObjectOptions...)
  39. if !options.disableQuery {
  40. binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
  41. Path: "/configuration/values",
  42. SendResponseFunc: response.SendMapResponse,
  43. RequestParams: &GetConfigurationValuesQueryParams{},
  44. Objects: []domain.Object{&ValueObject{}},
  45. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  46. dbExecutor := i.DBExecutor()
  47. e, ok := objects[0].(*ValueObject)
  48. if !ok {
  49. return map[string]any{
  50. "values": make([]string, 0),
  51. }, errors.New("传递的实体不是该领域的实体")
  52. }
  53. conditions := sql.NewConditions()
  54. if strutils.IsStringNotEmpty(e.Scope) {
  55. conditions.Equal(ColumnScope, e.Scope)
  56. }
  57. if strutils.IsStringNotEmpty(e.Group) {
  58. conditions.Equal(ColumnGroup, e.Group)
  59. }
  60. results, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  61. TableName: configurationTableName,
  62. SelectColumns: []string{ColumnValue},
  63. Conditions: conditions,
  64. })
  65. if err != nil {
  66. return map[string]any{
  67. "values": make([]string, 0),
  68. }, err
  69. }
  70. values := make([]string, 0)
  71. for _, result := range results {
  72. values = append(values, result.ColumnValueString(ColumnValue))
  73. }
  74. return map[string]any{
  75. "values": values,
  76. }, nil
  77. },
  78. })
  79. }
  80. }
  81. func BindConfiguration(app *application.App, simple *Simple, opts ...Option) {
  82. options := new(Options)
  83. for _, opt := range opts {
  84. opt(options)
  85. }
  86. simple.options = options
  87. binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
  88. simple.bind(binder)
  89. }
  90. type Option func(options *Options)
  91. type Options struct {
  92. // 创建删除
  93. disableCreate bool
  94. // 关闭删除
  95. disableDelete bool
  96. // 关闭查询
  97. disableQuery bool
  98. }
  99. func WithDisableCreate() Option {
  100. return func(options *Options) {
  101. options.disableCreate = true
  102. }
  103. }
  104. func WithDisableDelete() Option {
  105. return func(options *Options) {
  106. options.disableDelete = true
  107. }
  108. }
  109. func WithDisableQuery() Option {
  110. return func(options *Options) {
  111. options.disableQuery = true
  112. }
  113. }