2022-09-07 12:55:55 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
2022-09-09 09:43:25 +00:00
|
|
|
gm "db_service/gorm_models"
|
2022-09-07 12:55:55 +00:00
|
|
|
helper "db_service/pkg"
|
|
|
|
|
"db_service/repositories"
|
|
|
|
|
"encoding/json"
|
2022-09-10 06:55:59 +00:00
|
|
|
"errors"
|
2022-09-07 12:55:55 +00:00
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ParseLink(w http.ResponseWriter, route *http.Request) {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
|
|
link := route.URL.Query().Get("url")
|
|
|
|
|
helper.Info("link: ", link)
|
|
|
|
|
|
|
|
|
|
linkParser := repositories.NewLinkParser(link)
|
|
|
|
|
|
|
|
|
|
//todo
|
|
|
|
|
product, err := linkParser.ParseLink()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
|
"msg": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonProduct, err := linkParser.GetProductDetailWithOptions(product.ID, product.ProductGroupID)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
helper.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
baza, err := gorm.Open(mysql.Open(os.Getenv("database_url")), &gorm.Config{})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
helper.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 09:43:25 +00:00
|
|
|
var productFlat gm.ProductFlat
|
|
|
|
|
|
|
|
|
|
err = baza.Preload("Variants").
|
|
|
|
|
//Preload("Product").
|
|
|
|
|
//Preload("Proudct.AttributeValues","attribute_id in(11,13)").
|
|
|
|
|
First(&productFlat, "sku = ?", jsonProduct.ProductGroupID).Error
|
|
|
|
|
|
2022-09-10 06:55:59 +00:00
|
|
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-09-09 09:43:25 +00:00
|
|
|
helper.Error(err)
|
|
|
|
|
err = ImportProduct(jsonProduct, baza)
|
2022-09-10 06:55:59 +00:00
|
|
|
} else if err == nil {
|
2022-09-09 09:43:25 +00:00
|
|
|
err = UpdateProduct(jsonProduct, baza, productFlat)
|
2022-09-15 11:50:32 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
|
"msg": "Link parsed successfully",
|
|
|
|
|
"productGroupId": strconv.Itoa(int(productFlat.ProductID)),
|
|
|
|
|
})
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
log.Printf("end parse took %s", elapsed)
|
|
|
|
|
return
|
2022-09-09 09:43:25 +00:00
|
|
|
}
|
2022-09-09 06:05:36 +00:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
helper.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-09-15 11:50:32 +00:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
log.Printf("end parse took %s", elapsed)
|
2022-09-07 12:55:55 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
|
"msg": "Link parsed successfully",
|
|
|
|
|
"productGroupId": strconv.Itoa(product.ProductGroupID),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|