api.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 = tableName + "." + options.schema
  23. }
  24. value_object.BindSimple(binder, &value_object.Simple[any]{
  25. ValueObject: &Entity{},
  26. TableName: configurationTableName,
  27. DomainPath: "/configuration",
  28. CreateJsonBody: &AddConfigurationJsonBody{},
  29. DeleteQueryParams: &RemoveConfigurationQueryParams{},
  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. Infrastructure: binder.Infrastructure(),
  37. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  38. dbExecutor := i.DBExecutor()
  39. e, ok := objects[0].(*Entity)
  40. if !ok {
  41. return map[string]any{
  42. "values": make([]string, 0),
  43. }, fserr.New("传递的实体不是该领域的实体")
  44. }
  45. results, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  46. TableName: configurationTableName,
  47. SelectColumns: []string{ColumnValue},
  48. Conditions: sql.NewConditions().
  49. Equal(ColumnGroup, e.Group),
  50. })
  51. if err != nil {
  52. return map[string]any{
  53. "values": make([]string, 0),
  54. }, err
  55. }
  56. values := make([]string, 0)
  57. for _, result := range results {
  58. values = append(values, result.ColumnValueString(ColumnValue))
  59. }
  60. return map[string]any{
  61. "values": values,
  62. }, nil
  63. },
  64. })
  65. }
  66. type Option func(options *Options)
  67. type Options struct {
  68. schema string
  69. }
  70. func WithSchema(schema string) Option {
  71. return func(options *Options) {
  72. options.schema = schema
  73. }
  74. }