This commit is contained in:
2026-08-21 23:24:38 +02:00
parent 2d43c227da
commit b6ef29ad2d
6 changed files with 1172 additions and 45 deletions
@@ -3,15 +3,16 @@ package controller
import ( import (
"net/http" "net/http"
"simplycpq/models" "simplycpq/models"
"simplycpq/services"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type ImportController struct { type ImportController struct {
importService *ImportService importService *services.ImportService
} }
func NewImportController(i *ImportService) *ImportController { func NewImportController(i *services.ImportService) *ImportController {
return &ImportController{ return &ImportController{
importService: i, importService: i,
} }
@@ -47,6 +48,7 @@ func (ic *ImportController) ImportProductGroups(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Import ProductGroups endpoint"}) c.JSON(http.StatusOK, gin.H{"message": "Import ProductGroups endpoint"})
} }
/*
func (ic *ImportController) ImportProducts(c *gin.Context) { func (ic *ImportController) ImportProducts(c *gin.Context) {
// Implement the logic for importing Products here // Implement the logic for importing Products here
var importData []models.ImportProduct var importData []models.ImportProduct
@@ -61,3 +63,4 @@ func (ic *ImportController) ImportProducts(c *gin.Context) {
} }
c.JSON(http.StatusOK, gin.H{"message": "Import Products endpoint"}) c.JSON(http.StatusOK, gin.H{"message": "Import Products endpoint"})
} }
*/
File diff suppressed because it is too large Load Diff
+13 -13
View File
@@ -1,11 +1,11 @@
package models package models
type ImportPropertyTypeGroup struct { type ImportPropertyTypeGroup struct {
ID int `json:"id,omitempty"` ID int `json:"id,omitempty"`
Label string `json:"label"` Label string `json:"label"`
Description string `json:"description"` Description string `json:"description"`
SubGroups []ImportPropertyTypeGroup `json:"subGroups,omitempty"` PropertyTypeGroups []ImportPropertyTypeGroup `json:"propertyTypeGroups,omitempty"`
PropertyTypes []ImportPropertyType `json:"propertyTypes,omitempty"` PropertyTypes []ImportPropertyType `json:"propertyTypes,omitempty"`
} }
type ImportPropertyType struct { type ImportPropertyType struct {
@@ -15,9 +15,9 @@ type ImportPropertyType struct {
} }
type ImportProperty struct { type ImportProperty struct {
ID int `json:"id,omitempty"` ID int `json:"id,omitempty"`
Label string `json:"label"` Label string `json:"label"`
Description string `json:"description"` Value string `json:"value"`
} }
type ImportProductGroup struct { type ImportProductGroup struct {
@@ -29,9 +29,9 @@ type ImportProductGroup struct {
} }
type ImportProduct struct { type ImportProduct struct {
ID int `json:"id,omitempty"` ID int `json:"id,omitempty"`
Label string `json:"label"` Label string `json:"label"`
Description string `json:"description"` Description string `json:"description"`
ProductGroupLabel string `json:"productGroupLabel, omitempty"` ProductGroupLabel string `json:"productGroupLabel,omitempty"`
Properties []ImportProperty `json:"properties,omitempty"` Properties []ImportProperty `json:"properties,omitempty"`
} }
@@ -37,8 +37,8 @@ func (r *ImportRepository) ImportPropertyTypeGroups(importData []models.ImportPr
return err return err
} }
// update subgroups if they exist // update subgroups if they exist
if data.SubGroups != nil { if data.PropertyTypeGroups != nil {
return r.ImportPropertyTypeGroups(data.SubGroups) return r.ImportPropertyTypeGroups(data.PropertyTypeGroups)
} }
} else { } else {
// Insert new record // Insert new record
@@ -46,8 +46,8 @@ func (r *ImportRepository) ImportPropertyTypeGroups(importData []models.ImportPr
if err != nil { if err != nil {
return err return err
} }
if data.SubGroups != nil { if data.PropertyTypeGroups != nil {
return r.ImportPropertyTypeGroups(data.SubGroups) return r.ImportPropertyTypeGroups(data.PropertyTypeGroups)
} }
} }
} }
@@ -124,40 +124,79 @@ func (r *ImportRepository) ImportProducts(importData []models.ImportProduct, gro
} }
func (r *ImportRepository) ImportProperties(importData []models.ImportProperty, productID int, groupID int) error { func (r *ImportRepository) ImportProperties(importData []models.ImportProperty, productID int, groupID int) error {
// Implement the logic to import Properties into the database
for _, property := range importData { for _, property := range importData {
query1 := "SELECT id FROM propertyTypes WHERE label = $1" propertyTypeID, err := r.findPropertyTypeID(property.Label)
rows, err := r.db.Query(query1, property.Label)
if err != nil { if err != nil {
return err return err
} }
defer rows.Close()
var pti int propertyID := property.ID
if rows.Next() { if propertyID != 0 {
if err := rows.Scan(&pti); err != nil { if err := r.updateProperty(property, propertyID, productID, groupID, propertyTypeID); err != nil {
return err
}
}
if property.ID != 0 {
// Update existing Property
query := "UPDATE properties SET label = $1, description = $2, propertyTypeId= $3, productId = $4, productGroupId =$5 WHERE id = $6"
_, err := r.db.Exec(query, property.Label, property.Description, pti, productID, groupID, property.ID)
if err != nil {
return err return err
} }
} else { } else {
// Insert new Property propertyID, err = r.insertProperty(property, productID, groupID, propertyTypeID)
query := "INSERT INTO properties (label, description, productId, propertyTypeId, productGroupId) VALUES ($1, $2, $3, $4, $5) RETURNING id"
err := r.db.QueryRow(query, property.Label, property.Description, productID, pti, groupID).Scan(&property.ID)
if err != nil { if err != nil {
return err return err
} }
} }
if err := r.upsertValue(propertyID, property.Value); err != nil {
return err
}
if err := r.upsertValue(propertyTypeID, property.Value); err != nil {
return err
}
} }
return nil return nil
} }
func (r *ImportRepository) findPropertyTypeID(label string) (int, error) {
var propertyTypeID int
err := r.db.QueryRow("SELECT id FROM propertyTypes WHERE label = $1", label).Scan(&propertyTypeID)
if err != nil {
if err == sql.ErrNoRows {
return 0, nil
}
return 0, err
}
return propertyTypeID, nil
}
func (r *ImportRepository) updateProperty(property models.ImportProperty, propertyID int, productID int, groupID int, propertyTypeID int) error {
query := "UPDATE properties SET label = $1, description = $2, propertyTypeId = $3, productId = $4, productGroupId = $5 WHERE id = $6"
_, err := r.db.Exec(query, property.Label, property.Label, propertyTypeID, productID, groupID, propertyID)
return err
}
func (r *ImportRepository) insertProperty(property models.ImportProperty, productID int, groupID int, propertyTypeID int) (int, error) {
query := "INSERT INTO properties (label, description, productId, propertyTypeId, productGroupId) VALUES ($1, $2, $3, $4, $5) RETURNING id"
var propertyID int
err := r.db.QueryRow(query, property.Label, property.Label, productID, propertyTypeID, groupID).Scan(&propertyID)
if err != nil {
return 0, err
}
return propertyID, nil
}
func (r *ImportRepository) upsertValue(propertyID int, value string) error {
var existingValueID int
err := r.db.QueryRow("SELECT id FROM values WHERE label = $1 AND propertyId = $2", value, propertyID).Scan(&existingValueID)
if err != nil {
if err != sql.ErrNoRows {
return err
}
_, err = r.db.Exec("INSERT INTO values (label, description, propertyId) VALUES ($1, $2, $3)", value, "", propertyID)
return err
}
_, err = r.db.Exec("UPDATE values SET label = $1, description = $2 WHERE id = $3", value, "", existingValueID)
return err
}
func (r *ImportRepository) ImportPropertyTypes(importData []models.ImportPropertyType, groupID int) error { func (r *ImportRepository) ImportPropertyTypes(importData []models.ImportPropertyType, groupID int) error {
for _, propertyType := range importData { for _, propertyType := range importData {
if propertyType.ID != 0 { if propertyType.ID != 0 {
+1 -1
View File
@@ -95,7 +95,7 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
// Import functionality // Import functionality
router.POST("/importPropertyTypeGroups", importController.ImportPropertyTypeGroups) // import PropertyTypeGroups router.POST("/importPropertyTypeGroups", importController.ImportPropertyTypeGroups) // import PropertyTypeGroups
router.POST("/importProductGroups", importController.ImportProductGroups) // import ProductGroups router.POST("/importProductGroups", importController.ImportProductGroups) // import ProductGroups
router.POST("/importProducts", importController.ImportProducts) // import Products // router.POST("/importProducts", importController.ImportProducts) // import Products
// server // server
return HttpServer{ return HttpServer{
@@ -23,6 +23,8 @@ func (is *ImportService) ImportProductGroups(importData []models.ImportProductGr
return is.importRepository.ImportProductGroups(importData) return is.importRepository.ImportProductGroups(importData)
} }
/*
func (is *ImportService) ImportProducts(importData []models.ImportProduct) error { func (is *ImportService) ImportProducts(importData []models.ImportProduct) error {
return is.importRepository.ImportProducts(importData) return is.importRepository.ImportProducts(importData)
} }
*/