add count method for pagination

features/feature-catalog-redis
Ernest Litvinenko 2024-03-21 19:46:20 +03:00
parent 32efec31e2
commit 115fb5d527
6 changed files with 34 additions and 37 deletions

View File

@ -1,6 +1,7 @@
package endpoints package endpoints
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"relynolli-server/models" "relynolli-server/models"
"relynolli-server/services" "relynolli-server/services"
@ -16,7 +17,7 @@ func (h *handlers) GetCatalogItems(c *gin.Context) {
c.JSON(200, services.GetCatalogItems(limit, offset)) c.JSON(200, services.GetCatalogItems(limit, offset))
return return
} }
c.JSON(200, services.FilterCatalogItems(c.Request.URL.Query())) c.JSON(200, services.FilterCatalogItems(c.Request.URL.Query(), limit, offset))
} }
func (h *handlers) GetCatalogItem(c *gin.Context) { func (h *handlers) GetCatalogItem(c *gin.Context) {
@ -34,3 +35,7 @@ func (h *handlers) GetCatalogItem(c *gin.Context) {
c.JSON(200, resp) c.JSON(200, resp)
} }
func (h *handlers) Count(c *gin.Context) {
c.JSON(200, models.Response{Status: 200, Info: fmt.Sprintf("%d", services.GetCatalogItemsCount())})
}

View File

@ -8,6 +8,7 @@ type Handlers interface {
GetFilters(c *gin.Context) GetFilters(c *gin.Context)
GetCatalogItems(c *gin.Context) GetCatalogItems(c *gin.Context)
GetCatalogItem(c *gin.Context) GetCatalogItem(c *gin.Context)
Count(c *gin.Context)
} }
func GetHandlers() Handlers { func GetHandlers() Handlers {

View File

@ -9,6 +9,7 @@ func HandleRoutes(parent *gin.RouterGroup) {
h := endpoints.GetHandlers() h := endpoints.GetHandlers()
catalog := parent.Group("/catalog") catalog := parent.Group("/catalog")
catalog.GET("/filters", h.GetFilters) catalog.GET("/filters", h.GetFilters)
catalog.GET("/", h.GetCatalogItems) catalog.GET("/count", h.Count)
catalog.GET("", h.GetCatalogItems)
catalog.GET("/:code", h.GetCatalogItem) catalog.GET("/:code", h.GetCatalogItem)
} }

View File

@ -1,15 +1,16 @@
package main package main
import ( import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"log" "log"
"os" "os"
"os/signal" "os/signal"
"relynolli-server/handlers" "relynolli-server/handlers"
"relynolli-server/internal" "relynolli-server/internal"
"syscall" "syscall"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
) )
func main() { func main() {

View File

@ -22,32 +22,6 @@ func GetCartItems(fuserId int) []models.CatalogWithQuantityWeb {
} }
return result return result
//cartList := []models.CatalogWithQuantity{}
//returnedList := []models.CatalogWithQuantityWeb{}
//
//stmt := fmt.Sprintf(`select t1.*, t2.quantity, t3.QUANTITY as available_quantity from api_catalog t1
// join api_cart t2 on t1.id = t2.product_id
// left join b_catalog_product t3 on t1.id = t3.ID where t2.fuser_id = %d;`, fuserId)
//
//retrieveItems(stmt, &cartList)
//
//for _, item := range cartList {
// itemProd := models.CatalogWithQuantityWeb{
// Id: item.Id,
// Code: item.Code,
// Name: item.Name,
// IsActive: item.IsActive,
// DetailText: item.DetailText,
// Quantity: item.Quantity,
// AvailableQuantity: item.AvailableQuantity,
// }
// json.Unmarshal(item.Price, &itemProd.Price)
// json.Unmarshal(item.Properties, &itemProd.Properties)
// returnedList = append(returnedList, itemProd)
//}
//
//return returnedList
} }
func CreateFuser() int64 { func CreateFuser() int64 {

View File

@ -34,8 +34,18 @@ func retrieveCatalogItems(stmt string) []models.CatalogStructWeb {
return returnedList return returnedList
} }
func GetCatalogItemsCount() int {
stmt := "select count(id) from api_catalog where available_quantity > 0 and is_active = 1;"
var count int
db := internal.InitDatabase()
rows := db.Query(stmt)
rows.Next()
rows.Scan(&count)
return count
}
func GetCatalogItems(limit int, offset int) []models.CatalogStructWeb { func GetCatalogItems(limit int, offset int) []models.CatalogStructWeb {
stmt := fmt.Sprintf("select * from api_catalog order by id DESC limit %d offset %d;", limit, offset) stmt := fmt.Sprintf("select * from api_catalog where available_quantity > 0 and is_active = 1 order by code limit %d offset %d;", limit, offset)
return retrieveCatalogItems(stmt) return retrieveCatalogItems(stmt)
} }
@ -57,11 +67,11 @@ func GetCatalogItemById(id int) (models.CatalogStructWeb, error) {
return retrieveCatalogItems(stmt)[0], nil return retrieveCatalogItems(stmt)[0], nil
} }
func FilterCatalogItems(filters map[string][]string) []models.CatalogStructWeb { func FilterCatalogItems(filters map[string][]string, limit int, offset int) []models.CatalogStructWeb {
// Generate stmt // Generate stmt
propertiesSubStmt := "properties->>'$.%s' = '%s'" propertiesSubStmt := "properties->>'$.%s' = '%s'"
stmt := "select * from api_catalog where %s" stmt := "select * from api_catalog"
sample := "(%s)" sample := "(%s)"
@ -71,6 +81,13 @@ func FilterCatalogItems(filters map[string][]string) []models.CatalogStructWeb {
if key == "isFilter" { if key == "isFilter" {
continue continue
} }
if key == "limit" {
continue
}
if key == "offset" {
continue
}
subFilterArr := []string{} subFilterArr := []string{}
values := strings.Split(filter[0], ",") values := strings.Split(filter[0], ",")
for _, val := range values { for _, val := range values {
@ -86,7 +103,5 @@ func FilterCatalogItems(filters map[string][]string) []models.CatalogStructWeb {
} }
stmt = fmt.Sprintf(stmt, strings.Join(samples, " and ")) stmt = fmt.Sprintf(stmt, strings.Join(samples, " and "))
println(stmt) return retrieveCatalogItems(stmt + fmt.Sprintf("and is_active = 1 and available_quantity > 0 limit %d offset %d", limit, offset))
return retrieveCatalogItems(stmt)
} }