From 03c1e7671246ebffd3f622ebfae86376d8d1c05f Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 9 Sep 2024 18:39:01 +0300 Subject: [PATCH] Fix/bun handlers (#9) * BeforeInsert and BeforeUpdate hooks replaced with BeforeAppendModel hook * Rollback imports * Added BeforeAppendModelHook interface check --- internal/storage/token_metadata.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/internal/storage/token_metadata.go b/internal/storage/token_metadata.go index 82432e5..8a80cad 100644 --- a/internal/storage/token_metadata.go +++ b/internal/storage/token_metadata.go @@ -45,17 +45,18 @@ func (TokenMetadata) TableName() string { return "token_metadata" } -// BeforeInsert - -func (tm *TokenMetadata) BeforeInsert(ctx context.Context) (context.Context, error) { - tm.UpdatedAt = time.Now().Unix() - tm.CreatedAt = tm.UpdatedAt - tm.UpdateID = TokenUpdateID.Increment() - return ctx, nil -} - -// BeforeUpdate - -func (tm *TokenMetadata) BeforeUpdate(ctx context.Context) (context.Context, error) { - tm.UpdatedAt = time.Now().Unix() - tm.UpdateID = TokenUpdateID.Increment() - return ctx, nil +var _ bun.BeforeAppendModelHook = (*TokenMetadata)(nil) + +// BeforeAppendModel - +func (tm *TokenMetadata) BeforeAppendModel(ctx context.Context, query bun.Query) error { + switch query.(type) { + case *bun.InsertQuery: + tm.UpdatedAt = time.Now().Unix() + tm.CreatedAt = tm.UpdatedAt + tm.UpdateID = TokenUpdateID.Increment() + case *bun.UpdateQuery: + tm.UpdatedAt = time.Now().Unix() + tm.UpdateID = TokenUpdateID.Increment() + } + return nil }