269 lines
9.1 KiB
Go
269 lines
9.1 KiB
Go
package repositories
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
"simplycpq/models"
|
|
)
|
|
|
|
type ImportRepository struct {
|
|
db *sql.DB
|
|
productGroupRepo *ProductGroupRepository
|
|
productRepo *ProductRepository
|
|
valueRepo *ValueRepository
|
|
}
|
|
|
|
func NewImportRepository(db *sql.DB, productGroupRepo *ProductGroupRepository, productRepo *ProductRepository, valueRepo *ValueRepository) *ImportRepository {
|
|
return &ImportRepository{
|
|
db: db,
|
|
productGroupRepo: productGroupRepo,
|
|
productRepo: productRepo,
|
|
valueRepo: valueRepo,
|
|
}
|
|
}
|
|
|
|
func (r *ImportRepository) ImportPropertyTypeGroups(importData []models.ImportPropertyTypeGroup, parentGroupId int) error {
|
|
// Implement the logic to import PropertyTypeGroups into the database
|
|
// This is a placeholder implementation; you need to write the actual logic
|
|
for _, data := range importData {
|
|
if data.ID != 0 {
|
|
// Update existing record
|
|
query := "UPDATE propertytypegroups SET label = $1, description = $2 WHERE id = $3"
|
|
_, err := r.db.Exec(query, data.Label, data.Description, data.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// import property types if they exist
|
|
if err := r.ImportPropertyTypes(data.PropertyTypes, data.ID); err != nil {
|
|
return err
|
|
}
|
|
// update subgroups if they exist
|
|
if data.PropertyTypeGroups != nil {
|
|
return r.ImportPropertyTypeGroups(data.PropertyTypeGroups, data.ID)
|
|
}
|
|
} else {
|
|
// check if propertyTypegroup with Label exists
|
|
var existingID int
|
|
err := r.db.QueryRow("SELECT id FROM propertytypegroups WHERE label = $1", data.Label).Scan(&existingID)
|
|
if err == sql.ErrNoRows {
|
|
// Insert new record
|
|
var newID int
|
|
err := r.db.QueryRow("INSERT INTO propertytypegroups (label, description, parentGroupId) VALUES ($1, $2, $3) RETURNING id", data.Label, data.Description, parentGroupId).Scan(&newID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
existingID = newID
|
|
if data.PropertyTypeGroups != nil {
|
|
return r.ImportPropertyTypeGroups(data.PropertyTypeGroups, newID)
|
|
}
|
|
} else if err != nil {
|
|
return err
|
|
} else {
|
|
// Existing record found, import subgroups
|
|
if data.PropertyTypeGroups != nil {
|
|
return r.ImportPropertyTypeGroups(data.PropertyTypeGroups, existingID)
|
|
}
|
|
}
|
|
// import property types if they exist
|
|
if err := r.ImportPropertyTypes(data.PropertyTypes, existingID); err != nil {
|
|
return err
|
|
}
|
|
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *ImportRepository) ImportPropertyTypes(importData []models.ImportPropertyType, groupID int) error {
|
|
for _, propertyType := range importData {
|
|
if propertyType.ID != 0 {
|
|
// Update existing PropertyType
|
|
query := "UPDATE propertyTypes SET label = $1, description = $2, propertyTypeGroupId = $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 propertyTypes (label, description, propertyTypeGroupId) VALUES ($1, $2, $3)"
|
|
_, err := r.db.Exec(query, propertyType.Label, propertyType.Description, groupID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Implement the ImportProducts method
|
|
func (r *ImportRepository) ImportProductGroups(importData []models.ImportProductGroup, parentProductGroupID int) error {
|
|
// check if ProductGroup allready exists
|
|
for _, productGroup := range importData {
|
|
if productGroup.ID != 0 {
|
|
// Update existing ProductGroup
|
|
query := "UPDATE productGroups SET label = $1, description = $2, parentGroupId = $3 WHERE id = $4"
|
|
_, err := r.db.Exec(query, productGroup.Label, productGroup.Description, parentProductGroupID, productGroup.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
// Insert new ProductGroup
|
|
err := r.db.QueryRow("INSERT INTO productGroups (label, description, parentGroupId) VALUES ($1, $2, $3) RETURNING id", productGroup.Label, productGroup.Description, parentProductGroupID).Scan(&productGroup.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Import Properties for the ProductGroup
|
|
for _, property := range productGroup.Properties {
|
|
property.ProductGroupID = productGroup.ID
|
|
property.ProductID = 0 // Clear ProductID for ProductGroup properties
|
|
// get PropertyTypeID by label
|
|
log.Printf("Getting PropertyTypeID for label: %s", property.Label)
|
|
ptid, err := r.valueRepo.GetPropertyTypeIDByLabel(property.Label)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Printf("Retrieved PropertyTypeID: %d for label: %s", ptid, property.Label)
|
|
property.PropertyTypeID = ptid // Set PropertyTypeID to the retrieved ID for ProductGroup properties
|
|
err = r.ImportProperty(property)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Import Products for the ProductGroup
|
|
for _, product := range productGroup.Products {
|
|
product.ProductGroupID = productGroup.ID // Set the ProductGroupID for the Product
|
|
err := r.ImportProduct(product)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Import sub ProductGroups recursively
|
|
if productGroup.ProductGroups != nil {
|
|
err := r.ImportProductGroups(productGroup.ProductGroups, productGroup.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *ImportRepository) ImportProduct(product models.ImportProduct) error {
|
|
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, product.ProductGroupID, product.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
// Insert new Product
|
|
err := r.db.QueryRow("INSERT INTO products (label, description, productGroupId) VALUES ($1, $2, $3) RETURNING id", product.Label, product.Description, product.ProductGroupID).Scan(&product.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Import Properties for the Product
|
|
for _, property := range product.Properties {
|
|
property.ProductID = product.ID // Set the ProductID for the Property
|
|
property.ProductGroupID = product.ProductGroupID // Clear ProductGroupID for Product properties
|
|
err := r.ImportProperty(property)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *ImportRepository) ImportProperty(property models.ImportProperty) error {
|
|
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.Label, property.PropertyTypeID, property.ProductID, property.ProductGroupID, property.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
// Insert new Property
|
|
query := "INSERT INTO properties (label, description, propertyTypeId, productId, productGroupId) VALUES ($1, $2, $3, $4, $5) RETURNING id"
|
|
err := r.db.QueryRow(query, property.Label, property.Label, property.PropertyTypeID, property.ProductID, property.ProductGroupID).Scan(&property.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// Import Property Value for the Property
|
|
if property.Value != "" {
|
|
err := r.ImportPropertyValue(property.Value, property)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err2 := r.ImportPropertyTypeValue(property.Value, property.PropertyTypeID, property.Label)
|
|
if err2 != nil {
|
|
return err2
|
|
}
|
|
err3 := r.ImportPropertyProductValue(property.Value, property.ProductID, property.Label)
|
|
if err3 != nil {
|
|
return err3
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *ImportRepository) ImportPropertyValue(value string, property models.ImportProperty) error {
|
|
// Check if a value already exists for the property
|
|
var existingValueID int
|
|
err := r.db.QueryRow("SELECT id FROM values WHERE label = $1 AND propertyId = $2", value, property.ID).Scan(&existingValueID)
|
|
if err == sql.ErrNoRows {
|
|
// Insert new Property Value
|
|
query := "INSERT INTO values (label, description, propertyId) VALUES ($1, $2, $3)"
|
|
_, err := r.db.Exec(query, value, value, property.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *ImportRepository) ImportPropertyTypeValue(value string, propertyTypeID int, description string) error {
|
|
// Check if a value already exists for the property
|
|
var existingValueID int
|
|
err := r.db.QueryRow("SELECT id FROM values WHERE label = $1 AND propertyId = $2", value, propertyTypeID).Scan(&existingValueID)
|
|
if err == sql.ErrNoRows {
|
|
// Insert new Property Value
|
|
query := "INSERT INTO values (label, description, propertyId) VALUES ($1, $2, $3)"
|
|
_, err := r.db.Exec(query, value, description, propertyTypeID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *ImportRepository) ImportPropertyProductValue(value string, productID int, description string) error {
|
|
// Check if a value already exists for the property
|
|
var existingValueID int
|
|
err := r.db.QueryRow("SELECT id FROM values WHERE label = $1 AND propertyId = $2", value, productID).Scan(&existingValueID)
|
|
if err == sql.ErrNoRows {
|
|
// Insert new Property Value
|
|
query := "INSERT INTO values (label, description, propertyId) VALUES ($1, $2, $3)"
|
|
_, err := r.db.Exec(query, value, description, productID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|