-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.go
93 lines (73 loc) · 2.26 KB
/
subscriber.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package entity
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)
type Frequency string
const (
Day Frequency = "day"
Week Frequency = "week"
Hour Frequency = "hour"
)
type Notify struct {
Hour int `json:"hour" bson:"hour"`
Minute int `json:"minute" bson:"minute"`
Weekday time.Weekday `json:"weekday" bson:"weekday"`
Frequency Frequency `json:"frequency" bson:"frequency"`
}
type Subscriber struct {
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
RepoID primitive.ObjectID `json:"repoID" bson:"repoID"`
Email string `json:"email" bson:"email"`
IsConfirmed bool `json:"isConfirmed" bson:"isConfirmed"`
Notify *Notify `json:"notify,omitempty" bson:"notify,omitempty"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
}
type SubscriberDTO struct {
ID *string `json:"id,omitempty"`
RepoID string `json:"repoID"`
Email string `json:"email"`
IsConfirmed bool `json:"isConfirmed"`
Notify *Notify `json:"notify,omitempty"`
}
type SubscriberIDRequest struct {
ID string `json:"id"`
}
type SubscriberRequest struct {
RepoID *string `json:"repoID,omitempty"`
Email *string `json:"email,omitempty"`
Notify *Notify `json:"notify,omitempty"`
}
func ToSubscriberDTO(subscriber *Subscriber) *SubscriberDTO {
id := subscriber.ID.Hex()
return &SubscriberDTO{
ID: &id,
RepoID: subscriber.RepoID.Hex(),
Email: subscriber.Email,
IsConfirmed: subscriber.IsConfirmed,
Notify: subscriber.Notify,
}
}
func ToSubscriberDTOs(subscribers []*Subscriber) []*SubscriberDTO {
subDTOs := make([]*SubscriberDTO, len(subscribers))
for i, item := range subscribers {
subDTOs[i] = ToSubscriberDTO(item)
}
return subDTOs
}
func ToSubscriber(subscribeDTO *SubscriberDTO) *Subscriber {
repoID, _ := primitive.ObjectIDFromHex(subscribeDTO.RepoID)
subscriber := &Subscriber{
RepoID: repoID,
Email: subscribeDTO.Email,
IsConfirmed: subscribeDTO.IsConfirmed,
Notify: subscribeDTO.Notify,
}
if subscribeDTO.ID != nil {
id, _ := primitive.ObjectIDFromHex(*subscribeDTO.ID)
subscriber.ID = id
} else {
subscriber.ID = primitive.NilObjectID
}
return subscriber
}