-
Notifications
You must be signed in to change notification settings - Fork 0
/
CategoryService.go
141 lines (117 loc) · 4.25 KB
/
CategoryService.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package services
import (
"context"
"github.com/ysfada/product-management-system/domain/dtos"
"github.com/ysfada/product-management-system/domain/interfaces"
)
type CategoryService struct {
repository interfaces.ICategoryRepository
}
var _ interfaces.ICategoryService = (*CategoryService)(nil)
func NewCategoryService(repository interfaces.ICategoryRepository) *CategoryService {
return &CategoryService{
repository: repository,
}
}
func (s *CategoryService) Fetch(ctx context.Context, page int, size int, sortBy string, orderBy string) (*dtos.CategoryPaginatedDto, error) {
if categories, err := s.repository.Fetch(ctx, page, size, sortBy, orderBy); err != nil {
return nil, err
} else {
var categoriesDto dtos.CategoryPaginatedDto
for _, category := range categories.Categories {
categoryDto := &dtos.CategoryDto{
ID: category.ID,
Name: category.Name,
Description: category.Description,
}
categoriesDto.Categories = append(categoriesDto.Categories, categoryDto)
}
categoriesDto.TotalPage = categories.TotalPage
categoriesDto.CurrentPage = categories.CurrentPage
categoriesDto.NextPage = categories.NextPage
categoriesDto.PreviousPage = categories.PreviousPage
categoriesDto.Count = categories.Count
categoriesDto.Size = categories.Size
return &categoriesDto, nil
}
}
func (s *CategoryService) GetByID(ctx context.Context, id int) (res *dtos.CategoryDto, err error) {
if category, err := s.repository.GetByID(ctx, id); err != nil {
return nil, err
} else {
if category != nil {
return &dtos.CategoryDto{
ID: category.ID,
Name: category.Name,
Description: category.Description,
}, nil
}
return nil, nil
}
}
func (s *CategoryService) Update(ctx context.Context, dto *dtos.UpdateCategoryDto) error {
return s.repository.Update(ctx, dto)
}
func (s *CategoryService) Create(ctx context.Context, dto *dtos.CreateCategoryDto) error {
return s.repository.Create(ctx, dto)
}
func (s *CategoryService) Delete(ctx context.Context, id int) error {
return s.repository.Delete(ctx, id)
}
func (s *CategoryService) Search(ctx context.Context, q string, page int, size int, sortBy string, orderBy string) (*dtos.CategoryPaginatedDto, error) {
if categories, err := s.repository.Search(ctx, q, page, size, sortBy, orderBy); err != nil {
return nil, err
} else {
var categoriesDto dtos.CategoryPaginatedDto
for _, category := range categories.Categories {
categoryDto := &dtos.CategoryDto{
ID: category.ID,
Name: category.Name,
Description: category.Description,
}
categoriesDto.Categories = append(categoriesDto.Categories, categoryDto)
}
categoriesDto.TotalPage = categories.TotalPage
categoriesDto.CurrentPage = categories.CurrentPage
categoriesDto.NextPage = categories.NextPage
categoriesDto.PreviousPage = categories.PreviousPage
categoriesDto.Count = categories.Count
categoriesDto.Size = categories.Size
return &categoriesDto, nil
}
}
func (s *CategoryService) GetProducts(ctx context.Context, id int, page int, size int, sortBy string, orderBy string) (*dtos.CategoryProductsPaginatedDto, error) {
if products, err := s.repository.GetProducts(ctx, id, page, size, sortBy, orderBy); err != nil {
return nil, err
} else {
var productsDto dtos.CategoryProductsPaginatedDto
for _, product := range products.Products {
productDto := &dtos.ProductDto{
ID: product.ID,
Name: product.Name,
Description: product.Description,
CategoryID: product.CategoryID,
}
productsDto.Products = append(productsDto.Products, productDto)
for _, image := range product.Images {
imageDto := &dtos.ImageDto{
ID: image.ID,
Name: image.Name,
ImageUrl: image.ImageUrl,
ThumbnailUrl: image.ThumbnailUrl,
}
productDto.Images = append(productDto.Images, imageDto)
}
}
productsDto.ID = products.ID
productsDto.Name = products.Name
productsDto.Description = products.Description
productsDto.TotalPage = products.TotalPage
productsDto.CurrentPage = products.CurrentPage
productsDto.NextPage = products.NextPage
productsDto.PreviousPage = products.PreviousPage
productsDto.Count = products.Count
productsDto.Size = products.Size
return &productsDto, nil
}
}