Ejemplo n.º 1
0
func VehicleGetOne(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, _ := strconv.Atoi(vars["id"])

	queryVals := r.URL.Query()
	profile := getProfileFromQueryVals(queryVals)

	v := models.Vehicle{}
	eis := make([]models.EmissionsInfo, 0)
	query := fmt.Sprintf("SELECT * FROM vehicles WHERE epa_id = %s",
		global.Db.Dialect.Placeholder(1))
	err := global.Db.SelectOne(&v, query, id)
	checkErr(err, w)

	fp := getMostRecentFuelPrices()
	v.Fuels = models.CalculateFuelData(&v, profile, fp)

	query = fmt.Sprintf("SELECT * FROM emissions_info WHERE epa_id = %s",
		global.Db.Dialect.Placeholder(1))
	err = global.Db.SelectMany(&eis, query, id)
	checkErr(err, w)
	v.EmissionsInfo = eis

	js, err := json.Marshal(VehicleResponse{profile, v})
	checkErr(err, w)
	sendJSON(w, js)
}
Ejemplo n.º 2
0
func calculateFuelDataForAndCollectEpaIdsFromVehicles(vehicles *[]models.Vehicle,
	profile models.DrivingProfile, fp models.FuelPrices) (epaIdsQuery string, epaIds []interface{},
	epaIdToIdx map[int]int) {

	queryBuff := bytes.Buffer{}
	epaIdToIdx = make(map[int]int, 0)
	vs := *vehicles
	for i := 0; i < len(vs); i++ {
		v := &vs[i]
		epaIdToIdx[v.EpaID] = i
		epaIds = append(epaIds, v.EpaID)
		if i > 0 {
			queryBuff.WriteString(", ")
		}
		queryBuff.WriteString(global.Db.Dialect.Placeholder(i + 1))
		v.Fuels = models.CalculateFuelData(v, profile, fp)
	}
	return queryBuff.String(), epaIds, epaIdToIdx
}