-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_daos.go
52 lines (42 loc) · 1.19 KB
/
test_daos.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
49
50
51
52
package mgorepo
import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
type testDAO struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Sortable int `bson:"sortable,omitempty"`
Identifier string `bson:"identifier,omitempty"`
CreatedAt primitive.DateTime `bson:"createdAt,omitempty"`
UpdatedAt primitive.DateTime `bson:"updatedAt,omitempty"`
DeletedAt primitive.DateTime `bson:"deletedAt,omitempty"`
}
var _ DaoFiller[testModel] = (*testDAO)(nil)
func (d *testDAO) ToModel() testModel {
var id string
if !d.ID.IsZero() {
id = d.ID.Hex()
}
return testModel{
ID: id,
Identifier: d.Identifier,
Sortable: d.Sortable,
CreatedAt: d.CreatedAt.Time().UTC(),
UpdatedAt: d.UpdatedAt.Time().UTC(),
DeletedAt: d.DeletedAt.Time().UTC(),
}
}
func (d *testDAO) FromModel(m testModel) error {
if m.ID != "" {
id, err := primitive.ObjectIDFromHex(m.ID)
if err != nil {
return err
}
d.ID = id
}
d.Identifier = m.Identifier
d.Sortable = m.Sortable
d.CreatedAt = primitive.NewDateTimeFromTime(m.CreatedAt)
d.UpdatedAt = primitive.NewDateTimeFromTime(m.UpdatedAt)
d.DeletedAt = primitive.NewDateTimeFromTime(m.DeletedAt)
return nil
}