/
training.go
103 lines (83 loc) · 2.02 KB
/
training.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
94
95
96
97
98
99
100
101
102
103
package training
import (
"time"
commonerrors "github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/errors"
"github.com/pkg/errors"
)
type Training struct {
uuid string
userUUID string
userName string
time time.Time
notes string
proposedNewTime time.Time
moveProposedBy UserType
canceled bool
}
func NewTraining(uuid string, userUUID string, userName string, trainingTime time.Time) (*Training, error) {
if uuid == "" {
return nil, errors.New("empty training uuid")
}
if userUUID == "" {
return nil, errors.New("empty userUUID")
}
if userName == "" {
return nil, errors.New("empty userName")
}
if trainingTime.IsZero() {
return nil, errors.New("zero training time")
}
return &Training{
uuid: uuid,
userUUID: userUUID,
userName: userName,
time: trainingTime,
}, nil
}
// UnmarshalTrainingFromDatabase unmarshals Training from the database.
//
// It should be used only for unmarshalling from the database!
// You can't use UnmarshalTrainingFromDatabase as constructor - It may put domain into the invalid state!
func UnmarshalTrainingFromDatabase(
uuid string,
userUUID string,
userName string,
trainingTime time.Time,
notes string,
canceled bool,
proposedNewTime time.Time,
moveProposedBy UserType,
) (*Training, error) {
tr, err := NewTraining(uuid, userUUID, userName, trainingTime)
if err != nil {
return nil, err
}
tr.notes = notes
tr.proposedNewTime = proposedNewTime
tr.moveProposedBy = moveProposedBy
tr.canceled = canceled
return tr, nil
}
func (t Training) UUID() string {
return t.uuid
}
func (t Training) UserUUID() string {
return t.userUUID
}
func (t Training) UserName() string {
return t.userName
}
func (t Training) Time() time.Time {
return t.time
}
var ErrNoteTooLong = commonerrors.NewIncorrectInputError("Note too long", "note-too-long")
func (t *Training) UpdateNotes(notes string) error {
if len(notes) > 1000 {
return errors.WithStack(ErrNoteTooLong)
}
t.notes = notes
return nil
}
func (t Training) Notes() string {
return t.notes
}