diff --git a/handlers/catalog/endpoints/catalog.go b/handlers/catalog/endpoints/catalog.go index 7b40954..1988a83 100644 --- a/handlers/catalog/endpoints/catalog.go +++ b/handlers/catalog/endpoints/catalog.go @@ -1,6 +1,7 @@ package endpoints import ( + "fmt" "github.com/gin-gonic/gin" "relynolli-server/models" "relynolli-server/services" @@ -16,7 +17,7 @@ func (h *handlers) GetCatalogItems(c *gin.Context) { c.JSON(200, services.GetCatalogItems(limit, offset)) 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) { @@ -34,3 +35,7 @@ func (h *handlers) GetCatalogItem(c *gin.Context) { c.JSON(200, resp) } + +func (h *handlers) Count(c *gin.Context) { + c.JSON(200, models.Response{Status: 200, Info: fmt.Sprintf("%d", services.GetCatalogItemsCount())}) +} diff --git a/handlers/catalog/endpoints/ep.go b/handlers/catalog/endpoints/ep.go index 8cb2605..8348297 100644 --- a/handlers/catalog/endpoints/ep.go +++ b/handlers/catalog/endpoints/ep.go @@ -8,6 +8,7 @@ type Handlers interface { GetFilters(c *gin.Context) GetCatalogItems(c *gin.Context) GetCatalogItem(c *gin.Context) + Count(c *gin.Context) } func GetHandlers() Handlers { diff --git a/handlers/catalog/routes.go b/handlers/catalog/routes.go index e917827..e58559c 100644 --- a/handlers/catalog/routes.go +++ b/handlers/catalog/routes.go @@ -9,6 +9,7 @@ func HandleRoutes(parent *gin.RouterGroup) { h := endpoints.GetHandlers() catalog := parent.Group("/catalog") catalog.GET("/filters", h.GetFilters) - catalog.GET("/", h.GetCatalogItems) + catalog.GET("/count", h.Count) + catalog.GET("", h.GetCatalogItems) catalog.GET("/:code", h.GetCatalogItem) } diff --git a/main.go b/main.go index 22f3a8e..88b5c71 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,16 @@ package main import ( - "github.com/gin-contrib/cors" - "github.com/gin-gonic/gin" - "github.com/joho/godotenv" "log" "os" "os/signal" "relynolli-server/handlers" "relynolli-server/internal" "syscall" + + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" + "github.com/joho/godotenv" ) func main() { diff --git a/services/cart.go b/services/cart.go index dc781c0..a918cd9 100644 --- a/services/cart.go +++ b/services/cart.go @@ -22,32 +22,6 @@ func GetCartItems(fuserId int) []models.CatalogWithQuantityWeb { } 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 { diff --git a/services/catalog.go b/services/catalog.go index 4ec1905..ccd658b 100644 --- a/services/catalog.go +++ b/services/catalog.go @@ -34,8 +34,18 @@ func retrieveCatalogItems(stmt string) []models.CatalogStructWeb { 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 { - 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) } @@ -57,11 +67,11 @@ func GetCatalogItemById(id int) (models.CatalogStructWeb, error) { 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 propertiesSubStmt := "properties->>'$.%s' = '%s'" - stmt := "select * from api_catalog where %s" + stmt := "select * from api_catalog" sample := "(%s)" @@ -71,6 +81,13 @@ func FilterCatalogItems(filters map[string][]string) []models.CatalogStructWeb { if key == "isFilter" { continue } + if key == "limit" { + continue + } + if key == "offset" { + continue + } + subFilterArr := []string{} values := strings.Split(filter[0], ",") for _, val := range values { @@ -86,7 +103,5 @@ func FilterCatalogItems(filters map[string][]string) []models.CatalogStructWeb { } stmt = fmt.Sprintf(stmt, strings.Join(samples, " and ")) - println(stmt) - - return retrieveCatalogItems(stmt) + return retrieveCatalogItems(stmt + fmt.Sprintf("and is_active = 1 and available_quantity > 0 limit %d offset %d", limit, offset)) }