api.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package permission_group
  2. import (
  3. "git.sxidc.com/go-framework/baize/convenient/entity_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/domain"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  9. )
  10. // Simple Bind参数
  11. type Simple struct {
  12. // schema
  13. Schema string
  14. // 鉴权中间件
  15. AuthMiddleware binding.Middleware
  16. }
  17. func (simple *Simple) Bind(binder *binding.Binder) {
  18. entity_crud.BindSimple(binder, &entity_crud.Simple[Info]{
  19. Entity: &Entity{},
  20. Schema: simple.Schema,
  21. CreateJsonBody: &CreatePermissionGroupJsonBody{},
  22. DeleteQueryParams: &DeletePermissionGroupQueryParams{},
  23. UpdateJsonBody: &UpdatePermissionGroupJsonBody{},
  24. QueryQueryParams: &GetPermissionGroupsQueryParams{},
  25. GetByIDQueryParams: &GetPermissionGroupQueryParams{},
  26. }, entity_crud.WithGlobalMiddlewares(simple.AuthMiddleware),
  27. entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
  28. Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
  29. userInfo := c.GetUserInfo()
  30. err := domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID())
  31. if err != nil {
  32. return nil, err
  33. }
  34. return nil, err
  35. },
  36. }), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
  37. Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
  38. userInfo := c.GetUserInfo()
  39. err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID())
  40. if err != nil {
  41. return nil, err
  42. }
  43. return nil, err
  44. },
  45. }))
  46. }