From 77018e28490554504a0c43ebc3ee6e883c498168 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 19 Jul 2024 17:11:06 +0200 Subject: [PATCH] Fix: update state --- database/db_test.go | 2 +- database/state.go | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/database/db_test.go b/database/db_test.go index b3b7c51..001a84f 100644 --- a/database/db_test.go +++ b/database/db_test.go @@ -18,7 +18,7 @@ const ( testIndex = "test_index" ) -func newDatabase(ctx context.Context, typ string, cfg config.Database) (Database, error) { +func newDatabase(_ context.Context, typ string, _ config.Database) (Database, error) { switch typ { case "bun": return NewBun(), nil diff --git a/database/state.go b/database/state.go index bbe17ad..0d370ca 100644 --- a/database/state.go +++ b/database/state.go @@ -22,17 +22,18 @@ type State struct { CreatedAt int `gorm:"autoCreateTime" comment:"Created timestamp"` } -// BeforeInsert - -func (s *State) BeforeInsert(ctx context.Context) (context.Context, error) { - s.UpdatedAt = int(time.Now().Unix()) - s.CreatedAt = s.UpdatedAt - return ctx, nil -} - -// BeforeUpdate - -func (s *State) BeforeUpdate(ctx context.Context) (context.Context, error) { - s.UpdatedAt = int(time.Now().Unix()) - return ctx, nil +var _ bun.BeforeAppendModelHook = (*State)(nil) + +func (s *State) BeforeAppendModel(ctx context.Context, query bun.Query) error { + switch query.(type) { + case *bun.InsertQuery: + s.UpdatedAt = int(time.Now().Unix()) + s.CreatedAt = s.UpdatedAt + + case *bun.UpdateQuery: + s.UpdatedAt = int(time.Now().Unix()) + } + return nil } // TableName -