82 lines
2.9 KiB
Go
82 lines
2.9 KiB
Go
package restricted
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"git.ma-al.com/goc_daniel/b2b/app/config"
|
|
"git.ma-al.com/goc_daniel/b2b/app/service/productDescriptionService"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
// ProductDescriptionHandler handles endpoints that receive, save and translate product descriptions.
|
|
type ProductDescriptionHandler struct {
|
|
productDescriptionService *productDescriptionService.ProductDescriptionService
|
|
config *config.Config
|
|
}
|
|
|
|
// NewProductDescriptionHandler creates a new ProductDescriptionHandler instance
|
|
func NewProductDescriptionHandler() *ProductDescriptionHandler {
|
|
productDescriptionService := productDescriptionService.New()
|
|
return &ProductDescriptionHandler{
|
|
productDescriptionService: productDescriptionService,
|
|
config: config.Get(),
|
|
}
|
|
}
|
|
|
|
// ProductDescriptionRoutes registers all product description routes
|
|
func ProductDescriptionHandlerRoutes(r fiber.Router) fiber.Router {
|
|
handler := NewProductDescriptionHandler()
|
|
|
|
r.Get("/get-product-description", handler.GetProductDescription)
|
|
// r.Get("/get-years", handler.GetYears)
|
|
// r.Get("/get-quarters", handler.GetQuarters)
|
|
// r.Get("/get-issues", handler.GetIssues)
|
|
|
|
return r
|
|
}
|
|
|
|
// GetProductDescription returns the product description for a given product ID
|
|
func (h *ProductDescriptionHandler) GetProductDescription(c fiber.Ctx) error {
|
|
userID, ok := c.Locals("userID").(uint)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).JSON(fiber.Map{
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody), // possibly could return a dfifferent error
|
|
})
|
|
}
|
|
|
|
productID_attribute := c.Query("productID")
|
|
productID, err := strconv.Atoi(productID_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).JSON(fiber.Map{
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute),
|
|
})
|
|
}
|
|
|
|
productShopID_attribute := c.Query("productShopID")
|
|
productShopID, err := strconv.Atoi(productShopID_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).JSON(fiber.Map{
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute),
|
|
})
|
|
}
|
|
|
|
productLangID_attribute := c.Query("productLangID")
|
|
productLangID, err := strconv.Atoi(productLangID_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).JSON(fiber.Map{
|
|
"error": responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute),
|
|
})
|
|
}
|
|
|
|
response, err := h.productDescriptionService.GetProductDescription(userID, uint(productID), uint(productShopID), uint(productLangID))
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(err)).JSON(fiber.Map{
|
|
"error": responseErrors.GetErrorCode(c, err),
|
|
})
|
|
}
|
|
|
|
return c.JSON(response)
|
|
}
|