-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.go
More file actions
35 lines (29 loc) · 806 Bytes
/
base.go
File metadata and controls
35 lines (29 loc) · 806 Bytes
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
package mongox
import (
"time"
"go.mongodb.org/mongo-driver/v2/bson"
)
type Base struct {
ID bson.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty"`
UpdatedAt *time.Time `bson:"updatedAt,omitempty" json:"updatedAt,omitempty"`
DeletedAt *time.Time `bson:"deletedAt,omitempty" json:"deletedAt,omitempty"`
}
func (m *Base) DefaultId() bson.ObjectID {
if m.ID.IsZero() {
m.ID = bson.NewObjectID()
}
return m.ID
}
func (m *Base) DefaultCreatedAt() *time.Time {
if m.CreatedAt == nil || m.CreatedAt.IsZero() {
now := time.Now().Local()
m.CreatedAt = &now
}
return m.CreatedAt
}
func (m *Base) DefaultUpdatedAt() *time.Time {
now := time.Now().Local()
m.UpdatedAt = &now
return m.UpdatedAt
}