252 lines
5.7 KiB
Go
252 lines
5.7 KiB
Go
package rest
|
|
|
|
import (
|
|
"context"
|
|
"git.nobla.cn/golang/rest/types"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
beforeCreate = "beforeCreate"
|
|
afterCreate = "afterCreate"
|
|
beforeUpdate = "beforeUpdate"
|
|
afterUpdate = "afterUpdate"
|
|
beforeSave = "beforeSave"
|
|
afterSave = "afterSave"
|
|
beforeDelete = "beforeDelete"
|
|
afterDelete = "afterDelete"
|
|
afterExport = "afterExport"
|
|
afterImport = "afterImport"
|
|
)
|
|
|
|
type (
|
|
BeforeCreate func(ctx context.Context, tx *gorm.DB, model any) (err error)
|
|
AfterCreate func(ctx context.Context, tx *gorm.DB, model any, diff []*types.DiffAttr)
|
|
BeforeUpdate func(ctx context.Context, tx *gorm.DB, model any) (err error)
|
|
AfterUpdate func(ctx context.Context, tx *gorm.DB, model any, diff []*types.DiffAttr)
|
|
BeforeSave func(ctx context.Context, tx *gorm.DB, model any) (err error)
|
|
AfterSave func(ctx context.Context, tx *gorm.DB, model any, diff []*types.DiffAttr)
|
|
BeforeDelete func(ctx context.Context, tx *gorm.DB, model any) (err error)
|
|
AfterDelete func(ctx context.Context, tx *gorm.DB, model any)
|
|
AfterExport func(ctx context.Context, filename string) //导出回调
|
|
AfterImport func(ctx context.Context, result *types.ImportResult) //导入回调
|
|
hookManager struct {
|
|
callbacks map[string][]any
|
|
}
|
|
)
|
|
|
|
type (
|
|
ActiveQuery interface {
|
|
BeforeQuery(ctx context.Context, query *Query) (err error)
|
|
AfterQuery(ctx context.Context, query *Query) (err error)
|
|
}
|
|
)
|
|
|
|
func (hook *hookManager) register(spec string, cb any) {
|
|
if hook.callbacks == nil {
|
|
hook.callbacks = make(map[string][]any)
|
|
}
|
|
if _, ok := hook.callbacks[spec]; !ok {
|
|
hook.callbacks[spec] = make([]any, 0)
|
|
}
|
|
hook.callbacks[spec] = append(hook.callbacks[spec], cb)
|
|
}
|
|
|
|
func (hook *hookManager) beforeCreate(ctx context.Context, tx *gorm.DB, model any) (err error) {
|
|
callbacks, ok := hook.callbacks[beforeCreate]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(BeforeCreate); ok {
|
|
if err = cb(ctx, tx, model); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) afterCreate(ctx context.Context, tx *gorm.DB, model any, diff []*types.DiffAttr) {
|
|
callbacks, ok := hook.callbacks[afterCreate]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(AfterCreate); ok {
|
|
cb(ctx, tx, model, diff)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) beforeUpdate(ctx context.Context, tx *gorm.DB, model any) (err error) {
|
|
callbacks, ok := hook.callbacks[beforeUpdate]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(BeforeUpdate); ok {
|
|
if err = cb(ctx, tx, model); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) afterUpdate(ctx context.Context, tx *gorm.DB, model any, diff []*types.DiffAttr) {
|
|
callbacks, ok := hook.callbacks[afterUpdate]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(AfterUpdate); ok {
|
|
cb(ctx, tx, model, diff)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) beforeSave(ctx context.Context, tx *gorm.DB, model any) (err error) {
|
|
callbacks, ok := hook.callbacks[beforeSave]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(BeforeSave); ok {
|
|
if err = cb(ctx, tx, model); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) afterSave(ctx context.Context, tx *gorm.DB, model any, diff []*types.DiffAttr) {
|
|
callbacks, ok := hook.callbacks[afterSave]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(AfterSave); ok {
|
|
cb(ctx, tx, model, diff)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) beforeDelete(ctx context.Context, tx *gorm.DB, model any) (err error) {
|
|
callbacks, ok := hook.callbacks[beforeDelete]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(BeforeDelete); ok {
|
|
if err = cb(ctx, tx, model); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) afterDelete(ctx context.Context, tx *gorm.DB, model any) {
|
|
callbacks, ok := hook.callbacks[afterDelete]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(AfterDelete); ok {
|
|
cb(ctx, tx, model)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) afterExport(ctx context.Context, filename string) {
|
|
callbacks, ok := hook.callbacks[afterExport]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(AfterExport); ok {
|
|
cb(ctx, filename)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) afterImport(ctx context.Context, ret *types.ImportResult) {
|
|
callbacks, ok := hook.callbacks[afterImport]
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, callback := range callbacks {
|
|
if cb, ok := callback.(AfterImport); ok {
|
|
cb(ctx, ret)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (hook *hookManager) BeforeCreate(cb BeforeCreate) {
|
|
if cb != nil {
|
|
hook.register(beforeCreate, cb)
|
|
}
|
|
}
|
|
|
|
func (hook *hookManager) AfterCreate(cb AfterCreate) {
|
|
if cb != nil {
|
|
hook.register(afterCreate, cb)
|
|
}
|
|
}
|
|
|
|
func (hook *hookManager) BeforeUpdate(cb BeforeUpdate) {
|
|
if cb != nil {
|
|
hook.register(beforeUpdate, cb)
|
|
}
|
|
}
|
|
|
|
func (hook *hookManager) AfterUpdate(cb AfterUpdate) {
|
|
if cb != nil {
|
|
hook.register(afterUpdate, cb)
|
|
}
|
|
}
|
|
|
|
func (hook *hookManager) BeforeSave(cb BeforeSave) {
|
|
if cb != nil {
|
|
hook.register(beforeSave, cb)
|
|
}
|
|
}
|
|
|
|
func (hook *hookManager) AfterSave(cb AfterSave) {
|
|
if cb != nil {
|
|
hook.register(afterSave, cb)
|
|
}
|
|
}
|
|
|
|
func (hook *hookManager) BeforeDelete(cb BeforeDelete) {
|
|
if cb != nil {
|
|
hook.register(beforeDelete, cb)
|
|
}
|
|
}
|
|
|
|
func (hook *hookManager) AfterDelete(cb AfterDelete) {
|
|
if cb != nil {
|
|
hook.register(afterDelete, cb)
|
|
}
|
|
}
|
|
|
|
func (hook *hookManager) AfterExport(cb AfterExport) {
|
|
if cb != nil {
|
|
hook.register(afterExport, cb)
|
|
}
|
|
}
|
|
|
|
func (hook *hookManager) AfterImport(cb AfterImport) {
|
|
if cb != nil {
|
|
hook.register(afterImport, cb)
|
|
}
|
|
}
|