2016-04-03 17:59:27 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2016-04-12 19:23:02 +00:00
|
|
|
"encoding/json"
|
2016-04-03 17:59:27 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2016-08-15 11:37:03 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2016-04-03 17:59:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MethodData is a struct containing the data passed over to an API method.
|
|
|
|
type MethodData struct {
|
|
|
|
User Token
|
2016-08-15 11:37:03 +00:00
|
|
|
DB *sqlx.DB
|
2016-04-12 19:23:02 +00:00
|
|
|
RequestData RequestData
|
2016-04-03 17:59:27 +00:00
|
|
|
C *gin.Context
|
|
|
|
}
|
2016-04-08 17:05:54 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2016-04-12 19:23:02 +00:00
|
|
|
|
2016-08-15 17:59:46 +00:00
|
|
|
// Query is shorthand for md.C.Query.
|
|
|
|
func (md MethodData) Query(q string) string {
|
|
|
|
return md.C.Query(q)
|
|
|
|
}
|
|
|
|
|
2016-08-27 10:04:12 +00:00
|
|
|
// HasQuery returns true if the parameter is encountered in the querystring.
|
|
|
|
// It returns true even if the parameter is "" (the case of ?param&etc=etc)
|
|
|
|
func (md MethodData) HasQuery(q string) bool {
|
|
|
|
_, has := md.C.GetQuery(q)
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2016-04-12 19:23:02 +00:00
|
|
|
// 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)
|
|
|
|
}
|