api.go 3.6 KB

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