-
Notifications
You must be signed in to change notification settings - Fork 42
/
getIDHandler.go
243 lines (221 loc) · 7.25 KB
/
getIDHandler.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
package server
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/hoshinonyaruko/gensokyo/idmap"
"github.com/hoshinonyaruko/gensokyo/mylog"
)
func GetIDHandler(c *gin.Context) {
idOrRow := c.Query("id")
typeVal, err := strconv.Atoi(c.Query("type"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid type"})
return
}
switch typeVal {
case 1:
newRow, err := idmap.StoreIDv2(idOrRow)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"row": newRow})
case 2:
id, err := idmap.RetrieveRowByIDv2(idOrRow)
if err == idmap.ErrKeyNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "ID not found"})
return
} else if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"id": id})
case 3:
// 存储
section := c.Query("id")
subtype := c.Query("subtype")
value := c.Query("value")
err := idmap.WriteConfigv2(section, subtype, value)
if err != nil {
mylog.Printf(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success"})
case 4:
// 获取值
section := c.Query("id")
subtype := c.Query("subtype")
value, err := idmap.ReadConfigv2(section, subtype)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"value": value})
case 5:
oldRowValue, err := strconv.ParseInt(c.Query("oldRowValue"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid oldRowValue"})
return
}
newRowValue, err := strconv.ParseInt(c.Query("newRowValue"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid newRowValue"})
return
}
err = idmap.UpdateVirtualValuev2(oldRowValue, newRowValue)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success"})
case 6:
virtualValue, err := strconv.ParseInt(c.Query("virtualValue"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid virtualValue"})
return
}
virtual, real, err := idmap.RetrieveRealValuev2(virtualValue)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"virtual": virtual, "real": real})
case 7:
realValue := c.Query("id")
if realValue == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
_, virtualValue, err := idmap.RetrieveVirtualValuev2(realValue)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"virtual": virtualValue})
case 8:
// 调用新的 StoreIDv2Pro
subid := c.Query("subid")
if subid == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "subid parameter is required for type 8"})
return
}
newRow, newSubRow, err := idmap.StoreIDv2Pro(idOrRow, subid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"row": newRow, "subRow": newSubRow})
case 9:
// 调用新的 RetrieveRowByIDv2Pro
subid := c.Query("subid")
if subid == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "subid parameter is required for type 9"})
return
}
id, subid, err := idmap.RetrieveRowByIDv2Pro(idOrRow, subid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"id": id, "subid": subid})
case 10:
subid := c.Query("subid")
if idOrRow == "" || subid == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "id and subid parameters are required for type 10"})
return
}
firstValue, secondValue, err := idmap.RetrieveVirtualValuev2Pro(idOrRow, subid) // 确保函数名称正确
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"firstValue": firstValue, "secondValue": secondValue})
case 11:
subid := c.Query("subid")
if idOrRow == "" || subid == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "id and subid parameters are required for type 11"})
return
}
var virtualValue, virtualValueSub int64
virtualValue, err = strconv.ParseInt(idOrRow, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input values idOrRow"})
return
}
virtualValueSub, err = strconv.ParseInt(subid, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input values subid"})
return
}
firstRealValue, secondRealValue, err := idmap.RetrieveRealValuesv2Pro(virtualValue, virtualValueSub)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"firstRealValue": firstRealValue, "secondRealValue": secondRealValue})
case 12:
oldVirtualValue1Str := c.Query("oldVirtualValue1")
newVirtualValue1Str := c.Query("newVirtualValue1")
oldVirtualValue2Str := c.Query("oldVirtualValue2")
newVirtualValue2Str := c.Query("newVirtualValue2")
var oldVirtualValue1, newVirtualValue1, oldVirtualValue2, newVirtualValue2 int64
// 将字符串转换为int64
oldVirtualValue1, err = strconv.ParseInt(oldVirtualValue1Str, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input values oldVirtualValue1"})
return
}
newVirtualValue1, err = strconv.ParseInt(newVirtualValue1Str, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input values newVirtualValue1"})
return
}
oldVirtualValue2, err = strconv.ParseInt(oldVirtualValue2Str, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input values oldVirtualValue2"})
return
}
newVirtualValue2, err = strconv.ParseInt(newVirtualValue2Str, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input values newVirtualValue2"})
return
}
err = idmap.UpdateVirtualValuev2Pro(oldVirtualValue1, newVirtualValue1, oldVirtualValue2, newVirtualValue2)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Virtual values updated successfully"})
case 13:
newRow, err := idmap.SimplifiedStoreIDv2(idOrRow)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"row": newRow})
case 14:
id := c.Query("id")
keys, err := idmap.FindSubKeysByIdPro(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"keys": keys})
case 15:
// 删除
// 从请求中获取参数
section := c.Query("id")
subtype := c.Query("subtype")
// 调用DeleteConfigv2来删除配置
err := idmap.DeleteConfigv2(section, subtype)
if err != nil {
// 如果有错误,记录并返回错误信息
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// 如果删除成功,返回成功响应
c.JSON(http.StatusOK, gin.H{"status": "success"})
}
}