|
|
@@ -21,7 +21,7 @@ func NewImportRepository(db *sql.DB, productGroupRepo *ProductGroupRepository, p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *ImportRepository) ImportPropertyTypeGroups(importData []models.ImportPropertyTypeGroup) error {
|
|
|
|
func (r *ImportRepository) ImportPropertyTypeGroups(importData []models.ImportPropertyTypeGroup, parentGroupId int) error {
|
|
|
|
// Implement the logic to import PropertyTypeGroups into the database
|
|
|
|
// Implement the logic to import PropertyTypeGroups into the database
|
|
|
|
// This is a placeholder implementation; you need to write the actual logic
|
|
|
|
// This is a placeholder implementation; you need to write the actual logic
|
|
|
|
for _, data := range importData {
|
|
|
|
for _, data := range importData {
|
|
|
@@ -32,29 +32,70 @@ func (r *ImportRepository) ImportPropertyTypeGroups(importData []models.ImportPr
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// import property types if they exist
|
|
|
|
if err := r.ImportPropertyTypes(data.PropertyTypes, data.ID); err != nil {
|
|
|
|
if err := r.ImportPropertyTypes(data.PropertyTypes, data.ID); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// update subgroups if they exist
|
|
|
|
// update subgroups if they exist
|
|
|
|
if data.PropertyTypeGroups != nil {
|
|
|
|
if data.PropertyTypeGroups != nil {
|
|
|
|
return r.ImportPropertyTypeGroups(data.PropertyTypeGroups)
|
|
|
|
return r.ImportPropertyTypeGroups(data.PropertyTypeGroups, data.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} 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
|
|
|
|
// Insert new record
|
|
|
|
_, err := r.db.Exec("INSERT INTO propertytypegroups (label, description) VALUES ($1, $2)", data.Label, data.Description)
|
|
|
|
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 {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
existingID = newID
|
|
|
|
if data.PropertyTypeGroups != nil {
|
|
|
|
if data.PropertyTypeGroups != nil {
|
|
|
|
return r.ImportPropertyTypeGroups(data.PropertyTypeGroups)
|
|
|
|
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
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *ImportRepository) ImportProductGroups(importData []models.ImportProductGroup) error {
|
|
|
|
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 ProductGroups into the database
|
|
|
|
// Implement the logic to import PropertyTypeGroups into the database
|
|
|
|
// Implement the logic to import PropertyTypeGroups into the database
|
|
|
|
// This is a placeholder implementation; you need to write the actual logic
|
|
|
|
// This is a placeholder implementation; you need to write the actual logic
|
|
|
@@ -71,50 +112,50 @@ func (r *ImportRepository) ImportProductGroups(importData []models.ImportProduct
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// update subgroups if they exist
|
|
|
|
// update subgroups if they exist
|
|
|
|
if data.SubGroups != nil {
|
|
|
|
if data.ProductGroups != nil {
|
|
|
|
return r.ImportProductGroups(data.SubGroups)
|
|
|
|
return r.ImportProductGroups(data.ProductGroups, data.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Insert new record
|
|
|
|
// Insert new record
|
|
|
|
_, err := r.db.Exec("INSERT INTO productgroups (label, description) VALUES ($1, $2)", data.Label, data.Description)
|
|
|
|
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 {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := r.ImportProducts(data.Products, data.ID); err != nil {
|
|
|
|
if err := r.ImportProducts(data.Products, data.ID); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if data.SubGroups != nil {
|
|
|
|
if data.ProductGroups != nil {
|
|
|
|
return r.ImportProductGroups(data.SubGroups)
|
|
|
|
return r.ImportProductGroups(data.ProductGroups, data.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *ImportRepository) ImportProducts(importData []models.ImportProduct, groupID int) error {
|
|
|
|
func (r *ImportRepository) ImportProducts(importData []models.ImportProduct, productGroupID int) error {
|
|
|
|
// Implement the logic to import Products into the database
|
|
|
|
// Implement the logic to import Products into the database
|
|
|
|
for _, product := range importData {
|
|
|
|
for _, product := range importData {
|
|
|
|
if product.ID != 0 {
|
|
|
|
if product.ID != 0 {
|
|
|
|
// Update existing Product
|
|
|
|
// Update existing Product
|
|
|
|
query := "UPDATE products SET label = $1, description = $2, productGroupId = $3 WHERE id = $4"
|
|
|
|
query := "UPDATE products SET externalId = $1, label = $2, description = $3, productGroupId = $4 WHERE id = $5"
|
|
|
|
_, err := r.db.Exec(query, product.Label, product.Description, groupID, product.ID)
|
|
|
|
_, err := r.db.Exec(query, product.ExternalID, product.Label, product.Description, productGroupID, product.ID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if product.Properties != nil {
|
|
|
|
if product.Properties != nil {
|
|
|
|
if err := r.ImportProperties(product.Properties, product.ID, groupID); err != nil {
|
|
|
|
if err := r.ImportProperties(product.Properties, product.ID, productGroupID); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Insert new Product
|
|
|
|
// Insert new Product
|
|
|
|
query := "INSERT INTO products (label, description, productGroupId) VALUES ($1, $2, $3) RETURNING id"
|
|
|
|
query := "INSERT INTO products (externalId, label, description, productGroupId) VALUES ($1, $2, $3, $4) RETURNING id"
|
|
|
|
err := r.db.QueryRow(query, product.Label, product.Description, groupID).Scan(&product.ID)
|
|
|
|
err := r.db.QueryRow(query, product.ExternalID, product.Label, product.Description, productGroupID).Scan(&product.ID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if product.Properties != nil {
|
|
|
|
if product.Properties != nil {
|
|
|
|
if err := r.ImportProperties(product.Properties, product.ID, groupID); err != nil {
|
|
|
|
if err := r.ImportProperties(product.Properties, product.ID, productGroupID); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -123,98 +164,89 @@ func (r *ImportRepository) ImportProducts(importData []models.ImportProduct, gro
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *ImportRepository) ImportProperties(importData []models.ImportProperty, productID int, groupID int) error {
|
|
|
|
func (r *ImportRepository) ImportProperties(importData []models.ImportProperty, productID int, productGroupID int) error {
|
|
|
|
for _, property := range importData {
|
|
|
|
for _, property := range importData {
|
|
|
|
propertyTypeID, err := r.findPropertyTypeID(property.Label)
|
|
|
|
var propertyType models.ImportPropertyType
|
|
|
|
|
|
|
|
var propertyIDs []int
|
|
|
|
|
|
|
|
propertyType, err := r.findPropertyType(property.Label)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if property.ID != 0 {
|
|
|
|
propertyID := property.ID
|
|
|
|
if err := r.updateProperty(property, property.ID, productID, productGroupID, propertyType); err != nil {
|
|
|
|
if propertyID != 0 {
|
|
|
|
|
|
|
|
if err := r.updateProperty(property, propertyID, productID, groupID, propertyTypeID); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
propertyID, err = r.insertProperty(property, productID, groupID, propertyTypeID)
|
|
|
|
propertyIDs, err = r.insertProperty(property, productID, productGroupID, propertyType)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := r.upsertValue(propertyID, property.Value); err != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, propertyId := range propertyIDs {
|
|
|
|
|
|
|
|
property.ID = propertyId
|
|
|
|
|
|
|
|
if err := r.upsertValue(property, property.Value, propertyType.ID); err != nil {
|
|
|
|
return err
|
|
|
|
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) {
|
|
|
|
func (r *ImportRepository) findPropertyType(label string) (models.ImportPropertyType, error) {
|
|
|
|
var propertyTypeID int
|
|
|
|
var propertyType models.ImportPropertyType
|
|
|
|
err := r.db.QueryRow("SELECT id FROM propertyTypes WHERE label = $1", label).Scan(&propertyTypeID)
|
|
|
|
err := r.db.QueryRow("SELECT id, label FROM propertyTypes WHERE label = $1", label).Scan(&propertyType.ID, &propertyType.Label)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return 0, nil
|
|
|
|
return models.ImportPropertyType{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
return models.ImportPropertyType{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return propertyTypeID, nil
|
|
|
|
return propertyType, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *ImportRepository) updateProperty(property models.ImportProperty, propertyID int, productID int, groupID int, propertyTypeID int) error {
|
|
|
|
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"
|
|
|
|
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)
|
|
|
|
_, err := r.db.Exec(query, property.Label, propertyType.Label, propertyType.ID, productID, productGroupID, propertyID)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *ImportRepository) insertProperty(property models.ImportProperty, productID int, groupID int, propertyTypeID int) (int, error) {
|
|
|
|
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"
|
|
|
|
query := "INSERT INTO properties (label, description, productId, propertyTypeId, productGroupId) VALUES ($1, $2, $3, $4, $5) RETURNING id"
|
|
|
|
var propertyID int
|
|
|
|
var propertyID int
|
|
|
|
err := r.db.QueryRow(query, property.Label, property.Label, productID, propertyTypeID, groupID).Scan(&propertyID)
|
|
|
|
var propertyID2 int
|
|
|
|
|
|
|
|
err := r.db.QueryRow(query, property.Label, propertyType.Label, productID, propertyType.ID, productGroupID).Scan(&propertyID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
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 propertyID, nil
|
|
|
|
return []int{propertyID, propertyID2}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *ImportRepository) upsertValue(propertyID int, value string) error {
|
|
|
|
func (r *ImportRepository) upsertValue(property models.ImportProperty, value string, propertyTypeId int) error {
|
|
|
|
var existingValueID int
|
|
|
|
var existingValueID int
|
|
|
|
err := r.db.QueryRow("SELECT id FROM values WHERE label = $1 AND propertyId = $2", value, propertyID).Scan(&existingValueID)
|
|
|
|
err := r.db.QueryRow("SELECT id FROM values WHERE label = $1 AND propertyId = $2", value, property.ID).Scan(&existingValueID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = r.db.Exec("INSERT INTO values (label, description, propertyId) VALUES ($1, $2, $3)", value, "", propertyID)
|
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, err = r.db.Exec("UPDATE values SET label = $1, description = $2 WHERE id = $3", value, "", existingValueID)
|
|
|
|
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, propertyGroupId = $3 WHERE id = $4"
|
|
|
|
|
|
|
|
_, err := r.db.Exec(query, propertyType.Label, propertyType.Description, groupID, propertyType.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Insert new PropertyType
|
|
|
|
_, err = r.db.Exec("UPDATE values SET label = $1, description = $2 WHERE id = $3", value, property.Label, existingValueID)
|
|
|
|
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
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|