package repositories import ( "database/sql" "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 } func (r *ImportRepository) ImportProductGroups(importData []models.ImportProductGroup, parentGroupId int) error { // Implement the logic to import ProductGroups into the database // 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 productgroups SET label = $1, description = $2 WHERE id = $3" _, err := r.db.Exec(query, data.Label, data.Description, data.ID) if err != nil { return err } // if err := r.ImportProducts(data.Products, data.ID); err != nil { return err } // update subgroups if they exist if data.ProductGroups != nil { return r.ImportProductGroups(data.ProductGroups, data.ID) } } else { // Insert new record err := r.db.QueryRow("INSERT INTO productgroups (label, description, parentGroupId) VALUES ($1, $2, $3) RETURNING id", data.Label, data.Description, parentGroupId).Scan(&data.ID) if err != nil { return err } if err := r.ImportProducts(data.Products, data.ID); err != nil { return err } if data.ProductGroups != nil { return r.ImportProductGroups(data.ProductGroups, data.ID) } } } return nil } func (r *ImportRepository) ImportProducts(importData []models.ImportProduct, productGroupID int) error { // Implement the logic to import Products into the database for _, product := range importData { if product.ID != 0 { // Update existing Product query := "UPDATE products SET externalId = $1, label = $2, description = $3, productGroupId = $4 WHERE id = $5" _, err := r.db.Exec(query, product.ExternalID, product.Label, product.Description, productGroupID, product.ID) if err != nil { return err } if product.Properties != nil { if err := r.ImportProperties(product.Properties, product.ID, productGroupID); err != nil { return err } } } else { // Insert new Product query := "INSERT INTO products (externalId, label, description, productGroupId) VALUES ($1, $2, $3, $4) RETURNING id" err := r.db.QueryRow(query, product.ExternalID, product.Label, product.Description, productGroupID).Scan(&product.ID) if err != nil { return err } if product.Properties != nil { if err := r.ImportProperties(product.Properties, product.ID, productGroupID); err != nil { return err } } } } return nil } func (r *ImportRepository) ImportProperties(importData []models.ImportProperty, productID int, productGroupID int) error { for _, property := range importData { var propertyType models.ImportPropertyType var propertyIDs []int propertyType, err := r.findPropertyType(property.Label) if err != nil { return err } if property.ID != 0 { if err := r.updateProperty(property, property.ID, productID, productGroupID, propertyType); err != nil { return err } } else { propertyIDs, err = r.insertProperty(property, productID, productGroupID, propertyType) if err != nil { return err } } for _, propertyId := range propertyIDs { property.ID = propertyId if err := r.upsertValue(property, property.Value, propertyType.ID); err != nil { return err } } } return nil } func (r *ImportRepository) findPropertyType(label string) (models.ImportPropertyType, error) { var propertyType models.ImportPropertyType err := r.db.QueryRow("SELECT id, label FROM propertyTypes WHERE label = $1", label).Scan(&propertyType.ID, &propertyType.Label) if err != nil { if err == sql.ErrNoRows { return models.ImportPropertyType{}, nil } return models.ImportPropertyType{}, err } return propertyType, nil } func (r *ImportRepository) updateProperty(property models.ImportProperty, propertyID int, productID int, productGroupID int, propertyType models.ImportPropertyType) 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, propertyType.Label, propertyType.ID, productID, productGroupID, propertyID) return err } func (r *ImportRepository) insertProperty(property models.ImportProperty, productID int, productGroupID int, propertyType models.ImportPropertyType) ([]int, error) { query := "INSERT INTO properties (label, description, productId, propertyTypeId, productGroupId) VALUES ($1, $2, $3, $4, $5) RETURNING id" var propertyID int var propertyID2 int err := r.db.QueryRow(query, property.Label, propertyType.Label, productID, propertyType.ID, productGroupID).Scan(&propertyID) if err != nil { return nil, err } err2 := r.db.QueryRow(query, property.Label, propertyType.Label, 0, propertyType.ID, productGroupID).Scan(&propertyID2) if err2 != nil { return nil, err2 } return []int{propertyID, propertyID2}, nil } func (r *ImportRepository) upsertValue(property models.ImportProperty, value string, propertyTypeId int) error { var existingValueID int err := r.db.QueryRow("SELECT id FROM values WHERE label = $1 AND propertyId = $2", value, property.ID).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, property.Label, property.ID) // check if value exists for propertyType, if not insert it, if yes update it query3 := "SELECT id FROM values WHERE label = $1 AND propertyId = $2" _, err = r.db.Exec(query3, value, propertyTypeId) if err == sql.ErrNoRows { _, err = r.db.Exec("INSERT INTO values (label, description, propertyId) VALUES ($1, $2, $3)", value, property.Label, propertyTypeId) return err } return err } else { _, err = r.db.Exec("UPDATE values SET label = $1, description = $2 WHERE id = $3", value, property.Label, existingValueID) return err } }