-
Notifications
You must be signed in to change notification settings - Fork 4
/
exchange.go
113 lines (106 loc) · 2.41 KB
/
exchange.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
package handler
import (
"fmt"
"github.com/hprose/hprose-golang/rpc"
"github.com/marstau/smartcooly/constant"
"github.com/marstau/smartcooly/model"
)
type exchange struct{}
// Types ...
func (exchange) Types(_ string, ctx rpc.Context) (resp response) {
resp.Data = constant.ExchangeTypes
resp.Success = true
return
}
// List ...
func (exchange) 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, exchanges, err := self.ListExchange(size, page, order)
if err != nil {
resp.Message = fmt.Sprint(err)
return
}
resp.Data = struct {
Total int64
List []model.Exchange
}{
Total: total,
List: exchanges,
}
resp.Success = true
return
}
// Put
func (exchange) Put(req model.Exchange, 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
}
exchange := req
if req.ID > 0 {
if err := model.DB.First(&exchange, req.ID).Error; err != nil {
resp.Message = fmt.Sprint(err)
return
}
exchange.Name = req.Name
exchange.Type = req.Type
exchange.AccessKey = req.AccessKey
exchange.SecretKey = req.SecretKey
if err := model.DB.Save(&exchange).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 (exchange) 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.Exchange{}).Error; err != nil {
resp.Message = fmt.Sprint(err)
} else {
resp.Success = true
}
return
}