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