api.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }
  21. func (simple *Simple) bind(binder *binding.Binder) {
  22. configurationTableName := domain.TableName(simple.Schema, &ValueObject{})
  23. value_object_crud.BindSimple(binder, &value_object_crud.Simple[any]{
  24. ValueObject: &ValueObject{},
  25. Schema: simple.Schema,
  26. CreateJsonBody: &AddConfigurationJsonBody{},
  27. DeleteJsonBody: &RemoveConfigurationJsonBody{},
  28. }, value_object_crud.WithDisableQuery[any]())
  29. binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
  30. Path: "/configuration/values",
  31. SendResponseFunc: response.SendMapResponse,
  32. RequestParams: &GetConfigurationValuesQueryParams{},
  33. Objects: []domain.Object{&ValueObject{}},
  34. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  35. dbExecutor := i.DBExecutor()
  36. e, ok := objects[0].(*ValueObject)
  37. if !ok {
  38. return map[string]any{
  39. "values": make([]string, 0),
  40. }, errors.New("传递的实体不是该领域的实体")
  41. }
  42. conditions := sql.NewConditions()
  43. if strutils.IsStringNotEmpty(e.Scope) {
  44. conditions.Equal(ColumnScope, e.Scope)
  45. }
  46. if strutils.IsStringNotEmpty(e.Group) {
  47. conditions.Equal(ColumnGroup, e.Group)
  48. }
  49. results, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  50. TableName: configurationTableName,
  51. SelectClauses: []string{ColumnValue},
  52. Conditions: conditions,
  53. })
  54. if err != nil {
  55. return map[string]any{
  56. "values": make([]string, 0),
  57. }, err
  58. }
  59. values := make([]string, 0)
  60. err = sql.ParseSqlResult(results, &values)
  61. if err != nil {
  62. return map[string]any{
  63. "values": make([]string, 0),
  64. }, err
  65. }
  66. return map[string]any{
  67. "values": values,
  68. }, nil
  69. },
  70. })
  71. }
  72. func Bind(app *application.App, simple *Simple) {
  73. binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
  74. simple.bind(binder)
  75. }