This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_category_api.go
125 lines (103 loc) · 3.88 KB
/
project_category_api.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
package server
import "net/http"
type ProjectCategoryAPI struct {
db *DB
}
func NewProjectCategoryAPI(db *DB) *ProjectCategoryAPI {
return &ProjectCategoryAPI{db}
}
func (projectCategoryAPI *ProjectCategoryAPI) GetTreeHandler(context *HandlerContext) (interface{}, *HandlerError) {
projectCategories, err := projectCategoryAPI.getTree(nil)
if err != nil {
return nil, &HandlerError{err, "couldn't retrieve project categories", http.StatusInternalServerError}
}
return projectCategories, nil
}
func (projectCategoryAPI *ProjectCategoryAPI) GetListHandler(context *HandlerContext) (interface{}, *HandlerError) {
projectCategories, err := projectCategoryAPI.getList(nil)
if err != nil {
return nil, &HandlerError{err, "couldn't retrieve project categories", http.StatusInternalServerError}
}
return projectCategories, nil
}
func (projectCategoryAPI *ProjectCategoryAPI) SaveHandler(context *HandlerContext) (interface{}, *HandlerError) {
var projectCategory ProjectCategory
err := context.GetReqBodyJSON(&projectCategory)
if err != nil {
return nil, &HandlerError{err, err.Error(), http.StatusBadRequest}
}
err = projectCategoryAPI.save(&projectCategory)
if err != nil {
if err == errIDNotUnique {
return nil, &HandlerError{err, "Error: Reference ID is already in use.", http.StatusBadRequest}
} else if err == errOptimisticLocking {
return nil, &HandlerError{err, "Error: Project category (or dependent data) was changed/deleted by another user.", http.StatusInternalServerError}
}
return nil, &HandlerError{err, "Error: Project category could not be saved.", http.StatusInternalServerError}
}
return projectCategory, nil
}
func (projectCategoryAPI *ProjectCategoryAPI) DeleteHandler(context *HandlerContext) (interface{}, *HandlerError) {
id, err := context.GetRouteVarInt("id")
if err != nil {
return nil, &HandlerError{err, err.Error(), http.StatusBadRequest}
}
err = projectCategoryAPI.delete(id)
if err != nil {
if err == errItemInUse {
return nil, &HandlerError{err, "Error: This project category, or one of its descendants, is in use.", http.StatusBadRequest}
}
return nil, &HandlerError{err, "Error: Project category could not deleted.", http.StatusInternalServerError}
}
return &SingleValue{true}, nil
}
func (projectCategoryAPI *ProjectCategoryAPI) getTree(parent *ProjectCategory) ([]ProjectCategory, error) {
projectCategories, err := projectCategoryAPI.db.GetProjectCategories(parent)
if err != nil {
return nil, err
}
for i := range projectCategories {
projectCategories[i].ProjectCategories, err = projectCategoryAPI.getTree(&projectCategories[i])
if err != nil {
return nil, err
}
}
return projectCategories, nil
}
func (projectCategoryAPI *ProjectCategoryAPI) getList(parent *ProjectCategory) ([]ProjectCategory, error) {
projectCategories, err := projectCategoryAPI.db.GetProjectCategories(parent)
if err != nil {
return nil, err
}
for i := 0; i < len(projectCategories); i++ {
children, err := projectCategoryAPI.getList(&projectCategories[i])
if err != nil {
return nil, err
}
// inserting children into slice after parent
slicingIndex := i + 1
projectCategories = append(projectCategories[:slicingIndex], append(children, projectCategories[slicingIndex:]...)...)
i += len(children)
}
return projectCategories, nil
}
func (projectCategoryAPI *ProjectCategoryAPI) save(projectCategory *ProjectCategory) error {
return projectCategoryAPI.db.SaveProjectCategory(projectCategory)
}
func (projectCategoryAPI *ProjectCategoryAPI) delete(id int) error {
projectCatetory, err := projectCategoryAPI.db.GetProjectCategory(id)
if err != nil {
return err
}
isReferenced, err := projectCategoryAPI.db.IsProjectCategoryReferenced(projectCatetory)
if err != nil {
return err
} else if isReferenced {
return errItemInUse
}
err = projectCategoryAPI.db.DeleteProjectCategory(projectCatetory)
if err != nil {
return err
}
return nil
}