|
| 1 | +package mooc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/CodelyTV/go-hexagonal_http_api-course/07-02-domain-events-subscriber/kit/event" |
| 9 | + "github.com/google/uuid" |
| 10 | +) |
| 11 | + |
| 12 | +var ErrInvalidCourseID = errors.New("invalid Course ID") |
| 13 | + |
| 14 | +// CourseID represents the course unique identifier. |
| 15 | +type CourseID struct { |
| 16 | + value string |
| 17 | +} |
| 18 | + |
| 19 | +// NewCourseID instantiate the VO for CourseID |
| 20 | +func NewCourseID(value string) (CourseID, error) { |
| 21 | + v, err := uuid.Parse(value) |
| 22 | + if err != nil { |
| 23 | + return CourseID{}, fmt.Errorf("%w: %s", ErrInvalidCourseID, value) |
| 24 | + } |
| 25 | + |
| 26 | + return CourseID{ |
| 27 | + value: v.String(), |
| 28 | + }, nil |
| 29 | +} |
| 30 | + |
| 31 | +// String type converts the CourseID into string. |
| 32 | +func (id CourseID) String() string { |
| 33 | + return id.value |
| 34 | +} |
| 35 | + |
| 36 | +var ErrEmptyCourseName = errors.New("the field Course Name can not be empty") |
| 37 | + |
| 38 | +// CourseName represents the course name. |
| 39 | +type CourseName struct { |
| 40 | + value string |
| 41 | +} |
| 42 | + |
| 43 | +// NewCourseName instantiate VO for CourseName |
| 44 | +func NewCourseName(value string) (CourseName, error) { |
| 45 | + if value == "" { |
| 46 | + return CourseName{}, ErrEmptyCourseName |
| 47 | + } |
| 48 | + |
| 49 | + return CourseName{ |
| 50 | + value: value, |
| 51 | + }, nil |
| 52 | +} |
| 53 | + |
| 54 | +// String type converts the CourseName into string. |
| 55 | +func (name CourseName) String() string { |
| 56 | + return name.value |
| 57 | +} |
| 58 | + |
| 59 | +var ErrEmptyDuration = errors.New("the field Duration can not be empty") |
| 60 | + |
| 61 | +// CourseDuration represents the course duration. |
| 62 | +type CourseDuration struct { |
| 63 | + value string |
| 64 | +} |
| 65 | + |
| 66 | +func NewCourseDuration(value string) (CourseDuration, error) { |
| 67 | + if value == "" { |
| 68 | + return CourseDuration{}, ErrEmptyDuration |
| 69 | + } |
| 70 | + |
| 71 | + return CourseDuration{ |
| 72 | + value: value, |
| 73 | + }, nil |
| 74 | +} |
| 75 | + |
| 76 | +// String type converts the CourseDuration into string. |
| 77 | +func (duration CourseDuration) String() string { |
| 78 | + return duration.value |
| 79 | +} |
| 80 | + |
| 81 | +// Course is the data structure that represents a course. |
| 82 | +type Course struct { |
| 83 | + id CourseID |
| 84 | + name CourseName |
| 85 | + duration CourseDuration |
| 86 | + |
| 87 | + events []event.Event |
| 88 | +} |
| 89 | + |
| 90 | +// CourseRepository defines the expected behaviour from a course storage. |
| 91 | +type CourseRepository interface { |
| 92 | + Save(ctx context.Context, course Course) error |
| 93 | +} |
| 94 | + |
| 95 | +//go:generate mockery --case=snake --outpkg=storagemocks --output=platform/storage/storagemocks --name=CourseRepository |
| 96 | + |
| 97 | +// NewCourse creates a new course. |
| 98 | +func NewCourse(id, name, duration string) (Course, error) { |
| 99 | + idVO, err := NewCourseID(id) |
| 100 | + if err != nil { |
| 101 | + return Course{}, err |
| 102 | + } |
| 103 | + |
| 104 | + nameVO, err := NewCourseName(name) |
| 105 | + if err != nil { |
| 106 | + return Course{}, err |
| 107 | + } |
| 108 | + |
| 109 | + durationVO, err := NewCourseDuration(duration) |
| 110 | + if err != nil { |
| 111 | + return Course{}, err |
| 112 | + } |
| 113 | + |
| 114 | + course := Course{ |
| 115 | + id: idVO, |
| 116 | + name: nameVO, |
| 117 | + duration: durationVO, |
| 118 | + } |
| 119 | + course.Record(NewCourseCreatedEvent(idVO.String(), nameVO.String(), durationVO.String())) |
| 120 | + return course, nil |
| 121 | +} |
| 122 | + |
| 123 | +// ID returns the course unique identifier. |
| 124 | +func (c Course) ID() CourseID { |
| 125 | + return c.id |
| 126 | +} |
| 127 | + |
| 128 | +// Name returns the course name. |
| 129 | +func (c Course) Name() CourseName { |
| 130 | + return c.name |
| 131 | +} |
| 132 | + |
| 133 | +// Duration returns the course duration. |
| 134 | +func (c Course) Duration() CourseDuration { |
| 135 | + return c.duration |
| 136 | +} |
| 137 | + |
| 138 | +// Record records a new domain event. |
| 139 | +func (c *Course) Record(evt event.Event) { |
| 140 | + c.events = append(c.events, evt) |
| 141 | +} |
| 142 | + |
| 143 | +// PullEvents returns all the recorded domain events. |
| 144 | +func (c Course) PullEvents() []event.Event { |
| 145 | + evt := c.events |
| 146 | + c.events = []event.Event{} |
| 147 | + |
| 148 | + return evt |
| 149 | +} |
0 commit comments