move common code to pkg
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package pkg
|
||||
|
||||
var (
|
||||
SQLSTATE_25P02 = "25P02"
|
||||
SQLSTATE_23505 = "23505"
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
module pkg
|
||||
|
||||
go 1.25.2
|
||||
@@ -0,0 +1,36 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type (
|
||||
BaseHandler struct{}
|
||||
)
|
||||
|
||||
func (bh *BaseHandler) ToJSON(w http.ResponseWriter, statusCode int, data any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) ParamToInt(param string, defaultValue ...int) int {
|
||||
if param == "" {
|
||||
if len(defaultValue) > 0 {
|
||||
return defaultValue[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
if len(defaultValue) > 0 {
|
||||
return defaultValue[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user