relynolli-server/handlers/catalog/endpoints/catalog.go

42 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-03-15 21:27:45 +03:00
package endpoints
import (
2024-03-21 19:46:20 +03:00
"fmt"
2024-03-15 21:27:45 +03:00
"github.com/gin-gonic/gin"
"relynolli-server/models"
"relynolli-server/services"
"strconv"
)
func (h *handlers) GetCatalogItems(c *gin.Context) {
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
offset := (page - 1) * limit
if c.DefaultQuery("isFilter", "0") == "0" {
c.JSON(200, services.GetCatalogItems(limit, offset))
return
}
2024-03-21 19:46:20 +03:00
c.JSON(200, services.FilterCatalogItems(c.Request.URL.Query(), limit, offset))
2024-03-15 21:27:45 +03:00
}
func (h *handlers) GetCatalogItem(c *gin.Context) {
code := c.Param("code")
if code == "" {
c.JSON(400, models.Response{Status: 400, Info: "product \"Code\" should be provided"})
return
}
resp, err := services.GetCatalogItem(code)
if err != nil {
c.JSON(404, models.Response{Status: 404, Info: err.Error()})
return
}
c.JSON(200, resp)
}
2024-03-21 19:46:20 +03:00
func (h *handlers) Count(c *gin.Context) {
c.JSON(200, models.Response{Status: 200, Info: fmt.Sprintf("%d", services.GetCatalogItemsCount())})
}