api.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package configuration
  2. import (
  3. "git.sxidc.com/go-framework/baize/api"
  4. "git.sxidc.com/go-framework/baize/binding"
  5. "git.sxidc.com/go-framework/baize/binding/request"
  6. "git.sxidc.com/go-framework/baize/binding/response"
  7. "git.sxidc.com/go-framework/baize/convenient/value_object"
  8. "git.sxidc.com/go-framework/baize/domain"
  9. "git.sxidc.com/go-framework/baize/infrastructure"
  10. "git.sxidc.com/go-framework/baize/infrastructure/database"
  11. "git.sxidc.com/go-framework/baize/infrastructure/database/sql"
  12. "git.sxidc.com/go-tools/utils/strutils"
  13. "git.sxidc.com/service-supports/fserr"
  14. )
  15. func BindConfiguration(binder *binding.Binder, opts ...Option) {
  16. options := new(Options)
  17. for _, opt := range opts {
  18. opt(options)
  19. }
  20. configurationTableName := tableName
  21. if strutils.IsStringNotEmpty(options.schema) {
  22. configurationTableName = options.schema + "." + tableName
  23. }
  24. value_object.BindSimple(binder, &value_object.Simple[any]{
  25. ValueObject: &Entity{},
  26. TableName: configurationTableName,
  27. DomainPath: "/configuration",
  28. CreateJsonBody: &AddConfigurationJsonBody{},
  29. DeleteQueryParams: &RemoveConfigurationJsonBody{},
  30. }, value_object.WithDisableQuery[any]())
  31. binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
  32. Path: "/configuration/values",
  33. ResponseFunc: response.SendMapResponse,
  34. RequestParams: &GetConfigurationValuesQueryParams{},
  35. Objects: []domain.Object{&Entity{}},
  36. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  37. dbExecutor := i.DBExecutor()
  38. e, ok := objects[0].(*Entity)
  39. if !ok {
  40. return map[string]any{
  41. "values": make([]string, 0),
  42. }, fserr.New("传递的实体不是该领域的实体")
  43. }
  44. conditions := sql.NewConditions()
  45. if strutils.IsStringNotEmpty(e.Scope) {
  46. conditions.Equal(ColumnScope, e.Scope)
  47. }
  48. if strutils.IsStringNotEmpty(e.Group) {
  49. conditions.Equal(ColumnGroup, e.Group)
  50. }
  51. results, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  52. TableName: configurationTableName,
  53. SelectColumns: []string{ColumnValue},
  54. Conditions: conditions,
  55. })
  56. if err != nil {
  57. return map[string]any{
  58. "values": make([]string, 0),
  59. }, err
  60. }
  61. values := make([]string, 0)
  62. for _, result := range results {
  63. values = append(values, result.ColumnValueString(ColumnValue))
  64. }
  65. return map[string]any{
  66. "values": values,
  67. }, nil
  68. },
  69. })
  70. }
  71. type Option func(options *Options)
  72. type Options struct {
  73. schema string
  74. }
  75. func WithSchema(schema string) Option {
  76. return func(options *Options) {
  77. options.schema = schema
  78. }
  79. }