Esempio n. 1
0
// Finds further configuration options and parts that match
// the given configuration. Doesn't start looking for parts
// until the model is provided.
func Query(w http.ResponseWriter, r *http.Request, enc encoding.Encoder, dtx *apicontext.DataContext) string {
	var l products.Lookup
	var page int
	var count int

	qs := r.URL.Query()

	page, _ = strconv.Atoi(qs.Get("page"))
	count, _ = strconv.Atoi(qs.Get("count"))
	qs.Del("page")
	qs.Del("count")

	l.Vehicle = LoadVehicle(r)

	l.Brands = dtx.BrandArray

	if qs.Get("key") != "" {
		l.CustomerKey = qs.Get("key")
	} else if r.FormValue("key") != "" {
		l.CustomerKey = r.FormValue("key")
		delete(r.Form, "key")
	} else {
		l.CustomerKey = r.Header.Get("key")
	}

	if l.Vehicle.Base.Year == 0 { // Get Years
		if err := l.GetYears(dtx); err != nil {
			apierror.GenerateError("Trouble getting years for vehicle lookup", err, w, r)
			return ""
		}
	} else if l.Vehicle.Base.Make == "" { // Get Makes
		if err := l.GetMakes(dtx); err != nil {
			apierror.GenerateError("Trouble getting makes for vehicle lookup", err, w, r)
			return ""
		}
	} else if l.Vehicle.Base.Model == "" { // Get Models
		if err := l.GetModels(); err != nil {
			apierror.GenerateError("Trouble getting models for vehicle lookup", err, w, r)
			return ""
		}
	} else {

		// Kick off part getter
		partChan := make(chan []products.Part)
		go l.LoadParts(partChan, page, count, dtx)

		if l.Vehicle.Submodel == "" { // Get Submodels
			if err := l.GetSubmodels(); err != nil {
				apierror.GenerateError("Trouble getting submodels for vehicle lookup", err, w, r)
				return ""
			}
		} else { // Get configurations
			if err := l.GetConfigurations(); err != nil {
				apierror.GenerateError("Trouble getting configurations for vehicle lookup", err, w, r)
				return ""
			}
		}

		select {
		case parts := <-partChan:
			if len(parts) > 0 {
				l.Parts = parts
				l.Filter, _ = apifilter.PartFilter(l.Parts, nil)
			}
		case <-time.After(5 * time.Second):

		}
	}

	return encoding.Must(enc.Encode(l))
}
Esempio n. 2
0
func (av *AcesVehicle) getCurtVehicles(configMap map[int]interface{}) (products.Lookup, error) { //get CURT vehicles
	var l products.Lookup
	var err error
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return l, err
	}
	defer db.Close()
	stmt, err := db.Prepare(getCurtVehiclesPreConfig)
	if err != nil {
		return l, err
	}
	defer stmt.Close()
	res, err := stmt.Query(av.AAIABaseVehicleID, av.AAIASubmodelID)
	if err != nil {
		return l, err
	}

	var sub, configKey, configValue *string
	var subID, configKeyID, configValueID, acesConfigValID *int
	var cv CurtVehicle

	// var pco products.ConfigurationOption
	var vehicleConfig products.Configuration

	// pcoMap := make(map[string][]string)

	for res.Next() {

		err = res.Scan(
			&cv.ID,
			&cv.BaseVehicle.ModelID,
			&cv.BaseVehicle.ModelName,
			&cv.BaseVehicle.MakeID,
			&cv.BaseVehicle.MakeName,
			&cv.BaseVehicle.YearID,
			&subID,
			&sub,
			&configKey,
			&configKeyID,
			&configValue,
			&configValueID,
			&acesConfigValID,
		)
		if subID != nil {
			cv.Submodel.ID = *subID
		}
		if sub != nil {
			cv.Submodel.Name = *sub
		}
		if configKey != nil {
			cv.Configuration.Type = *configKey
		}
		if configValue != nil {
			cv.Configuration.Value = *configValue
		}
		if configKeyID != nil {
			cv.Configuration.TypeID = *configKeyID
		}
		if configValueID != nil {
			cv.Configuration.ValueID = *configValueID
		}
		if acesConfigValID != nil {
			cv.Configuration.AcesValueID = *acesConfigValID
		}

		// log.Print(configMap)

		// //configs - assign to map, flag
		// configValFlag := true
		// if vs, ok := pcoMap[cv.Configuration.Type]; ok {
		// 	for _, v := range vs {
		// 		if v == cv.Configuration.Value {
		// 			configValFlag = false
		// 		}
		// 	}
		// }
		// if configValFlag == true {
		// 	if name, ok := configMap[cv.Configuration.TypeID]; ok {
		// 		//configMap contains this config type

		// 		if cv.Configuration.AcesValueID == name {
		// 			pcoMap[cv.Configuration.Type] = append(pcoMap[cv.Configuration.Type], cv.Configuration.Value)

		// 			//vehicleConfigs (not l.ConfugurationOption)
		// 			vehicleConfig.Key = cv.Configuration.Type
		// 			vehicleConfig.Value = cv.Configuration.Value
		// 			l.Vehicle.Configurations = append(l.Vehicle.Configurations, vehicleConfig)
		// 		}
		// 	} else {
		// 		pcoMap[cv.Configuration.Type] = append(pcoMap[cv.Configuration.Type], cv.Configuration.Value)

		// 		//vehicleConfigs (not l.ConfugurationOption)
		// 		vehicleConfig.Key = cv.Configuration.Type
		// 		vehicleConfig.Value = cv.Configuration.Value
		// 		l.Vehicle.Configurations = append(l.Vehicle.Configurations, vehicleConfig)
		// 	}

		// }

		l.Vehicle.Base.Make = cv.BaseVehicle.MakeName
		l.Vehicle.Base.Model = cv.BaseVehicle.ModelName
		l.Vehicle.Base.Year = cv.BaseVehicle.YearID
		l.Vehicle.Submodel = cv.Submodel.Name

	} //end scan loop
	defer res.Close()

	//assign configs
	// for key, val := range pcoMap {
	// 	pco.Type = key
	// 	pco.Options = val
	// 	l.Configurations = append(l.Configurations, pco)
	// }

	//NEW
	curtConfigMap, err := getCurtConfigMapFromAcesConfigMap(configMap)
	if err != nil {
		return l, err
	}
	for configType, config := range curtConfigMap {
		vehicleConfig.Key = configType
		vehicleConfig.Value = config
		l.Vehicle.Configurations = append(l.Vehicle.Configurations, vehicleConfig)
	}

	l.Makes = append(l.Makes, l.Vehicle.Base.Make)
	l.Models = append(l.Models, l.Vehicle.Base.Model)
	l.Years = append(l.Years, l.Vehicle.Base.Year)
	l.Submodels = append(l.Submodels, l.Vehicle.Submodel)

	return l, err
}