/
base.go
48 lines (38 loc) · 1.11 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package entity
import (
"github.com/google/uuid"
"gorm.io/gorm"
"time"
)
type BaseModel struct {
Id string `gorm:"primary_key;column:id;" json:"id"`
CreatedAt time.Time `gorm:"column:created_at" json:"createdAt"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"-"`
CreatedBy string `gorm:"column:created_by" json:"createdBy"`
UpdatedBy string `gorm:"column:updated_by" json:"updatedBy"`
DeletedBy string `gorm:"column:deleted_by" json:"deletedBy"`
}
func (bm *BaseModel) BeforeCreate(tx *gorm.DB) (err error) {
bm.Id = uuid.NewString()
bm.CreatedAt = time.Now()
bm.UpdatedAt = time.Now()
actor := GetEntityActor(tx)
bm.CreatedBy = actor
bm.UpdatedBy = actor
return
}
func (bm *BaseModel) BeforeUpdate(tx *gorm.DB) (err error) {
bm.UpdatedAt = time.Now()
bm.UpdatedBy = GetEntityActor(tx)
return
}
func GetEntityActor(tx *gorm.DB) string {
ctx := tx.Statement.Context
userId := "SYSTEM"
value := ctx.Value("actor")
if value != nil {
userId = value.(string)
}
return userId
}