Added filtered GET request

This commit is contained in:
Fran Jurmanović
2021-05-22 16:51:18 +02:00
parent f9ea943ed9
commit 8d4dc3ff57
6 changed files with 83 additions and 8 deletions

View File

@@ -0,0 +1,35 @@
package controllers
import (
"fmt"
"strconv"
"strings"
"wallet-api/pkg/models"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
func FilteredResponse(c *gin.Context) *models.FilteredResponse {
filtered := new(models.FilteredResponse)
page, _ := c.GetQuery("page")
rpp, _ := c.GetQuery("rpp")
sortBy, _ := c.GetQuery("sortBy")
dividers := [5]string{"|", " ", ".", "/", ","}
for _, div := range dividers {
sortArr := strings.Split(sortBy, div)
if len(sortArr) >= 2 {
sortBy = fmt.Sprintf("%s %s", common.ToSnakeCase(sortArr[0]), strings.ToUpper(sortArr[1]))
}
}
filtered.Embed, _ = c.GetQuery("embed")
filtered.Page, _ = strconv.Atoi(page)
filtered.Rpp, _ = strconv.Atoi(rpp)
filtered.SortBy = sortBy
return filtered
}

View File

@@ -34,10 +34,10 @@ func (wc *TransactionController) New(c *gin.Context) {
}
func (wc *TransactionController) GetAll(c *gin.Context) {
embed, _ := c.GetQuery("embed")
fr := FilteredResponse(c)
wallet, _ := c.GetQuery("walletId")
wm := wc.TransactionService.GetAll(wallet, embed)
wc.TransactionService.GetAll(wallet, fr)
c.JSON(200, wm)
c.JSON(200, fr)
}