340 lines
12 KiB
Go
340 lines
12 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"simplycpq/models"
|
|
"simplycpq/services"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ValueController struct {
|
|
valueService *services.ValueService
|
|
}
|
|
|
|
func NewValueController(valueService *services.ValueService) *ValueController {
|
|
return &ValueController{
|
|
valueService: valueService,
|
|
}
|
|
}
|
|
|
|
func (vc *ValueController) CreatePropertyTypeGroup(c *gin.Context) {
|
|
var propertyTypeGroup models.PropertyTypeGroup
|
|
if err := c.ShouldBindJSON(&propertyTypeGroup); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
createdGroup, err2 := vc.valueService.CreatePropertyTypeGroup(&propertyTypeGroup)
|
|
if err2 != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err2.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyTypeGroup created", "data": createdGroup})
|
|
}
|
|
|
|
func (vc *ValueController) UpdatePropertyTypeGroup(c *gin.Context) {
|
|
var propertyTypeGroup models.PropertyTypeGroup
|
|
if err := c.ShouldBindJSON(&propertyTypeGroup); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
err := vc.valueService.UpdatePropertyTypeGroup(&propertyTypeGroup)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyTypeGroup updated", "data": propertyTypeGroup})
|
|
}
|
|
|
|
func (vc *ValueController) DeletePropertyTypeGroup(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type group id"})
|
|
return
|
|
}
|
|
err = vc.valueService.DeletePropertyTypeGroup(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyTypeGroup deleted", "data": id})
|
|
}
|
|
|
|
func (vc *ValueController) GetPropertyTypeGroup(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type group id"})
|
|
return
|
|
}
|
|
propertyTypeGroup, err := vc.valueService.GetPropertyTypeGroup(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyTypeGroup retrieved", "data": propertyTypeGroup})
|
|
}
|
|
|
|
func (vc *ValueController) GetPropertyTypeGroupsByParentId(c *gin.Context) {
|
|
parentId, err := strconv.Atoi(c.Param("parentId"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid parent id"})
|
|
return
|
|
}
|
|
propertyTypeGroups, err := vc.valueService.GetPropertyTypeGroupsByParentId(parentId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyTypeGroups retrieved by parent ID", "parentId": parentId, "data": propertyTypeGroups})
|
|
}
|
|
|
|
func (vc *ValueController) DeletePropertyTypeFromPropertyTypeGroupId(c *gin.Context) {
|
|
propertyTypeGroupId := c.Param("propertyTypeGroupId")
|
|
id, err := strconv.Atoi(propertyTypeGroupId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type group id"})
|
|
return
|
|
}
|
|
err = vc.valueService.DeletePropertyTypeFromPropertyTypeGroupId(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyType deleted from PropertyTypeGroup", "propertyTypeGroupId": id})
|
|
}
|
|
|
|
func (vc *ValueController) CreatePropertyType(c *gin.Context) {
|
|
var propertyType models.PropertyType
|
|
if err := c.ShouldBindJSON(&propertyType); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
var propertyType2 *models.PropertyType
|
|
propertyType2, err := vc.valueService.CreatePropertyType(&propertyType)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyType created", "data": propertyType2})
|
|
}
|
|
|
|
func (vc *ValueController) UpdatePropertyType(c *gin.Context) {
|
|
var propertyType models.PropertyType
|
|
if err := c.ShouldBindJSON(&propertyType); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
err := vc.valueService.UpdatePropertyType(&propertyType)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyType updated", "data": propertyType})
|
|
}
|
|
|
|
func (vc *ValueController) DeletePropertyType(c *gin.Context) {
|
|
id := c.Param("id")
|
|
idInt, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type id"})
|
|
return
|
|
}
|
|
err = vc.valueService.DeletePropertyType(idInt)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyType deleted", "id": idInt})
|
|
}
|
|
|
|
func (vc *ValueController) GetPropertyType(c *gin.Context) {
|
|
id := c.Param("id")
|
|
idInt, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type id"})
|
|
return
|
|
}
|
|
propertyType, err := vc.valueService.GetPropertyType(idInt)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyType retrieved", "data": propertyType})
|
|
}
|
|
|
|
func (vc *ValueController) GetPropertyTypesByGroupId(c *gin.Context) {
|
|
groupId := c.Param("groupId")
|
|
groupIdInt, err := strconv.Atoi(groupId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid group id"})
|
|
return
|
|
}
|
|
propertyTypes, err := vc.valueService.GetPropertyTypesByGroupId(groupIdInt)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "PropertyTypes retrieved by group ID", "data": propertyTypes})
|
|
}
|
|
|
|
func (vc *ValueController) CreateValueForPropertyTypeId(c *gin.Context) {
|
|
propertyTypeId := c.Param("propertyTypeId")
|
|
var value models.Value
|
|
if err := c.ShouldBindJSON(&value); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
propertyTypeIdInt, err := strconv.Atoi(propertyTypeId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type id"})
|
|
return
|
|
}
|
|
value2, err := vc.valueService.CreateValueForPropertyTypeId(propertyTypeIdInt, &value)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Value created for PropertyType", "propertyTypeId": propertyTypeIdInt, "data": value2})
|
|
}
|
|
|
|
func (vc *ValueController) DeleteValueFromPropertyTypeId(c *gin.Context) {
|
|
propertyTypeId := c.Param("propertyTypeId")
|
|
propertyTypeIdInt, err := strconv.Atoi(propertyTypeId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type id"})
|
|
return
|
|
}
|
|
err = vc.valueService.DeleteValueFromPropertyTypeId(propertyTypeIdInt)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Value deleted from PropertyType", "propertyTypeId": propertyTypeIdInt})
|
|
}
|
|
|
|
func (vc *ValueController) GetValuesFromPropertyTypeId(c *gin.Context) {
|
|
propertyTypeId := c.Param("propertyTypeId")
|
|
propertyTypeIdInt, err := strconv.Atoi(propertyTypeId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type id"})
|
|
return
|
|
}
|
|
values, err := vc.valueService.GetValuesFromPropertyTypeId(propertyTypeIdInt)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Values retrieved from PropertyType", "propertyTypeId": propertyTypeIdInt, "data": values})
|
|
}
|
|
|
|
func (vc *ValueController) GetValuesFromPropertyId(c *gin.Context) {
|
|
propertyId := c.Param("propertyId")
|
|
propertyIdInt, err := strconv.Atoi(propertyId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property id"})
|
|
return
|
|
}
|
|
values, err := vc.valueService.GetValuesFromPropertyId(propertyIdInt)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Values retrieved from Property", "propertyId": propertyIdInt, "data": values})
|
|
}
|
|
|
|
func (vc *ValueController) DeleteValueFromPropertyId(c *gin.Context) {
|
|
valueId := c.Param("valueId")
|
|
propertyId := c.Param("propertyId")
|
|
valueIdInt, err := strconv.Atoi(valueId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid value id"})
|
|
return
|
|
}
|
|
propertyIdInt, err := strconv.Atoi(propertyId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property id"})
|
|
return
|
|
}
|
|
err = vc.valueService.DeleteValueFromPropertyId(valueIdInt, propertyIdInt)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Value deleted from Property", "valueId": valueIdInt, "propertyId": propertyIdInt})
|
|
}
|
|
|
|
func (vc *ValueController) GetValue(c *gin.Context) {
|
|
id := c.Param("id")
|
|
idInt, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid value id"})
|
|
return
|
|
}
|
|
value, err := vc.valueService.GetValue(idInt)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Value retrieved", "id": idInt, "data": value})
|
|
}
|
|
|
|
func (vc *ValueController) CreateTerminalPropertyForProductGroupId(c *gin.Context) {
|
|
productGroupId := c.Param("productGroupId")
|
|
propertyTypeId := c.Param("propertyTypeId")
|
|
value := c.Param("value")
|
|
productGroupIdInt, err := strconv.Atoi(productGroupId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid product group id"})
|
|
return
|
|
}
|
|
propertyTypeIdInt, err := strconv.Atoi(propertyTypeId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type id"})
|
|
return
|
|
}
|
|
err = vc.valueService.CreateTerminalPropertyForProductGroupId(productGroupIdInt, propertyTypeIdInt, value)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Terminal Property created for ProductGroup", "productGroupId": productGroupIdInt, "propertyTypeId": propertyTypeIdInt, "value": value})
|
|
}
|
|
|
|
func (vc *ValueController) CreateTerminalPropertyForProductId(c *gin.Context) {
|
|
productId := c.Param("productId")
|
|
propertyTypeId := c.Param("propertyTypeId")
|
|
value := c.Param("value")
|
|
productIdInt, err := strconv.Atoi(productId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid product id"})
|
|
return
|
|
}
|
|
propertyTypeIdInt, err := strconv.Atoi(propertyTypeId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid property type id"})
|
|
return
|
|
}
|
|
err = vc.valueService.CreateTerminalPropertyForProductId(productIdInt, propertyTypeIdInt, value)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Terminal Property created for Product", "productId": productIdInt, "propertyTypeId": propertyTypeIdInt, "value": value})
|
|
}
|
|
|
|
func (vc *ValueController) SetTerminalValueForProperty(c *gin.Context) {
|
|
propertyId, err := strconv.Atoi(c.Param("propertyId"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid property ID"})
|
|
return
|
|
}
|
|
value := c.Param("value")
|
|
err = vc.valueService.SetTerminalValueForProperty(propertyId, value)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Terminal value set for property successfully"})
|
|
}
|