Add support for offset in table rows endpoint, dry up code

This commit is contained in:
Dan Sosedoff
2016-01-07 21:18:22 -06:00
parent 9b5764d4fb
commit 61523e33df
3 changed files with 36 additions and 17 deletions

View File

@@ -1,9 +1,11 @@
package api
import (
"fmt"
"log"
"mime"
"path/filepath"
"strconv"
"github.com/gin-gonic/gin"
"github.com/sosedoff/pgweb/pkg/data"
@@ -33,6 +35,25 @@ func getQueryParam(c *gin.Context, name string) string {
return result
}
func parseIntFormValue(c *gin.Context, name string, defValue int) (int, error) {
val := c.Request.FormValue(name)
if val == "" {
return defValue, nil
}
num, err := strconv.Atoi(val)
if err != nil {
return defValue, fmt.Errorf("%s must be a number", name)
}
if num < 1 {
return defValue, fmt.Errorf("%s must be greated than 0", name)
}
return num, nil
}
func assetContentType(name string) string {
ext := filepath.Ext(name)
result := mime.TypeByExtension(ext)