From fa8bba5bec2f7ba55e4d8fd34a82eccf8cda980f Mon Sep 17 00:00:00 2001 From: merdan Date: Wed, 7 Dec 2022 17:25:58 +0500 Subject: [PATCH] ready link import fx 1 --- controllers/ParseController.go | 33 +++++++++++++++++++++++++++++++++ main.go | 1 + pkg/request.go | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/controllers/ParseController.go b/controllers/ParseController.go index 2df43ec..5b8daa6 100644 --- a/controllers/ParseController.go +++ b/controllers/ParseController.go @@ -1,6 +1,7 @@ package controller import ( + "db_service/models" helper "db_service/pkg" "db_service/repositories" "encoding/json" @@ -60,3 +61,35 @@ func ParseLink(w http.ResponseWriter, route *http.Request) { ///////////////////////////////////////// } + +func ImportLink(w http.ResponseWriter, route *http.Request) { + + var product models.Product + + err := json.NewDecoder(route.Body).Decode(&product) + + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + ///////////////////////////// + importRepo, err := repositories.ImporterInstance() + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + //wait until initialization data is loaded to memory + importRepo.ImportWGroup.Wait() + + if err = importRepo.UpdateOrCreate(product).Error; err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) + + json.NewEncoder(w).Encode(map[string]string{ + "msg": "Link parsed successfully", + "productGroupId": product.ProductGroupID, + }) +} diff --git a/main.go b/main.go index 57e821f..0747bd9 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,7 @@ func main() { //route.HandleFunc("/init-importer", controller.StartImport) route.HandleFunc("/init-importer", controller.StartProductImport) route.HandleFunc("/parse-link", controller.ParseLink) + route.HandleFunc("/import-link", controller.ImportLink).Methods("POST", "GET") err := http.ListenAndServe(os.Getenv("port"), route) if err != nil { diff --git a/pkg/request.go b/pkg/request.go index 43bf74f..385354c 100644 --- a/pkg/request.go +++ b/pkg/request.go @@ -20,7 +20,7 @@ func SendRequest(method string, endpoint string, values []byte, authKey string) DialContext: (&net.Dialer{ Timeout: ConnectMaxWaitTime, }).DialContext, - }, + },gl } ctx, cancel := context.WithTimeout(context.Background(), RequestMaxWaitTime)