This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.go
225 lines (180 loc) · 5.28 KB
/
Util.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
package main
import (
"github.com/labstack/echo"
"database/sql"
"time"
"strconv"
"fmt"
"github.com/fatih/color"
"reflect"
)
func Timestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func ParamToInt64(c echo.Context, param string) (int64, bool) {
if str := c.Param(param); str != "" {
p, err := strconv.ParseInt(str, 10, 64)
if err == nil {
return p, true
}
}
return 0, false
}
func PrintError(str string) {
color.Red(str)
}
func PrintDebug(str string) {
if debugging {
color.Yellow(str)
}
}
func PrintSuccess(str string) {
if debugging {
color.Green(str)
}
}
func ValidateRequest(request *APIRequest, required []string, disallowed []string) (bool, []string) {
var result []string
if required != nil {
for _, r := range required {
if _, ok := request.ApiArguments[r]; !ok {
result = append(result, "missing " + r)
}
}
}
if disallowed != nil {
for _, r := range disallowed {
if _, ok := request.ApiArguments[r]; ok {
result = append(result, r + " not allowed")
}
}
}
if len(result) > 0 {
PrintError(fmt.Sprintf("Missing required api arguments. %+v", result))
}
return len(result) == 0, result
}
func Insert(qs string, args map[string]interface{}) (sql.Result, error) {
return pool.NamedExec(qs, args)
}
func SelectOne(dest interface{}, qs string, args map[string]interface{}) error {
nstmt, err := pool.PrepareNamed(qs)
if err != nil {
return err
}
return nstmt.Get(dest, args)
}
func SelectAll(dest interface{}, qs string, args map[string]interface{}) error {
nstmt, err := pool.PrepareNamed(qs)
if err != nil {
return err
}
return nstmt.Select(dest, args)
}
func Delete(qs string, args map[string]interface{}) (sql.Result, error) {
return pool.NamedExec(qs, args)
}
func Update(qs string, args map[string]interface{}) (sql.Result, error) {
return pool.NamedExec(qs, args)
}
func APIFail(reason interface{}) *APIResponse {
return &APIResponse{ Success: false, Result: reason }
}
func APISuccess(reason interface{}) *APIResponse {
return &APIResponse{ Success: true, Result: reason }
}
func ValidateAndSelectAll(request *APIRequest, dest interface{}, qs string, required []string, disallowed ...string) *APIResponse {
if ok, err := ValidateRequest(request, required, disallowed); !ok {
return APIFail(err)
}
if err := SelectAll(dest, qs, request.ApiArguments); err != nil {
PrintError(fmt.Sprintf("Error trying to select all. %+v", err))
return APIFail(SelectFail)
}
return APISuccess(dest)
}
func ValidateAndSelectOne(request *APIRequest, dest interface{}, qs string, required []string, disallowed ...string) *APIResponse {
if ok, err := ValidateRequest(request, required, disallowed); !ok {
return APIFail(err)
}
if err := SelectOne(dest, qs, request.ApiArguments); err != nil {
PrintError(fmt.Sprintf("Error trying to select one. %+v", err))
return APIFail(SelectFail)
}
return APISuccess(dest)
}
func ValidateAndDelete(request *APIRequest, qs string, required []string, disallowed ...string) *APIResponse {
if ok, err := ValidateRequest(request, required, disallowed); !ok {
return APIFail(err)
}
result, err := Delete(qs, request.ApiArguments)
if err != nil {
PrintError(fmt.Sprintf("Error trying to delete. %+v", err))
return APIFail(DeleteFail)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return APIFail(NoRowsAffected)
}
return APISuccess(rowsAffected)
}
func ValidateAndInsert(request *APIRequest, qs string, required []string, disallowed ...string) *APIResponse {
if ok, err := ValidateRequest(request, required, disallowed); !ok {
return APIFail(err)
}
result, err := Insert(qs, request.ApiArguments)
if err != nil {
PrintError(fmt.Sprintf("Error trying to insert. %+v", err))
return APIFail(InsertFail)
}
lastInsertId, err := result.LastInsertId()
if err != nil {
return APIFail(NoInsertID)
}
return APISuccess(lastInsertId)
}
func ValidateAndUpdateFromStruct(request *APIRequest, dest interface{}, table string, qs string, qs2 string, required []string, disallowed ...string) *APIResponse {
if ok, err := ValidateRequest(request, required, disallowed); !ok {
return APIFail(err)
}
keys := ``
elem := reflect.ValueOf(dest).Elem()
for i := 0; i < elem.NumField(); i++ {
field := elem.Type().Field(i)
if tag, ok := field.Tag.Lookup("static"); ok && tag == "true" {
continue
}
key := field.Name
if _, ok := request.ApiArguments[key]; ok {
if len(keys) != 0 {
keys += `,`
}
keys += ` ` + table + `.` + key + ` = :` + key
}
}
result, err := Update(qs + keys + qs2, request.ApiArguments)
if err != nil {
PrintError(fmt.Sprintf("Error trying to update from struct. %+v", err))
return APIFail(UpdateFail)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return APIFail(NoRowsAffected)
}
return APISuccess(rowsAffected)
}
func ValidateAndUpdate(request *APIRequest, qs string, required []string, disallowed ...string) *APIResponse {
if ok, err := ValidateRequest(request, required, disallowed); !ok {
return APIFail(err)
}
result, err := Update(qs, request.ApiArguments)
if err != nil {
PrintError(fmt.Sprintf("Error trying to update. %+v", err))
return APIFail(UpdateFail)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return APIFail(NoRowsAffected)
}
return APISuccess(rowsAffected)
}