Files
simply_cpq/buildtime/server/repositories/importRepository.go
T
2026-08-21 23:24:38 +02:00

221 lines
7.0 KiB
Go

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) 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
}
//
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)
}
} else {
// Insert new record
_, err := r.db.Exec("INSERT INTO propertytypegroups (label, description) VALUES ($1, $2)", data.Label, data.Description)
if err != nil {
return err
}
if data.PropertyTypeGroups != nil {
return r.ImportPropertyTypeGroups(data.PropertyTypeGroups)
}
}
}
return nil
}
func (r *ImportRepository) ImportProductGroups(importData []models.ImportProductGroup) 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.SubGroups != nil {
return r.ImportProductGroups(data.SubGroups)
}
} else {
// Insert new record
_, err := r.db.Exec("INSERT INTO productgroups (label, description) VALUES ($1, $2)", data.Label, data.Description)
if err != nil {
return err
}
if err := r.ImportProducts(data.Products, data.ID); err != nil {
return err
}
if data.SubGroups != nil {
return r.ImportProductGroups(data.SubGroups)
}
}
}
return nil
}
func (r *ImportRepository) ImportProducts(importData []models.ImportProduct, groupID 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 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, productID int, groupID int) error {
for _, property := range importData {
propertyTypeID, err := r.findPropertyTypeID(property.Label)
if err != nil {
return err
}
propertyID := property.ID
if propertyID != 0 {
if err := r.updateProperty(property, propertyID, productID, groupID, propertyTypeID); err != nil {
return err
}
} else {
propertyID, err = r.insertProperty(property, productID, groupID, propertyTypeID)
if err != nil {
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
}
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 {
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 {
// Insert new PropertyType
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 nil
}