api.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package configuration
  2. import (
  3. "git.sxidc.com/go-framework/baize/convenient/binding"
  4. "git.sxidc.com/go-framework/baize/convenient/binding/request"
  5. "git.sxidc.com/go-framework/baize/convenient/binding/response"
  6. "git.sxidc.com/go-framework/baize/convenient/value_object"
  7. "git.sxidc.com/go-framework/baize/framwork/api"
  8. "git.sxidc.com/go-framework/baize/framwork/domain"
  9. domainValueObject "git.sxidc.com/go-framework/baize/framwork/domain/value_object"
  10. "git.sxidc.com/go-framework/baize/framwork/infrastructure"
  11. "git.sxidc.com/go-framework/baize/framwork/infrastructure/database"
  12. "git.sxidc.com/go-framework/baize/framwork/infrastructure/database/sql"
  13. "git.sxidc.com/go-tools/utils/strutils"
  14. "git.sxidc.com/service-supports/fserr"
  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 := domainValueObject.TableName(simple.Schema, &Entity{})
  26. valueObjectOptions := []value_object.Option[any]{value_object.WithDisableQuery[any]()}
  27. if options.disableCreate {
  28. valueObjectOptions = append(valueObjectOptions, value_object.WithDisableCreate[any]())
  29. }
  30. if options.disableDelete {
  31. valueObjectOptions = append(valueObjectOptions, value_object.WithDisableDelete[any]())
  32. }
  33. value_object.BindSimple(binder, &value_object.Simple[any]{
  34. ValueObject: &Entity{},
  35. Schema: simple.Schema,
  36. CreateJsonBody: &AddConfigurationJsonBody{},
  37. DeleteQueryParams: &RemoveConfigurationJsonBody{},
  38. }, valueObjectOptions...)
  39. if !options.disableQuery {
  40. binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
  41. Path: "/configuration/values",
  42. ResponseFunc: response.SendMapResponse,
  43. RequestParams: &GetConfigurationValuesQueryParams{},
  44. Objects: []domain.Object{&Entity{}},
  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].(*Entity)
  48. if !ok {
  49. return map[string]any{
  50. "values": make([]string, 0),
  51. }, fserr.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(binder *binding.Binder, simple *Simple, opts ...Option) {
  82. options := new(Options)
  83. for _, opt := range opts {
  84. opt(options)
  85. }
  86. simple.options = options
  87. simple.bind(binder)
  88. }
  89. type Option func(options *Options)
  90. type Options struct {
  91. // 创建删除
  92. disableCreate bool
  93. // 关闭删除
  94. disableDelete bool
  95. // 关闭查询
  96. disableQuery bool
  97. }
  98. func WithDisableCreate() Option {
  99. return func(options *Options) {
  100. options.disableCreate = true
  101. }
  102. }
  103. func WithDisableDelete() Option {
  104. return func(options *Options) {
  105. options.disableDelete = true
  106. }
  107. }
  108. func WithDisableQuery() Option {
  109. return func(options *Options) {
  110. options.disableQuery = true
  111. }
  112. }