api.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. SelectClauses: []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. err = sql.ParseSqlResult(results, &values)
  72. if err != nil {
  73. return map[string]any{
  74. "values": make([]string, 0),
  75. }, err
  76. }
  77. return map[string]any{
  78. "values": values,
  79. }, nil
  80. },
  81. })
  82. }
  83. }
  84. func BindConfiguration(app *application.App, simple *Simple, opts ...Option) {
  85. options := new(Options)
  86. for _, opt := range opts {
  87. opt(options)
  88. }
  89. simple.options = options
  90. binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
  91. simple.bind(binder)
  92. }
  93. type Option func(options *Options)
  94. type Options struct {
  95. // 创建删除
  96. disableCreate bool
  97. // 关闭删除
  98. disableDelete bool
  99. // 关闭查询
  100. disableQuery bool
  101. }
  102. func WithDisableCreate() Option {
  103. return func(options *Options) {
  104. options.disableCreate = true
  105. }
  106. }
  107. func WithDisableDelete() Option {
  108. return func(options *Options) {
  109. options.disableDelete = true
  110. }
  111. }
  112. func WithDisableQuery() Option {
  113. return func(options *Options) {
  114. options.disableQuery = true
  115. }
  116. }