context int shorthands

This commit is contained in:
Nazar Kanaev
2021-03-16 23:44:29 +00:00
parent 76937bedc9
commit ff3241bd57
2 changed files with 24 additions and 12 deletions

View File

@@ -2,8 +2,10 @@ package router
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
)
type Context struct {
@@ -31,3 +33,15 @@ func (c *Context) JSON(status int, data interface{}) {
c.Out.Write(body)
c.Out.Write([]byte("\n"))
}
func (c *Context) VarInt64(key string) (int64, error) {
if val, ok := c.Vars[key]; ok {
return strconv.ParseInt(val, 10, 64)
}
return 0, fmt.Errorf("no such var: %s", key)
}
func (c *Context) QueryInt64(key string) (int64, error) {
query := c.Req.URL.Query()
return strconv.ParseInt(query.Get("page"), 10, 64)
}