From f2c67e33ecfa66ca437ccdafadc49f7fb905cdec Mon Sep 17 00:00:00 2001 From: merdan Date: Thu, 22 Sep 2022 20:10:51 +0500 Subject: [PATCH] * --- controllers/ImportController.go | 17 ++++++++--------- gorm_models/brand.go | 5 +++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/controllers/ImportController.go b/controllers/ImportController.go index 3727c77..69b8b26 100644 --- a/controllers/ImportController.go +++ b/controllers/ImportController.go @@ -51,7 +51,7 @@ func StartImport(w http.ResponseWriter, route *http.Request) { r := new(big.Int) fmt.Println("start import", r.Binomial(1000, 10)) - baza, err := gorm.Open(mysql.Open(os.Getenv("database_url")), &gorm.Config{}) + baza, err := gorm.Open(mysql.Open(os.Getenv("database_url")), &gorm.Config{SkipDefaultTransaction: true}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -256,13 +256,14 @@ func ImportProduct(product models.Product, db *gorm.DB) error { log.Println("ERR0001" + errCat.Error()) return errCat } else { - log.Println("Import" + product.Name) + log.Println(product.Name) } - var brand gm.Brand - - if product.Brand != "" { - brand = gm.FindOrCreateBrand(&tx, product.Brand, categories) + brand, err := gm.FindOrCreateBrand(&tx, product.Brand, categories) + if err != nil { + tx.Rollback() + log.Println(err) + return err } attributes, mainProductFlat := prepearAttributesWithFlat(&product) @@ -378,8 +379,6 @@ func ImportProduct(product models.Product, db *gorm.DB) error { return errProdVariant } - log.Println("Variant product saved") - sizeOption := gm.GetAttributeOption(&tx, AttributesMap["size"].ID, sizeVariant.AttributeValue) productNumber := fmt.Sprintf("%s-%d", colorVariant.ProductNumber, sizeVariant.ItemNumber) @@ -662,7 +661,7 @@ func ImportProduct(product models.Product, db *gorm.DB) error { return errSProduct } - return nil + return tx.Commit().Error } func createSellerProduct(flat *gm.ProductFlat, sellerURL string) gm.MarketplaceProduct { diff --git a/gorm_models/brand.go b/gorm_models/brand.go index b1fb7e2..7f03085 100644 --- a/gorm_models/brand.go +++ b/gorm_models/brand.go @@ -18,7 +18,7 @@ type Brand struct { Categories []Category `gorm:"many2many:category_brands;"` } -func FindOrCreateBrand(db *gorm.DB, brand string, categories []Category) Brand { +func FindOrCreateBrand(db *gorm.DB, brand string, categories []Category) (Brand, error) { var brandObject Brand @@ -31,11 +31,12 @@ func FindOrCreateBrand(db *gorm.DB, brand string, categories []Category) Brand { if err != nil { log.Println("ERR0000000000000001" + err.Error()) + return brandObject, err } } // var brandObject Brand - return brandObject + return brandObject, nil }