/
apicode.go
74 lines (63 loc) · 1.96 KB
/
apicode.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
// Copyright (c) 2018-2022 The CYBAVO developers
// All Rights Reserved.
// NOTICE: All information contained herein is, and remains
// the property of CYBAVO and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to CYBAVO
// Dissemination of this information or reproduction of this materia
// is strictly forbidden unless prior written permission is obtained
// from CYBAVO.
package models
import (
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/orm"
)
type APICode struct {
APICodeID int64 `orm:"pk;auto;unique;column(api_code_id)" json:"api_code_id"`
APICode string `orm:"unique;column(api_code)" json:"api_code"`
ApiSecret string `orm:"unique;column(api_secret)" json:"api_secret"`
WalletID int64 `orm:"unique;column(wallet_id)" json:"wallet_id"`
}
func (m *APICode) TableName() string {
return "apicode"
}
func (m *APICode) TableEngine() string {
return "INNODB"
}
func (m *APICode) TableNameWithPrefix() string {
return GetMockDatabasePrefix() + m.TableName()
}
func SetAPICode(apiCodeObj *APICode) (err error) {
o := orm.NewOrm()
existedAPICodeObj, err := GetWalletAPICode(apiCodeObj.WalletID)
if err != nil {
apiCodeObj.APICodeID, err = o.Insert(apiCodeObj)
if err != nil {
logs.Error("Failed to insert API token =>", err)
return
}
} else {
apiCodeObj.APICodeID = existedAPICodeObj.APICodeID
_, err = o.Update(apiCodeObj, "api_secret", "api_code")
if err != nil {
logs.Warning("Failed to update API secret =>", err)
return
}
}
logs.Info("Succeeded to set API token =>", apiCodeObj)
return
}
func GetWalletAPICode(walletID int64) (apiCodeObj *APICode, err error) {
o := orm.NewOrm()
apiCodeObj = &APICode{}
err = o.QueryTable(apiCodeObj.TableNameWithPrefix()).
Filter("wallet_id", walletID).
One(apiCodeObj)
if err != nil {
// try read-only API code
err = o.QueryTable(apiCodeObj.TableNameWithPrefix()).
Filter("wallet_id", 0).
One(apiCodeObj)
}
return
}