/
repository.go
61 lines (52 loc) · 1.52 KB
/
repository.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
package apicall
import (
"fmt"
"github.com/go-xorm/xorm"
lib "github.com/beautiful-store/platform-service-library"
)
func (a *APICall) CheckTable(engine *xorm.Engine) bool {
exist, err := engine.IsTableExist(a.TableName())
if err != nil {
panic(err)
}
return exist
}
func (a *APICall) CheckAndMakeTable(engine *xorm.Engine) {
if exist := a.CheckTable(engine); !exist {
sql := a.sqlCreateTable()
if _, err := engine.Exec(sql); err != nil {
panic(err)
}
}
}
func (a *APICall) InsertTable(engine *xorm.Engine) error {
if a.Timestamp == "" {
a.Timestamp = lib.GetDefaultLogLocalDateTimeMilli()
}
if affected, err := engine.Insert(a); err != nil {
return err
} else if affected != 1 {
return fmt.Errorf(fmt.Sprintf("affected rows can't be %d", affected))
}
return nil
}
func (a *APICall) sqlCreateTable() string {
return fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s
(
id INT NOT NULL auto_increment,
env VARCHAR(20) NOT NULL,
module_name VARCHAR(60) NOT NULL,
timestamp VARCHAR(60) NOT NULL,
log_type VARCHAR(20) NOT NULL,
full_url VARCHAR(200) NOT NULL,
request JSON NULL,
response JSON NULL,
error_msg TEXT NULL,
donation_id INT NULL,
courier_id INT NULL,
org_id INT NULL,
member_id INT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);`, a.TableName())
}