From 29ee783bcada8b56cef3b06a6ae9d6b26f7502f6 Mon Sep 17 00:00:00 2001 From: merdan Date: Sat, 3 Sep 2022 18:27:46 +0500 Subject: [PATCH] variant fix --- controllers/importer.go | 28 +++++++++++++++++++++++----- gorm_models/family.go | 21 ++++++--------------- gorm_models/product.go | 6 +----- gorm_models/vendor.go | 10 +++------- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/controllers/importer.go b/controllers/importer.go index 39a9667..a7087c0 100644 --- a/controllers/importer.go +++ b/controllers/importer.go @@ -45,20 +45,39 @@ func Start(w http.ResponseWriter, route *http.Request) { //get attribute families from mysql with gorutine go func() { defer famAndSellerWG.Done() - families = gm.GetFamilies(baza) + + families, err = gm.GetFamilies(baza) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } }() //get sellers families from mysql with gorutine go func() { defer famAndSellerWG.Done() - for _, vendor := range gm.GetSellers(baza) { + var vendors, err = gm.GetSellers(baza) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + for _, vendor := range vendors { sellers[vendor.Url] = vendor } }() go func() { defer famAndSellerWG.Done() - var attributes = gm.GetAttributes(baza) + var attributes, err = gm.GetAttributes(baza) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + for _, atrattribute := range attributes { attributesMap[atrattribute.Code] = atrattribute } @@ -216,7 +235,7 @@ func importProduct(product models.Product, db *gorm.DB) { iproduct.Images = append(iproduct.Images, gm.ProductImage{Type: "cdn", Path: element}) } - if product.ColorVariantCount > 0 { + if len(product.ColorVariants) > 0 { iproduct.SuperAttributes = append(iproduct.SuperAttributes, attributesMap["color"]) } @@ -418,7 +437,6 @@ func importProduct(product models.Product, db *gorm.DB) { } mainProductFlat.Variants = append(mainProductFlat.Variants, variantFlat) - } } diff --git a/gorm_models/family.go b/gorm_models/family.go index 57b0177..5e1b279 100644 --- a/gorm_models/family.go +++ b/gorm_models/family.go @@ -32,18 +32,12 @@ type AttributeGroup struct { Attributes []Attribute `gorm:"many2many:attribute_group_mappings;"` } -func GetFamilies(db *gorm.DB) []AttributeFamily { +func GetFamilies(db *gorm.DB) ([]AttributeFamily, error) { var families []AttributeFamily + err := db.Model(&AttributeFamily{}).Preload("Groups").Order("id ASC").Find(&families).Error - if err != nil { - log.Println(err.Error()) - - } else { - log.Println("famililar alyndy", len(families)) - } - - return families + return families, err } func GetGroups(db *gorm.DB) []AttributeGroup { @@ -57,15 +51,12 @@ func GetGroups(db *gorm.DB) []AttributeGroup { return groups } -func GetAttributes(db *gorm.DB) []Attribute { +func GetAttributes(db *gorm.DB) ([]Attribute, error) { var attributes []Attribute + err := db.Model(&Attribute{}).Find(&attributes).Error - if err != nil { - log.Println(err.Error()) - - } - return attributes + return attributes, err } func GetAttributeOption(db *gorm.DB, attrbuteID uint, value string) AttributeOption { diff --git a/gorm_models/product.go b/gorm_models/product.go index 9cb2d4b..6c1ffd9 100644 --- a/gorm_models/product.go +++ b/gorm_models/product.go @@ -91,12 +91,8 @@ func (ProductFlat) TableName() string { } func DeleteProducts(db *gorm.DB) error { - //todo delete non ordered & non favorited products - //select distinct ordered and favorited product ids //todo delete from elastico - //Delete from products where id in (select distinct) - //db.Exec("DELETE FROM products") - return db.Exec("DELETE FROM products").Error + return db.Exec("DELETE p.* FROM products p, order_items oi,wishlist f WHERE oi.product_id != p.id AND oi.parent_id != p.id AND f.product_id != p.id").Error } diff --git a/gorm_models/vendor.go b/gorm_models/vendor.go index 9be2129..f630f56 100644 --- a/gorm_models/vendor.go +++ b/gorm_models/vendor.go @@ -1,7 +1,6 @@ package gorm_models import ( - "log" "time" "gorm.io/gorm" @@ -28,13 +27,10 @@ type MarketplaceSeller struct { Url string } -func GetSellers(db *gorm.DB) []MarketplaceSeller { +func GetSellers(db *gorm.DB) ([]MarketplaceSeller, error) { var sellers []MarketplaceSeller + err := db.Model(&MarketplaceSeller{}).Find(&sellers).Error - if err != nil { - log.Println(err.Error()) - - } - return sellers + return sellers, err }