import
This commit is contained in:
@@ -67,7 +67,7 @@ func (r *ImportRepository) ImportProductGroups(importData []models.ImportProduct
|
||||
return err
|
||||
}
|
||||
//
|
||||
if err := r.ImportProducts(data.Products); err != nil {
|
||||
if err := r.ImportProducts(data.Products, data.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
// update subgroups if they exist
|
||||
@@ -80,7 +80,7 @@ func (r *ImportRepository) ImportProductGroups(importData []models.ImportProduct
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.ImportProducts(data.Products); err != nil {
|
||||
if err := r.ImportProducts(data.Products, data.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
if data.SubGroups != nil {
|
||||
@@ -91,15 +91,70 @@ func (r *ImportRepository) ImportProductGroups(importData []models.ImportProduct
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ImportRepository) ImportProducts(importData []models.ImportProduct) error {
|
||||
func (r *ImportRepository) ImportProducts(importData []models.ImportProduct, groupID int) error {
|
||||
// Implement the logic to import Products into the database
|
||||
// This is a placeholder implementation; you need to write the actual logic
|
||||
for _, product := range importData {
|
||||
if product.ID != 0 {
|
||||
// Update existing Product
|
||||
query := "UPDATE products SET label = $1, description = $2, productGroupId = $3 WHERE id = $4"
|
||||
_, err := r.db.Exec(query, product.Label, product.Description, groupID, product.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if product.Properties != nil {
|
||||
if err := r.ImportProperties(product.Properties, product.ID, groupID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Insert new Product
|
||||
query := "INSERT INTO products (label, description, productGroupId) VALUES ($1, $2, $3) RETURNING id"
|
||||
err := r.db.QueryRow(query, product.Label, product.Description, groupID).Scan(&product.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if product.Properties != nil {
|
||||
if err := r.ImportProperties(product.Properties, product.ID, groupID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ImportRepository) ImportProperties(importData []models.ImportProperty) error {
|
||||
func (r *ImportRepository) ImportProperties(importData []models.ImportProperty, productID int, groupID int) error {
|
||||
// Implement the logic to import Properties into the database
|
||||
// This is a placeholder implementation; you need to write the actual logic
|
||||
|
||||
for _, property := range importData {
|
||||
query1 := "SELECT id FROM propertyTypes WHERE label = $1"
|
||||
rows, err := r.db.Query(query1, property.Label)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
var pti int
|
||||
if rows.Next() {
|
||||
if err := rows.Scan(&pti); 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
|
||||
}
|
||||
} else {
|
||||
// Insert new Property
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -107,14 +162,14 @@ func (r *ImportRepository) ImportPropertyTypes(importData []models.ImportPropert
|
||||
for _, propertyType := range importData {
|
||||
if propertyType.ID != 0 {
|
||||
// Update existing PropertyType
|
||||
query := "UPDATE property_types SET label = $1, description = $2, group_id = $3 WHERE id = $4"
|
||||
query := "UPDATE propertyTypes SET label = $1, description = $2, propertyGroupId = $3 WHERE id = $4"
|
||||
_, err := r.db.Exec(query, propertyType.Label, propertyType.Description, groupID, propertyType.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Insert new PropertyType
|
||||
query := "INSERT INTO property_types (label, description, group_id) VALUES ($1, $2, $3)"
|
||||
query := "INSERT INTO propertyTypes (label, description, propertyGroupId) VALUES ($1, $2, $3)"
|
||||
_, err := r.db.Exec(query, propertyType.Label, propertyType.Description, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user