123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- package entity_crud
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-framework/baize/framework/core/api/response"
- "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
- "git.sxidc.com/go-tools/utils/reflectutils"
- )
- type CreateCallbacks struct {
- Prepare func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error)
- Before func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
- After func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
- OnSuccessReturn func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, id string) (string, error)
- OnErrorReturn func(c *api.Context, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) (string, error)
- }
- func callbackPrepareCreate(callbacks *CreateCallbacks, c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
- if callbacks == nil {
- return make(map[string]any), nil
- }
- if callbacks.Prepare == nil {
- return make(map[string]any), nil
- }
- return callbacks.Prepare(c, e, i)
- }
- func callbackBeforeCreate(callbacks *CreateCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.Before == nil {
- return nil
- }
- return callbacks.Before(c, e, prepared, i, tx)
- }
- func callbackAfterCreate(callbacks *CreateCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, dbExecutor database.Executor) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.After == nil {
- return nil
- }
- return callbacks.After(c, e, prepared, i, dbExecutor)
- }
- func callbackOnCreateSuccessReturn(callbacks *CreateCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, id string) (string, error) {
- if callbacks == nil {
- return id, nil
- }
- if callbacks.OnSuccessReturn == nil {
- return id, nil
- }
- return callbacks.OnSuccessReturn(c, e, prepared, i, id)
- }
- func callbackOnCreateErrorReturn(callbacks *CreateCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) (string, error) {
- if callbacks == nil {
- return "", err
- }
- if callbacks.OnErrorReturn == nil {
- return "", err
- }
- return callbacks.OnErrorReturn(c, e, prepared, err, i)
- }
- type DeleteCallbacks struct {
- Prepare func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error)
- Before func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
- After func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
- OnSuccessReturn func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure) error
- OnErrorReturn func(c *api.Context, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) error
- }
- func callbackPrepareDelete(callbacks *DeleteCallbacks, c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
- if callbacks == nil {
- return make(map[string]any), nil
- }
- if callbacks.Prepare == nil {
- return make(map[string]any), nil
- }
- return callbacks.Prepare(c, e, i)
- }
- func callbackBeforeDelete(callbacks *DeleteCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.Before == nil {
- return nil
- }
- return callbacks.Before(c, e, prepared, i, tx)
- }
- func callbackAfterDelete(callbacks *DeleteCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, dbExecutor database.Executor) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.After == nil {
- return nil
- }
- return callbacks.After(c, e, prepared, i, dbExecutor)
- }
- func callbackOnDeleteSuccessReturn(callbacks *DeleteCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.OnSuccessReturn == nil {
- return nil
- }
- return callbacks.OnSuccessReturn(c, e, prepared, i)
- }
- func callbackOnDeleteErrorReturn(callbacks *DeleteCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) error {
- if callbacks == nil {
- return err
- }
- if callbacks.OnErrorReturn == nil {
- return err
- }
- return callbacks.OnErrorReturn(c, e, prepared, err, i)
- }
- type UpdateCallbacks struct {
- Prepare func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error)
- Before func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
- After func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
- OnSuccessReturn func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure) error
- OnErrorReturn func(c *api.Context, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) error
- }
- func callbackPrepareUpdate(callbacks *UpdateCallbacks, c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
- if callbacks == nil {
- return make(map[string]any), nil
- }
- if callbacks.Prepare == nil {
- return make(map[string]any), nil
- }
- return callbacks.Prepare(c, e, i)
- }
- func callbackBeforeUpdate(callbacks *UpdateCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.Before == nil {
- return nil
- }
- return callbacks.Before(c, e, prepared, i, tx)
- }
- func callbackAfterUpdate(callbacks *UpdateCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, dbExecutor database.Executor) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.After == nil {
- return nil
- }
- return callbacks.After(c, e, prepared, i, dbExecutor)
- }
- func callbackOnUpdateSuccessReturn(callbacks *UpdateCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.OnSuccessReturn == nil {
- return nil
- }
- return callbacks.OnSuccessReturn(c, e, prepared, i)
- }
- func callbackOnUpdateErrorReturn(callbacks *UpdateCallbacks, c *api.Context, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) error {
- if callbacks == nil {
- return err
- }
- if callbacks.OnErrorReturn == nil {
- return err
- }
- return callbacks.OnErrorReturn(c, e, prepared, err, i)
- }
- type QueryCallbacks[O any] struct {
- Before func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) error
- After func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) error
- OnSuccessReturn func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[O]) (response.InfosData[O], error)
- OnErrorReturn func(c *api.Context, e entity.Entity, err error, i *infrastructure.Infrastructure) (response.InfosData[O], error)
- }
- func callbackBeforeQuery[O any](callbacks *QueryCallbacks[O], c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.Before == nil {
- return nil
- }
- return callbacks.Before(c, e, i)
- }
- func callbackAfterQuery[O any](callbacks *QueryCallbacks[O], c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.After == nil {
- return nil
- }
- return callbacks.After(c, e, i)
- }
- func callbackOnQuerySuccessReturn[O any](callbacks *QueryCallbacks[O], c *api.Context, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[O]) (response.InfosData[O], error) {
- if callbacks == nil {
- return output, nil
- }
- if callbacks.OnSuccessReturn == nil {
- return output, nil
- }
- return callbacks.OnSuccessReturn(c, e, i, output)
- }
- func callbackOnQueryErrorReturn[O any](callbacks *QueryCallbacks[O], c *api.Context, e entity.Entity, err error, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
- if callbacks == nil {
- return response.InfosData[O]{
- Infos: make([]O, 0),
- }, err
- }
- if callbacks.OnErrorReturn == nil {
- return response.InfosData[O]{
- Infos: make([]O, 0),
- }, err
- }
- return callbacks.OnErrorReturn(c, e, err, i)
- }
- type GetByIDCallbacks[O any] struct {
- Before func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) error
- After func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) error
- OnSuccessReturn func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure, output O) (O, error)
- OnErrorReturn func(c *api.Context, e entity.Entity, err error, i *infrastructure.Infrastructure) (O, error)
- }
- func callbackBeforeGetByID[O any](callbacks *GetByIDCallbacks[O], c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.Before == nil {
- return nil
- }
- return callbacks.Before(c, e, i)
- }
- func callbackAfterGetByID[O any](callbacks *GetByIDCallbacks[O], c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) error {
- if callbacks == nil {
- return nil
- }
- if callbacks.After == nil {
- return nil
- }
- return callbacks.After(c, e, i)
- }
- func callbackOnGetByIDSuccessReturn[O any](callbacks *GetByIDCallbacks[O], c *api.Context, e entity.Entity, i *infrastructure.Infrastructure, output O) (O, error) {
- if callbacks == nil {
- return output, nil
- }
- if callbacks.OnSuccessReturn == nil {
- return output, nil
- }
- return callbacks.OnSuccessReturn(c, e, i, output)
- }
- func callbackOnGetByIDErrorReturn[O any](callbacks *GetByIDCallbacks[O], c *api.Context, e entity.Entity, err error, i *infrastructure.Infrastructure) (O, error) {
- if callbacks == nil {
- return reflectutils.Zero[O](), err
- }
- if callbacks.OnErrorReturn == nil {
- return reflectutils.Zero[O](), err
- }
- return callbacks.OnErrorReturn(c, e, err, i)
- }
|