43 lines
974 B
Go
43 lines
974 B
Go
package common
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
// MethodData is a struct containing the data passed over to an API method.
|
|
type MethodData struct {
|
|
User Token
|
|
DB *sqlx.DB
|
|
RequestData RequestData
|
|
C *gin.Context
|
|
}
|
|
|
|
// Err logs an error into gin.
|
|
func (md MethodData) Err(err error) {
|
|
md.C.Error(err)
|
|
}
|
|
|
|
// ID retrieves the Token's owner user ID.
|
|
func (md MethodData) ID() int {
|
|
return md.User.UserID
|
|
}
|
|
|
|
// Query is shorthand for md.C.Query.
|
|
func (md MethodData) Query(q string) string {
|
|
return md.C.Query(q)
|
|
}
|
|
|
|
// RequestData is the body of a request. It is wrapped into this type
|
|
// to implement the Unmarshal function, which is just a shorthand to
|
|
// json.Unmarshal.
|
|
type RequestData []byte
|
|
|
|
// Unmarshal json-decodes Requestdata into a value. Basically a
|
|
// shorthand to json.Unmarshal.
|
|
func (r RequestData) Unmarshal(into interface{}) error {
|
|
return json.Unmarshal([]byte(r), into)
|
|
}
|