-
Notifications
You must be signed in to change notification settings - Fork 4
/
algorithm.go
106 lines (100 loc) · 2.29 KB
/
algorithm.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
package handler
import (
"fmt"
"github.com/hprose/hprose-golang/rpc"
"github.com/marstau/smartcooly/constant"
"github.com/marstau/smartcooly/model"
)
type algorithm struct{}
// List ...
func (algorithm) List(size, page int64, order string, ctx rpc.Context) (resp response) {
username := ctx.GetString("username")
if username == "" {
resp.Message = constant.ErrAuthorizationError
return
}
self, err := model.GetUser(username)
if err != nil {
resp.Message = fmt.Sprint(err)
return
}
total, algorithms, err := self.ListAlgorithm(size, page, order)
if err != nil {
resp.Message = fmt.Sprint(err)
return
}
resp.Data = struct {
Total int64
List []model.Algorithm
}{
Total: total,
List: algorithms,
}
resp.Success = true
return
}
// Put
func (algorithm) Put(req model.Algorithm, ctx rpc.Context) (resp response) {
username := ctx.GetString("username")
if username == "" {
resp.Message = constant.ErrAuthorizationError
return
}
self, err := model.GetUser(username)
if err != nil {
resp.Message = fmt.Sprint(err)
return
}
algorithm := req
if req.ID > 0 {
if err := model.DB.First(&algorithm, req.ID).Error; err != nil {
resp.Message = fmt.Sprint(err)
return
}
algorithm.Name = req.Name
algorithm.Description = req.Description
algorithm.Script = req.Script
algorithm.EvnDefault = req.EvnDefault
if err := model.DB.Save(&algorithm).Error; err != nil {
resp.Message = fmt.Sprint(err)
return
}
resp.Success = true
return
}
req.UserID = self.ID
if err := model.DB.Create(&req).Error; err != nil {
resp.Message = fmt.Sprint(err)
return
}
resp.Success = true
return
}
// Delete
func (algorithm) Delete(ids []int64, ctx rpc.Context) (resp response) {
username := ctx.GetString("username")
if username == "" {
resp.Message = constant.ErrAuthorizationError
return
}
self, err := model.GetUser(username)
if err != nil {
resp.Message = fmt.Sprint(err)
return
}
userIds := []int64{}
_, users, err := self.ListUser(5, 1, "id")
if err != nil {
resp.Message = fmt.Sprint(err)
return
}
for _, u := range users {
userIds = append(userIds, u.ID)
}
if err := model.DB.Where("id in (?) AND user_id in (?)", ids, userIds).Delete(&model.Algorithm{}).Error; err != nil {
resp.Message = fmt.Sprint(err)
} else {
resp.Success = true
}
return
}