@@ -35,8 +35,48 @@ func NewDeleter[T any](collection *mongo.Collection) *Deleter[T] {
3535}
3636
3737type Deleter [T any ] struct {
38- collection * mongo.Collection
39- filter any
38+ collection * mongo.Collection
39+ filter any
40+ beforeHooks []beforeHookFn
41+ afterHooks []afterHookFn
42+ }
43+
44+ func (d * Deleter [T ]) RegisterBeforeHooks (hooks ... beforeHookFn ) * Deleter [T ] {
45+ d .beforeHooks = append (d .beforeHooks , hooks ... )
46+ return d
47+ }
48+
49+ func (d * Deleter [T ]) RegisterAfterHooks (hooks ... afterHookFn ) * Deleter [T ] {
50+ d .afterHooks = append (d .afterHooks , hooks ... )
51+ return d
52+ }
53+
54+ func (d * Deleter [T ]) preActionHandler (ctx context.Context , globalOpContext * operation.OpContext , opContext * BeforeOpContext , opType operation.OpType ) error {
55+ err := callback .GetCallback ().Execute (ctx , globalOpContext , opType )
56+ if err != nil {
57+ return err
58+ }
59+ for _ , beforeHook := range d .beforeHooks {
60+ err = beforeHook (ctx , opContext )
61+ if err != nil {
62+ return err
63+ }
64+ }
65+ return nil
66+ }
67+
68+ func (d * Deleter [T ]) postActionHandler (ctx context.Context , globalOpContext * operation.OpContext , opContext * AfterOpContext , opType operation.OpType ) error {
69+ err := callback .GetCallback ().Execute (ctx , globalOpContext , opType )
70+ if err != nil {
71+ return err
72+ }
73+ for _ , afterHook := range d .afterHooks {
74+ err = afterHook (ctx , opContext )
75+ if err != nil {
76+ return err
77+ }
78+ }
79+ return nil
4080}
4181
4282// Filter is used to set the filter of the query
@@ -46,8 +86,8 @@ func (d *Deleter[T]) Filter(filter any) *Deleter[T] {
4686}
4787
4888func (d * Deleter [T ]) DeleteOne (ctx context.Context , opts ... * options.DeleteOptions ) (* mongo.DeleteResult , error ) {
49- opContext := operation .NewOpContext (d .collection , operation .WithFilter (d .filter ))
50- err := callback . GetCallback (). Execute ( ctx , opContext , operation .OpTypeBeforeDelete )
89+ globalPoContext := operation .NewOpContext (d .collection , operation .WithFilter (d .filter ))
90+ err := d . preActionHandler ( ctx , globalPoContext , NewBeforeOpContext ( d . collection , d . filter ) , operation .OpTypeBeforeDelete )
5191 if err != nil {
5292 return nil , err
5393 }
@@ -57,7 +97,7 @@ func (d *Deleter[T]) DeleteOne(ctx context.Context, opts ...*options.DeleteOptio
5797 return nil , err
5898 }
5999
60- err = callback . GetCallback (). Execute ( ctx , opContext , operation .OpTypeAfterDelete )
100+ err = d . postActionHandler ( ctx , globalPoContext , NewAfterOpContext ( d . collection , d . filter ) , operation .OpTypeAfterDelete )
61101 if err != nil {
62102 return nil , err
63103 }
@@ -66,8 +106,8 @@ func (d *Deleter[T]) DeleteOne(ctx context.Context, opts ...*options.DeleteOptio
66106}
67107
68108func (d * Deleter [T ]) DeleteMany (ctx context.Context , opts ... * options.DeleteOptions ) (* mongo.DeleteResult , error ) {
69- opContext := operation .NewOpContext (d .collection , operation .WithFilter (d .filter ))
70- err := callback . GetCallback (). Execute ( ctx , opContext , operation .OpTypeBeforeDelete )
109+ globalPoContext := operation .NewOpContext (d .collection , operation .WithFilter (d .filter ))
110+ err := d . preActionHandler ( ctx , globalPoContext , NewBeforeOpContext ( d . collection , d . filter ) , operation .OpTypeBeforeDelete )
71111 if err != nil {
72112 return nil , err
73113 }
@@ -77,7 +117,7 @@ func (d *Deleter[T]) DeleteMany(ctx context.Context, opts ...*options.DeleteOpti
77117 return nil , err
78118 }
79119
80- err = callback . GetCallback (). Execute ( ctx , opContext , operation .OpTypeAfterDelete )
120+ err = d . postActionHandler ( ctx , globalPoContext , NewAfterOpContext ( d . collection , d . filter ) , operation .OpTypeAfterDelete )
81121 if err != nil {
82122 return nil , err
83123 }
0 commit comments