143 lines
4.7 KiB
Go
143 lines
4.7 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"simplycpq/models"
|
|
"simplycpq/services"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ProductController struct {
|
|
productService *services.ProductService
|
|
productGroupService *services.ProductGroupService
|
|
valueService *services.ValueService
|
|
}
|
|
|
|
func NewProductController(productService *services.ProductService, productGroupService *services.ProductGroupService, valueService *services.ValueService) *ProductController {
|
|
return &ProductController{
|
|
productService: productService,
|
|
productGroupService: productGroupService,
|
|
valueService: valueService,
|
|
}
|
|
}
|
|
|
|
func (pc *ProductController) CreateProduct(c *gin.Context) {
|
|
var product models.Product
|
|
if err := c.ShouldBindJSON(&product); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
createdProduct, err := pc.productService.CreateProduct(&product)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "product created successfully", "data": createdProduct})
|
|
}
|
|
|
|
func (pc *ProductController) UpdateProduct(c *gin.Context) {
|
|
var product models.Product
|
|
if err := c.ShouldBindJSON(&product); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if err := pc.productService.UpdateProduct(&product); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "product updated successfully"})
|
|
}
|
|
|
|
func (pc *ProductController) DeleteProduct(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid product id"})
|
|
return
|
|
}
|
|
if err := pc.productService.DeleteProduct(id); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "product deleted successfully"})
|
|
}
|
|
|
|
func (pc *ProductController) GetProduct(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid product id"})
|
|
return
|
|
}
|
|
product, err := pc.productService.GetProductById(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "product retrieved successfully", "product": product})
|
|
}
|
|
|
|
func (pc *ProductController) GetProductsByGroupId(c *gin.Context) {
|
|
groupId, err := strconv.Atoi(c.Param("groupId"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid group id"})
|
|
return
|
|
}
|
|
products, err := pc.productService.GetProductsByGroupId(groupId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "products retrieved successfully", "groupId": groupId, "products": products})
|
|
}
|
|
|
|
func (pc *ProductController) AddPropertyToProductId(c *gin.Context) {
|
|
productId, err := strconv.Atoi(c.Param("productId"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid product id"})
|
|
return
|
|
}
|
|
propertyId, err := strconv.Atoi(c.Param("propertyId"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property id"})
|
|
return
|
|
}
|
|
if err := pc.productService.AddPropertyToProduct(productId, propertyId); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "property added to product successfully", "productId": productId, "propertyId": propertyId})
|
|
}
|
|
|
|
func (pc *ProductController) DeletePropertyFromProductId(c *gin.Context) {
|
|
productId, err := strconv.Atoi(c.Param("productId"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid product id"})
|
|
return
|
|
}
|
|
propertyId, err := strconv.Atoi(c.Param("propertyId"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property id"})
|
|
return
|
|
}
|
|
if err := pc.productService.DeletePropertyFromProduct(productId, propertyId); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "property deleted from product successfully", "productId": productId, "propertyId": propertyId})
|
|
}
|
|
|
|
func (pc *ProductController) GetPropertiesFromProductId(c *gin.Context) {
|
|
productId, err := strconv.Atoi(c.Param("productId"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid product id"})
|
|
return
|
|
}
|
|
properties, err := pc.productService.GetPropertiesFromProduct(productId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "properties retrieved from product successfully", "productId": productId, "properties": properties})
|
|
}
|