Add support for offset in table rows endpoint, dry up code
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -109,27 +108,21 @@ func GetTable(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetTableRows(c *gin.Context) {
|
||||
limit := 1000 // Number of rows to fetch
|
||||
limitVal := c.Request.FormValue("limit")
|
||||
offset, err := parseIntFormValue(c, "offset", 0)
|
||||
if err != nil {
|
||||
c.JSON(400, NewError(err))
|
||||
return
|
||||
}
|
||||
|
||||
if limitVal != "" {
|
||||
num, err := strconv.Atoi(limitVal)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(400, Error{"Invalid limit value"})
|
||||
return
|
||||
}
|
||||
|
||||
if num <= 0 {
|
||||
c.JSON(400, Error{"Limit should be greater than 0"})
|
||||
return
|
||||
}
|
||||
|
||||
limit = num
|
||||
limit, err := parseIntFormValue(c, "limit", 100)
|
||||
if err != nil {
|
||||
c.JSON(400, NewError(err))
|
||||
return
|
||||
}
|
||||
|
||||
opts := client.RowsOptions{
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
SortColumn: c.Request.FormValue("sort_column"),
|
||||
SortOrder: c.Request.FormValue("sort_order"),
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user