api.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. 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. results, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  45. TableName: configurationTableName,
  46. SelectColumns: []string{ColumnValue},
  47. Conditions: sql.NewConditions().
  48. Equal(ColumnGroup, e.Group),
  49. })
  50. if err != nil {
  51. return map[string]any{
  52. "values": make([]string, 0),
  53. }, err
  54. }
  55. values := make([]string, 0)
  56. for _, result := range results {
  57. values = append(values, result.ColumnValueString(ColumnValue))
  58. }
  59. return map[string]any{
  60. "values": values,
  61. }, nil
  62. },
  63. })
  64. }
  65. type Option func(options *Options)
  66. type Options struct {
  67. schema string
  68. }
  69. func WithSchema(schema string) Option {
  70. return func(options *Options) {
  71. options.schema = schema
  72. }
  73. }