-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory_service.go
96 lines (77 loc) · 2.55 KB
/
category_service.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
package service
import (
"errors"
"project3/model/entity"
"project3/model/input"
"project3/repository"
)
type CategoryService interface {
CreateCategory(role_user string, input input.CategoryCreateInput) (entity.Category, error)
GetAllCategory() ([]entity.Category, error)
PatchCategory(role_user string, id_category int, input input.CategoryPatchInput) (entity.Category, error)
DeleteCategory(role_user string, id_category int) (entity.Category, error)
}
type categoryService struct {
categoryRepository repository.CategoryRepository
taskRepository repository.TaskRepository
}
func NewCategoryService(categoryRepository repository.CategoryRepository, taskRepository repository.TaskRepository) *categoryService {
return &categoryService{categoryRepository, taskRepository}
}
func (s *categoryService) CreateCategory(role_user string, input input.CategoryCreateInput) (entity.Category, error) {
if role_user != "admin" {
return entity.Category{}, errors.New("you are not admin")
}
category := entity.Category{
Type: input.Type,
}
categoryData, err := s.categoryRepository.CreateCategory(category)
if err != nil {
return entity.Category{}, err
}
return categoryData, nil
}
func (s *categoryService) GetAllCategory() ([]entity.Category, error) {
categories, err := s.categoryRepository.GetAll()
if err != nil {
return []entity.Category{}, err
}
return categories, nil
}
func (s *categoryService) PatchCategory(role_user string, id_category int, input input.CategoryPatchInput) (entity.Category, error) {
if role_user != "admin" {
return entity.Category{}, errors.New("you are not admin")
}
category := entity.Category{
Type: input.Type,
}
_, err := s.categoryRepository.PatchType(id_category, category)
if err != nil {
return entity.Category{}, err
}
categoryData, err := s.categoryRepository.GetCategoryById(id_category)
if err != nil {
return entity.Category{}, err
}
if categoryData.ID == 0 {
return entity.Category{}, errors.New("category not found")
}
return categoryData, nil
}
func (s *categoryService) DeleteCategory(role_user string, id_category int) (entity.Category, error) {
if role_user != "admin" {
return entity.Category{}, errors.New("you are not admin")
}
categoryData, err := s.categoryRepository.GetCategoryById(id_category)
if err != nil {
return entity.Category{}, err
}
if categoryData.ID == 0 {
return entity.Category{}, errors.New("category not found")
}
categoryData, err = s.categoryRepository.Delete(id_category)
if err != nil {
return entity.Category{}, err
}
return categoryData, nil
}