-
Notifications
You must be signed in to change notification settings - Fork 0
/
course_converter.go
81 lines (69 loc) · 1.96 KB
/
course_converter.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
package converter
import (
"github.com/best-project/api/internal"
"github.com/pkg/errors"
"strconv"
)
type CourseConverter struct {
TaskConverter *TaskConverter
}
func NewCourseConverter(taskConverter *TaskConverter) *CourseConverter {
return &CourseConverter{
TaskConverter: taskConverter,
}
}
func (c *CourseConverter) ToModel(dto *internal.CourseDTO) *internal.Course {
conv := &internal.Course{
Name: dto.Name,
Description: dto.Description,
DifficultyLevel: dto.DifficultyLevel,
Image: dto.Image,
Language: dto.Language,
MaxPoints: dto.MaxPoints,
Rate: dto.Rate,
Type: dto.Type,
UserID: dto.UserID,
Task: c.TaskConverter.ManyToModel(dto.Data),
}
if dto.CourseID != "" {
id, _ := strconv.Atoi(dto.CourseID)
conv.ID = uint(id)
}
return conv
}
func (c *CourseConverter) ManyToModel(dtos []internal.CourseDTO) []internal.Course {
result := make([]internal.Course, len(dtos))
for _, dto := range dtos {
result = append(result, *c.ToModel(&dto))
}
return result
}
func (c *CourseConverter) ToDTO(dto *internal.Course) (*internal.CourseDTO, error) {
return &internal.CourseDTO{
CourseID: strconv.Itoa(int(dto.ID)),
Type: dto.Type,
Name: dto.Name,
Rate: dto.Rate,
Image: dto.Image,
Language: dto.Language,
MaxPoints: dto.MaxPoints,
DifficultyLevel: dto.DifficultyLevel,
Description: dto.Description,
Data: c.TaskConverter.ManyToDTO(dto.Task),
UserID: dto.UserID,
}, nil
}
func (c *CourseConverter) ManyToDTO(courses []*internal.Course) ([]internal.CourseDTO, error) {
result := make([]internal.CourseDTO, 0)
for _, course := range courses {
if course.UserID == 0 {
continue
}
dto, err := c.ToDTO(course)
if err != nil {
return nil, errors.Wrapf(err, "while converting course %d", course.ID)
}
result = append(result, *dto)
}
return result, nil
}