forked from facesea/banshee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
379 lines (364 loc) · 9.69 KB
/
project.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2015 Eleme Inc. All rights reserved.
package webapp
import (
"github.com/eleme/banshee/models"
"github.com/jinzhu/gorm"
"github.com/julienschmidt/httprouter"
"github.com/mattn/go-sqlite3"
"net/http"
"strconv"
)
// getProjects returns all projects.
func getProjects(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var projs []models.Project
if err := db.Admin.DB().Find(&projs).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
ResponseJSONOK(w, projs)
}
// getProject returns project by id.
func getProject(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Params
id, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
ResponseError(w, ErrProjectID)
return
}
// Query db.
proj := &models.Project{}
if err := db.Admin.DB().First(proj, id).Error; err != nil {
switch err {
case gorm.RecordNotFound:
ResponseError(w, ErrProjectNotFound)
return
default:
ResponseError(w, NewUnexceptedWebError(err))
return
}
}
ResponseJSONOK(w, proj)
}
// createProject request
type createProjectRequest struct {
Name string `json:"name"`
}
// createProject creates a project.
func createProject(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Request
req := &createProjectRequest{}
if err := RequestBind(r, req); err != nil {
ResponseError(w, ErrBadRequest)
return
}
// Validate
if err := models.ValidateProjectName(req.Name); err != nil {
ResponseError(w, NewValidationWebError(err))
return
}
// Save.
proj := &models.Project{Name: req.Name}
if err := db.Admin.DB().Create(proj).Error; err != nil {
sqliteErr, ok := err.(sqlite3.Error)
if ok {
switch sqliteErr.ExtendedCode {
case sqlite3.ErrConstraintNotNull:
ResponseError(w, ErrNotNull)
return
case sqlite3.ErrConstraintPrimaryKey:
ResponseError(w, ErrPrimaryKey)
return
case sqlite3.ErrConstraintUnique:
ResponseError(w, ErrDuplicateProjectName)
return
}
}
ResponseError(w, NewUnexceptedWebError(err))
return
}
ResponseJSONOK(w, proj)
}
// updateProject request
type updateProjectRequest struct {
Name string `json:"name"`
EnableSilent bool `json:"enableSilent"`
SilentTimeStart int `json:"silentTimeStart"`
SilentTimeEnd int `json:"silentTimeEnd"`
}
// updateProject updates a project.
func updateProject(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Params
id, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
ResponseError(w, ErrProjectID)
return
}
// Request
req := &updateProjectRequest{}
if err := RequestBind(r, req); err != nil {
ResponseError(w, ErrBadRequest)
return
}
// Validate
if err := models.ValidateProjectName(req.Name); err != nil {
ResponseError(w, NewValidationWebError(err))
return
}
if !req.EnableSilent && (req.SilentTimeStart != 0 || req.SilentTimeEnd != 0) {
// Validate if silent is disabled and start and end both are zero.
if err := models.ValidateProjectSilentRange(req.SilentTimeStart, req.SilentTimeEnd); err != nil {
ResponseError(w, NewValidationWebError(err))
return
}
}
// Find
proj := &models.Project{}
if err := db.Admin.DB().First(proj, id).Error; err != nil {
switch err {
case gorm.RecordNotFound:
ResponseError(w, ErrProjectNotFound)
return
default:
ResponseError(w, NewUnexceptedWebError(err))
return
}
}
// Patch.
proj.Name = req.Name
proj.EnableSilent = req.EnableSilent
proj.SilentTimeStart = req.SilentTimeStart
proj.SilentTimeEnd = req.SilentTimeEnd
if err := db.Admin.DB().Save(proj).Error; err != nil {
if err == gorm.RecordNotFound {
// Not found.
ResponseError(w, ErrProjectNotFound)
return
}
// Writer errors.
sqliteErr, ok := err.(sqlite3.Error)
if ok {
switch sqliteErr.ExtendedCode {
case sqlite3.ErrConstraintNotNull:
ResponseError(w, ErrNotNull)
return
case sqlite3.ErrConstraintUnique:
ResponseError(w, ErrDuplicateProjectName)
return
}
}
ResponseError(w, NewUnexceptedWebError(err))
return
}
ResponseJSONOK(w, proj)
}
// deleteProject deletes a project.
func deleteProject(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Params
id, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
ResponseError(w, ErrProjectID)
return
}
proj := &models.Project{ID: id}
// Delete Its Rules
var rules []models.Rule
if err := db.Admin.DB().Model(proj).Related(&rules).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
for i := 0; i < len(rules); i++ {
db.Admin.DB().Delete(&rules[i])
db.Admin.RulesCache.Delete(rules[i].ID)
}
// Delete Its user relationships.
var users []models.User
if err := db.Admin.DB().Model(proj).Association("Users").Find(&users).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
if err := db.Admin.DB().Model(proj).Association("Users").Delete(users).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
// Delete.
if err := db.Admin.DB().Delete(proj).Error; err != nil {
switch err {
case gorm.RecordNotFound:
ResponseError(w, ErrProjectNotFound)
return
default:
ResponseError(w, NewUnexceptedWebError(err))
return
}
}
}
// getProjectRules gets project rules.
func getProjectRules(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Params
id, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
ResponseError(w, ErrProjectID)
return
}
// Query
var rules []models.Rule
if err := db.Admin.DB().Model(&models.Project{ID: id}).Related(&rules).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
for i := 0; i < len(rules); i++ {
rules[i].SetNumMetrics(len(db.Index.Filter(rules[i].Pattern)))
}
ResponseJSONOK(w, rules)
}
// getProjectUsers gets project users.
func getProjectUsers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Params
id, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
ResponseError(w, ErrProjectID)
return
}
// Query
var users []models.User
if err := db.Admin.DB().Model(&models.Project{ID: id}).Association("Users").Find(&users).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
// Universals
var univs []models.User
if err := db.Admin.DB().Where("universal = ?", true).Find(&univs).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
users = append(users, univs...)
ResponseJSONOK(w, users)
}
// addProjectUserRequest is the request of addProjectUser
type addProjectUserRequest struct {
Name string `json:"name"`
}
// projectUser is the tempory select result for table `project_users`
type projectUser struct {
UserID int `sql:"user_id"`
ProjectID int `sql:"project_id"`
}
// addProjectUser adds a user to a project.
func addProjectUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Params
id, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
ResponseError(w, ErrProjectID)
return
}
// Request
req := &addProjectUserRequest{}
if err := RequestBind(r, req); err != nil {
ResponseError(w, ErrBadRequest)
return
}
// Find user.
user := &models.User{}
if err := db.Admin.DB().Where("name = ?", req.Name).First(user).Error; err != nil {
switch err {
case gorm.RecordNotFound:
ResponseError(w, ErrUserNotFound)
return
default:
ResponseError(w, NewUnexceptedWebError(err))
return
}
}
if user.Universal {
ResponseError(w, ErrProjectUniversalUser)
return
}
// Find proj
proj := &models.Project{}
if err := db.Admin.DB().First(proj, id).Error; err != nil {
if err == gorm.RecordNotFound {
ResponseError(w, ErrNotFound)
return
}
ResponseError(w, NewUnexceptedWebError(err))
return
}
// Note: Gorm only insert values to join-table if the primary key not in
// the join-table. So we select the record at first here.
if err := db.Admin.DB().Table("project_users").Where("user_id = ? and project_id = ?", user.ID, proj.ID).Find(&projectUser{}).Error; err == nil {
ResponseError(w, ErrDuplicateProjectUser)
return
}
// Append user.
if err := db.Admin.DB().Model(proj).Association("Users").Append(user).Error; err != nil {
if err == gorm.RecordNotFound {
// User or Project not found.
ResponseError(w, ErrNotFound)
return
}
// Duplicate primay key.
sqliteErr, ok := err.(sqlite3.Error)
if ok {
switch sqliteErr.ExtendedCode {
case sqlite3.ErrConstraintUnique:
ResponseError(w, ErrDuplicateProjectUser)
return
case sqlite3.ErrConstraintPrimaryKey:
ResponseError(w, ErrDuplicateProjectUser)
return
}
}
// Unexcepted error.
ResponseError(w, NewUnexceptedWebError(err))
return
}
}
// deleteProjectUser deletes a user from a project.
func deleteProjectUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Params
id, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
ResponseError(w, ErrProjectID)
return
}
userID, err := strconv.Atoi(ps.ByName("user_id"))
if err != nil {
ResponseError(w, ErrUserID)
return
}
// Find user.
user := &models.User{}
if err := db.Admin.DB().First(user, userID).Error; err != nil {
switch err {
case gorm.RecordNotFound:
ResponseError(w, ErrUserNotFound)
return
default:
ResponseError(w, NewUnexceptedWebError(err))
return
}
}
// Find proj.
proj := &models.Project{}
if err := db.Admin.DB().First(proj, id).Error; err != nil {
switch err {
case gorm.RecordNotFound:
ResponseError(w, ErrProjectNotFound)
return
default:
ResponseError(w, NewUnexceptedWebError(err))
return
}
}
// Delete user.
if err := db.Admin.DB().Model(proj).Association("Users").Delete(user).Error; err != nil {
switch err {
case gorm.RecordNotFound:
ResponseError(w, ErrNotFound)
return
default:
ResponseError(w, NewUnexceptedWebError(err))
return
}
}
}