Skip to content

Commit

Permalink
State: created at
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Feb 1, 2022
1 parent 873f900 commit c795d35
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
19 changes: 10 additions & 9 deletions database/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,23 @@ func (db *Gorm) Ping(ctx context.Context) error {
}

// State -
func (db *Gorm) State(indexName string) (s State, err error) {
err = db.conn.Where("index_name = ?", indexName).First(&s).Error
return
func (db *Gorm) State(indexName string) (*State, error) {
var s State
err := db.conn.Where("index_name = ?", indexName).First(s).Error
return &s, err
}

// CreateState -
func (db *Gorm) CreateState(s State) error {
return db.conn.Create(&s).Error
func (db *Gorm) CreateState(s *State) error {
return db.conn.Create(s).Error
}

// UpdateState -
func (db *Gorm) UpdateState(s State) error {
return db.conn.Save(&s).Error
func (db *Gorm) UpdateState(s *State) error {
return db.conn.Save(s).Error
}

// DeleteState -
func (db *Gorm) DeleteState(s State) error {
return db.conn.Delete(&s).Error
func (db *Gorm) DeleteState(s *State) error {
return db.conn.Delete(s).Error
}
19 changes: 10 additions & 9 deletions database/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,26 @@ func (db *PgGo) Ping(ctx context.Context) error {
}

// State -
func (db *PgGo) State(indexName string) (s State, err error) {
err = db.conn.Model(&s).Where("index_name = ?", indexName).Limit(1).Select()
return
func (db *PgGo) State(indexName string) (*State, error) {
var s State
err := db.conn.Model(&s).Where("index_name = ?", indexName).Limit(1).Select()
return &s, err
}

// CreateState -
func (db *PgGo) CreateState(s State) error {
_, err := db.conn.Model(&s).Insert()
func (db *PgGo) CreateState(s *State) error {
_, err := db.conn.Model(s).Insert()
return err
}

// UpdateState -
func (db *PgGo) UpdateState(s State) error {
_, err := db.conn.Model(&s).Where("index_name = ?", s.IndexName).Update()
func (db *PgGo) UpdateState(s *State) error {
_, err := db.conn.Model(s).Where("index_name = ?", s.IndexName).Update()
return err
}

// DeleteState -
func (db *PgGo) DeleteState(s State) error {
_, err := db.conn.Model(&s).Where("index_name = ?", s.IndexName).Delete()
func (db *PgGo) DeleteState(s *State) error {
_, err := db.conn.Model(s).Where("index_name = ?", s.IndexName).Delete()
return err
}
10 changes: 6 additions & 4 deletions database/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ type State struct {
Timestamp time.Time `json:"timestamp"`
Level uint64 `json:"level"`
UpdatedAt int `gorm:"autoUpdateTime"`
CreatedAt int `gorm:"autoCreateTime"`
}

// BeforeInsert -
func (s *State) BeforeInsert(ctx context.Context) (context.Context, error) {
s.UpdatedAt = int(time.Now().Unix())
s.CreatedAt = s.UpdatedAt
return ctx, nil
}

Expand All @@ -37,8 +39,8 @@ func (State) TableName() string {

// StateRepository -
type StateRepository interface {
State(name string) (State, error)
UpdateState(state State) error
CreateState(state State) error
DeleteState(state State) error
State(name string) (*State, error)
UpdateState(state *State) error
CreateState(state *State) error
DeleteState(state *State) error
}

0 comments on commit c795d35

Please sign in to comment.