diff --git a/buildtime/server/README.md b/buildtime/server/README.md index a239d55ac..bf00b1b22 100644 --- a/buildtime/server/README.md +++ b/buildtime/server/README.md @@ -113,7 +113,7 @@ CREATE TABLE IF NOT EXISTS values ( -- Property Table CREATE TABLE IF NOT EXISTS propertys ( id BIGINT PRIMARY KEY DEFAULT nextval('global_id_seq'), - Label TEXT, + label TEXT, description TEXT, propertyTypeId BIGINT, productId BIGINT, @@ -123,7 +123,7 @@ CREATE TABLE IF NOT EXISTS propertys ( -- PropertyType Table CREATE TABLE IF NOT EXISTS propertyTypes( id BIGINT PRIMARY KEY DEFAULT nextval('global_id_seq'), - Label TEXT, + label TEXT, description TEXT, propertyId BIGINT ); @@ -131,7 +131,7 @@ CREATE TABLE IF NOT EXISTS propertyTypes( -- PropertyTypeGroupTable CREATE TABLE IF NOT EXISTS propertyTypeGroups ( id BIGINT PRIMARY KEY DEFAULT nextval('global_id_seq'), - Label TEXT, + label TEXT, description TEXT, parentGroupId BIGINT ); @@ -140,7 +140,7 @@ CREATE TABLE IF NOT EXISTS propertyTypeGroups ( -- Product Table CREATE TABLE IF NOT EXISTS products ( id BIGINT PRIMARY KEY DEFAULT nextval('global_id_seq'), - Label TEXT, + abel TEXT, externalId TEXT, description TEXT, productGroupId BIGINT @@ -149,7 +149,7 @@ CREATE TABLE IF NOT EXISTS products ( -- ProductGroup Table CREATE TABLE IF NOT EXISTS productGroups ( id BIGINT PRIMARY KEY DEFAULT nextval('global_id_seq'), - Label TEXT, + label TEXT, description TEXT, parentGroupId BIGINT ); @@ -310,5 +310,43 @@ Wenn Daten zurückkommen, so wie hier im Beispiel, dann werden sie im *"data"* T "label": "Red", "description": "Color Red", "propertyId": 100003 +} +``` + + + +## Datenimport + +### Produktdaten: + +Produktdaten werden in folgenden JSON-Datenformat entgegen genommen: + +```json +// Excample Productgroups + +{ + "group": { + "label": "HIFI", + "decription": "Hifi components", + "products": [ + { + "label": "Radio 4711", + "externalId": "ABC0815" + "description": "modern Radio for Hifi", + "properties": { + "voltage": 240, + "Color": "red", + "watt": 15 + }, + }, + ] + + } +} + +// Example Productdata for new product import +{ + + } ``` \ No newline at end of file diff --git a/buildtime/server/repositories/importRepository.go b/buildtime/server/repositories/importRepository.go index 77e8c4678..70325a7b7 100644 --- a/buildtime/server/repositories/importRepository.go +++ b/buildtime/server/repositories/importRepository.go @@ -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