api.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package permission
  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. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  10. )
  11. // Simple Bind参数
  12. type Simple struct {
  13. // schema
  14. Schema string
  15. // 鉴权中间件
  16. AuthMiddleware binding.Middleware
  17. }
  18. func (simple *Simple) Bind(binder *binding.Binder) {
  19. entity_crud.BindSimple(binder, &entity_crud.Simple[Info]{
  20. Entity: &Entity{},
  21. Schema: simple.Schema,
  22. CreateJsonBody: &CreatePermissionJsonBody{},
  23. DeleteQueryParams: &DeletePermissionQueryParams{},
  24. UpdateJsonBody: &UpdatePermissionJsonBody{},
  25. QueryQueryParams: &GetPermissionsQueryParams{},
  26. GetByIDQueryParams: &GetPermissionQueryParams{},
  27. }, entity_crud.WithGlobalMiddlewares(simple.AuthMiddleware),
  28. entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
  29. Before: func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  30. userInfo := c.GetUserInfo()
  31. return domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID())
  32. },
  33. }), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
  34. Before: func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  35. userInfo := c.GetUserInfo()
  36. return domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID())
  37. },
  38. }))
  39. }