ready link import fx 1

This commit is contained in:
merdan 2022-12-07 17:25:58 +05:00
parent b2649c95d9
commit fa8bba5bec
3 changed files with 35 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package controller package controller
import ( import (
"db_service/models"
helper "db_service/pkg" helper "db_service/pkg"
"db_service/repositories" "db_service/repositories"
"encoding/json" "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,
})
}

View File

@ -49,6 +49,7 @@ func main() {
//route.HandleFunc("/init-importer", controller.StartImport) //route.HandleFunc("/init-importer", controller.StartImport)
route.HandleFunc("/init-importer", controller.StartProductImport) route.HandleFunc("/init-importer", controller.StartProductImport)
route.HandleFunc("/parse-link", controller.ParseLink) route.HandleFunc("/parse-link", controller.ParseLink)
route.HandleFunc("/import-link", controller.ImportLink).Methods("POST", "GET")
err := http.ListenAndServe(os.Getenv("port"), route) err := http.ListenAndServe(os.Getenv("port"), route)
if err != nil { if err != nil {

View File

@ -20,7 +20,7 @@ func SendRequest(method string, endpoint string, values []byte, authKey string)
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
Timeout: ConnectMaxWaitTime, Timeout: ConnectMaxWaitTime,
}).DialContext, }).DialContext,
}, },gl
} }
ctx, cancel := context.WithTimeout(context.Background(), RequestMaxWaitTime) ctx, cancel := context.WithTimeout(context.Background(), RequestMaxWaitTime)