-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
254 lines (204 loc) · 6.17 KB
/
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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package cms
import (
"github.com/jinzhu/gorm"
"github.com/Xzya/headless-cms/auth"
"github.com/dgrijalva/jwt-go"
"github.com/Xzya/headless-cms/config"
"time"
"github.com/Xzya/headless-cms/models"
)
var (
JWTTokenExpiryTime time.Duration = 7 * 24 * time.Hour
)
type Service interface {
// Account
CreateAccount(*models.Account) (string, error)
AuthenticateAccount(*models.Account) (string, error)
// Project
CreateProject(*models.Project) error
UpdateProject(*models.Project) error
GetProject(*models.Project) error
GetProjectList() ([]models.Project, error)
DeleteProject(*models.Project) error
// Role
CreateRole(*models.Role) error
UpdateRole(*models.Role) error
GetRole(*models.Role) error
GetRoleList(uint) ([]models.Role, error)
DeleteRole(*models.Role) error
// User
CreateUser(*models.User) error
UpdateUser(*models.User) error
GetUser(*models.User) error
GetUserList(uint) ([]models.User, error)
DeleteUser(*models.User) error
// Item Type
CreateItemType(*models.ItemType) error
UpdateItemType(*models.ItemType) error
GetItemTypeList(uint) ([]models.ItemType, error)
GetItemType(*models.ItemType) error
DuplicateItemType(*models.ItemType) error
DeleteItemType(*models.ItemType) error
// Field
CreateField(*models.Field) error
UpdateField(*models.Field) error
GetFieldList(itemTypeID uint, projectID uint) ([]models.Field, error)
GetProjectFieldList(projectID uint, filter models.FilterOptions) ([]models.Field, error)
GetField(*models.Field) error
DeleteField(*models.Field) error
// Item
CreateItem(*models.Item) error
UpdateItem(*models.Item) error
GetItemList(projectID uint, filter models.ItemFilterOptions) (int, []models.Item, error)
GetItem(*models.Item) error
DeleteItem(*models.Item) error
}
func NewService(db *gorm.DB, config config.Config) Service {
return &service{
Config: config,
AccountDS: models.AccountStore(db),
ProjectDS: models.ProjectStore(db),
RoleDS: models.RoleStore(db),
UserDS: models.UserStore(db),
ItemTypeDS: models.ItemTypeStore(db),
FieldDS: models.FieldStore(db),
ItemDS: models.ItemStore(db),
}
}
type service struct {
Config config.Config
models.AccountDS
models.ProjectDS
models.RoleDS
models.UserDS
models.ItemTypeDS
models.FieldDS
models.ItemDS
}
// Account
func (s *service) CreateAccount(acc *models.Account) (string, error) {
if err := s.AccountDS.Create(acc); err != nil {
return "", err
}
token, err := auth.CreateToken(jwt.MapClaims{
"sub": acc.ID,
"email": acc.Email,
"iat": time.Now().Unix(),
"exp": time.Now().Add(JWTTokenExpiryTime).Unix(),
}, s.Config.Secret)
return token, err
}
func (s *service) AuthenticateAccount(acc *models.Account) (string, error) {
if err := s.AccountDS.Authenticate(acc); err != nil {
return "", err
}
token, err := auth.CreateToken(jwt.MapClaims{
"sub": acc.ID,
"email": acc.Email,
"iat": time.Now().Unix(),
"exp": time.Now().Add(JWTTokenExpiryTime).Unix(),
}, s.Config.Secret)
return token, err
}
// Project
func (s *service) CreateProject(project *models.Project) error {
return s.ProjectDS.Create(project)
}
func (s *service) UpdateProject(project *models.Project) error {
return s.ProjectDS.Update(project)
}
func (s *service) GetProject(project *models.Project) error {
return s.ProjectDS.Get(project)
}
func (s *service) GetProjectList() ([]models.Project, error) {
return s.ProjectDS.GetList()
}
func (s *service) DeleteProject(project *models.Project) error {
return s.ProjectDS.Delete(project)
}
// Role
func (s *service) CreateRole(role *models.Role) error {
return s.RoleDS.Create(role)
}
func (s *service) UpdateRole(role *models.Role) error {
return s.RoleDS.Update(role)
}
func (s *service) GetRole(role *models.Role) error {
return s.RoleDS.Get(role)
}
func (s *service) GetRoleList(projectID uint) ([]models.Role, error) {
return s.RoleDS.GetList(projectID)
}
func (s *service) DeleteRole(role *models.Role) error {
return s.RoleDS.Delete(role)
}
// User
func (s *service) CreateUser(user *models.User) error {
return s.UserDS.Create(user)
}
func (s *service) UpdateUser(user *models.User) error {
return s.UserDS.Update(user)
}
func (s *service) GetUser(user *models.User) error {
return s.UserDS.Get(user)
}
func (s *service) GetUserList(projectID uint) ([]models.User, error) {
return s.UserDS.GetList(projectID)
}
func (s *service) DeleteUser(user *models.User) error {
return s.UserDS.Delete(user)
}
// Item Type
func (s *service) CreateItemType(item *models.ItemType) error {
return s.ItemTypeDS.Create(item)
}
func (s *service) UpdateItemType(item *models.ItemType) error {
return s.ItemTypeDS.Update(item)
}
func (s *service) GetItemTypeList(projectID uint) ([]models.ItemType, error) {
return s.ItemTypeDS.GetList(projectID)
}
func (s *service) GetItemType(item *models.ItemType) error {
return s.ItemTypeDS.Get(item)
}
func (s *service) DuplicateItemType(item *models.ItemType) error {
return s.ItemTypeDS.Duplicate(item)
}
func (s *service) DeleteItemType(item *models.ItemType) error {
return s.ItemTypeDS.Delete(item)
}
// Field
func (s *service) CreateField(item *models.Field) error {
return s.FieldDS.Create(item)
}
func (s *service) UpdateField(item *models.Field) error {
return s.FieldDS.Update(item)
}
func (s *service) GetFieldList(itemTypeID uint, projectID uint) ([]models.Field, error) {
return s.FieldDS.GetList(itemTypeID, projectID)
}
func (s *service) GetProjectFieldList(projectID uint, filter models.FilterOptions) ([]models.Field, error) {
return s.FieldDS.GetListForProject(projectID, filter)
}
func (s *service) GetField(item *models.Field) error {
return s.FieldDS.Get(item)
}
func (s *service) DeleteField(item *models.Field) error {
return s.FieldDS.Delete(item)
}
// Item
func (s *service) CreateItem(item *models.Item) error {
return s.ItemDS.Create(item)
}
func (s *service) UpdateItem(item *models.Item) error {
return s.ItemDS.Update(item)
}
func (s *service) GetItemList(projectID uint, filter models.ItemFilterOptions) (int, []models.Item, error) {
return s.ItemDS.GetList(projectID, filter)
}
func (s *service) GetItem(item *models.Item) error {
return s.ItemDS.Get(item)
}
func (s *service) DeleteItem(item *models.Item) error {
return s.ItemDS.Delete(item)
}