/
model.go
64 lines (53 loc) · 1.27 KB
/
model.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
53
54
55
56
57
58
59
60
61
62
63
64
package mongo
import (
"time"
"github.com/rl404/shimakaze/internal/domain/agency/entity"
"go.mongodb.org/mongo-driver/bson"
)
type agency struct {
ID int64 `bson:"id"`
Name string `bson:"name"`
Image string `bson:"image"`
Member int `bson:"member"`
Subscriber int `bson:"subscriber"`
CreatedAt time.Time `bson:"created_at"`
UpdatedAt time.Time `bson:"updated_at"`
}
// MarshalBSON to override marshal function.
func (a *agency) MarshalBSON() ([]byte, error) {
if a.CreatedAt.IsZero() {
a.CreatedAt = time.Now()
}
a.UpdatedAt = time.Now()
type a2 agency
return bson.Marshal((*a2)(a))
}
func (a *agency) toEntity() *entity.Agency {
return &entity.Agency{
ID: a.ID,
Name: a.Name,
Image: a.Image,
Member: a.Member,
Subscriber: a.Subscriber,
UpdatedAt: a.UpdatedAt,
}
}
func (m *Mongo) agencyFromEntity(a entity.Agency) *agency {
return &agency{
ID: a.ID,
Name: a.Name,
Image: a.Image,
Member: a.Member,
Subscriber: a.Subscriber,
UpdatedAt: a.UpdatedAt,
}
}
func (m *Mongo) convertSort(sort string) bson.D {
if sort == "" {
sort = "name"
}
if sort[0] == '-' {
return bson.D{{Key: sort[1:], Value: -1}}
}
return bson.D{{Key: sort, Value: 1}}
}