diff --git a/controllers/ParseController.go b/controllers/ParseController.go index 55a0ef2..0239e64 100644 --- a/controllers/ParseController.go +++ b/controllers/ParseController.go @@ -45,7 +45,7 @@ func ParseLinkLCW(link string,w http.ResponseWriter) { } ///////////////////////////// - importRepo, err := repositories.ImporterInstance() + importRepo, err := repositories.ParseImporterInstannce() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -54,13 +54,13 @@ func ParseLinkLCW(link string,w http.ResponseWriter) { } //wait until initialization data is loaded to memory importRepo.ImportWGroup.Wait() - fmt.Println(product); + log.Println(product); - if err = importRepo.UpdateOrCreate(product).Error; err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - fmt.Println(err) - return - } + //if err = importRepo.UpdateOrCreate(product).Error; err != nil { + // http.Error(w, err.Error(), http.StatusInternalServerError) + // fmt.Println(err) + // return + //} w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]string{ "msg": "Link parsed successfully", diff --git a/repositories/ImportRepository.go b/repositories/ImportRepository.go index f49e496..fa4ca72 100644 --- a/repositories/ImportRepository.go +++ b/repositories/ImportRepository.go @@ -144,6 +144,87 @@ func ImporterInstance() (instance *Importer, err error) { return instance, nil } +func ParseImporterInstannce() (instance *Importer, err error) { + db, err := gorm.Open(mysql.Open(os.Getenv("database_url")), &gorm.Config{SkipDefaultTransaction: true}) + + if err != nil { + log.Println(err) + return nil, err + } + + instance = &Importer{baza: db} + instance.ImportWGroup.Add(3) + //load families to memory + go func() { + defer instance.ImportWGroup.Done() + instance.families, instance.Error = gm.GetFamilies(db) + }() + + //load attributes to memory + go func() { + defer instance.ImportWGroup.Done() + + if attributes, err := gm.GetAttributes(db); err != nil { + instance.Error = err + return + } else { + instance.AttributesMap = make(map[string]gm.Attribute, len(attributes)) + + for _, attribute := range attributes { + instance.AttributesMap[attribute.Code] = attribute + } + } + + if colorOptions, err := gm.GetAttrOptions(db, instance.AttributesMap["color"].ID); err != nil { + instance.Error = err + return + } else { + instance.ColorOptions = make(map[string]gm.AttributeOption, len(colorOptions)) + + for _, option := range colorOptions { + instance.ColorOptions[option.AdminName] = option + } + } + + if sexOPtions, err := gm.GetAttrOptions(db, instance.AttributesMap["cinsiyet"].ID); err != nil { + instance.Error = err + return + } else { + instance.SexOptions = make(map[string]gm.AttributeOption, len(sexOPtions)) + + for _, option := range sexOPtions { + instance.SexOptions[option.AdminName] = option + } + } + + }() + + //load sellers to memory + go func() { + defer instance.ImportWGroup.Done() + + var vendors, err = gm.GetSellers(db) + + if err != nil { + instance.Error = err + return + } + //init sellers map + instance.sellers = make(map[string]gm.MarketplaceSeller, len(vendors)) + + for _, vendor := range vendors { + instance.sellers[vendor.Url] = vendor + } + }() + + if instance.Error != nil { + log.Println(instance.Error) + return nil, instance.Error + } + + return instance, nil +} + func (importer *Importer) Start() (instance *Importer) { log.Println("Start Product delete")