mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
Added filtered GET request
This commit is contained in:
35
pkg/controllers/controllers.go
Normal file
35
pkg/controllers/controllers.go
Normal 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
|
||||||
|
}
|
||||||
@@ -34,10 +34,10 @@ func (wc *TransactionController) New(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (wc *TransactionController) GetAll(c *gin.Context) {
|
func (wc *TransactionController) GetAll(c *gin.Context) {
|
||||||
embed, _ := c.GetQuery("embed")
|
fr := FilteredResponse(c)
|
||||||
wallet, _ := c.GetQuery("walletId")
|
wallet, _ := c.GetQuery("walletId")
|
||||||
|
|
||||||
wm := wc.TransactionService.GetAll(wallet, embed)
|
wc.TransactionService.GetAll(wallet, fr)
|
||||||
|
|
||||||
c.JSON(200, wm)
|
c.JSON(200, fr)
|
||||||
}
|
}
|
||||||
|
|||||||
14
pkg/models/models.go
Normal file
14
pkg/models/models.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
type FilteredResponse struct {
|
||||||
|
Items interface{} `json:"items"`
|
||||||
|
SortBy string `json:"sortBy"`
|
||||||
|
Embed string `json:"embed"`
|
||||||
|
Page int `json:"page"`
|
||||||
|
Rpp int `json:"rpp"`
|
||||||
|
TotalRecords int `json:"totalRecords"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResponseFunc func(*gin.Context) *[]interface{}
|
||||||
18
pkg/services/services.go
Normal file
18
pkg/services/services.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
"wallet-api/pkg/utl/common"
|
||||||
|
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FilteredResponse(qry *pg.Query, mdl interface{}, filtered *models.FilteredResponse) {
|
||||||
|
qry = qry.Limit(filtered.Rpp).Offset((filtered.Page - 1) * filtered.Rpp)
|
||||||
|
common.GenerateEmbed(qry, filtered.Embed)
|
||||||
|
count, err := qry.SelectAndCount()
|
||||||
|
common.CheckError(err)
|
||||||
|
|
||||||
|
filtered.TotalRecords = count
|
||||||
|
filtered.Items = mdl
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
"wallet-api/pkg/utl/common"
|
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
"github.com/go-pg/pg/v10"
|
||||||
)
|
)
|
||||||
@@ -25,11 +24,9 @@ func (as *TransactionService) New(body *models.NewTransactionBody) *models.Trans
|
|||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *TransactionService) GetAll(walletId string, embed string) *[]models.Transaction {
|
func (as *TransactionService) GetAll(walletId string, filtered *models.FilteredResponse) {
|
||||||
wm := new([]models.Transaction)
|
wm := new([]models.Transaction)
|
||||||
|
|
||||||
query := as.Db.Model(wm).Where("? = ?", pg.Ident("wallet_id"), walletId)
|
query := as.Db.Model(wm).Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||||
common.GenerateEmbed(query, embed).Select()
|
FilteredResponse(query, wm, filtered)
|
||||||
|
|
||||||
return wm
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CheckError(err error) {
|
func CheckError(err error) {
|
||||||
@@ -9,3 +11,12 @@ func CheckError(err error) {
|
|||||||
log.Fatalf("Error occured. %v", err)
|
log.Fatalf("Error occured. %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
|
||||||
|
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
|
||||||
|
|
||||||
|
func ToSnakeCase(str string) string {
|
||||||
|
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
|
||||||
|
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
|
||||||
|
return strings.ToLower(snake)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user