Example #1
0
func Get(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var w webProperty_model.WebProperty
	var err error

	idStr := r.FormValue("id")
	if idStr == "" {
		idStr = params["id"]
	}

	if w.ID, err = strconv.Atoi(idStr); err != nil {
		apierror.GenerateError("Trouble getting web property ID", err, rw, r)
		return ""
	}

	if err = w.Get(dtx); err != nil {
		apierror.GenerateError("Trouble getting web property", err, rw, r)
		return ""
	}

	sort := r.FormValue("sort")
	direction := r.FormValue("direction")
	if sort != "" {
		if strings.ContainsAny(direction, "esc") {
			sortutil.DescByField(w, sort)
		} else {
			sortutil.AscByField(w, sort)
		}

	}
	return encoding.Must(enc.Encode(w))
}
Example #2
0
func GetPricesByPart(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string {
	var err error
	var ps customer.Prices
	var partID int

	id := r.FormValue("id")
	if id == "" {
		id = params["id"]
	}

	if partID, err = strconv.Atoi(id); err != nil {
		apierror.GenerateError("Trouble getting part ID", err, rw, r)
		return ""
	}

	if ps, err = customer.GetPricesByPart(partID); err != nil {
		apierror.GenerateError("Trouble getting prices by part", err, rw, r)
		return ""
	}

	sort := r.FormValue("sort")
	direction := r.FormValue("direction")
	if sort != "" {
		if strings.ContainsAny(direction, "esc") {
			sortutil.DescByField(ps, sort)
		} else {
			sortutil.AscByField(ps, sort)
		}
	}

	return encoding.Must(enc.Encode(ps))
}
Example #3
0
File: geo.go Project: ninnemana/API
func GetAllCountriesAndStates() (countries Countries, err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return
	}
	defer db.Close()

	stmt, err := db.Prepare(getAllCountriesAndStatesStmt)
	if err != nil {
		return
	}
	defer stmt.Close()

	rows, err := stmt.Query()
	if err != nil {
		return
	}

	countryMap := make(map[int]Country, 0)

	for rows.Next() {
		var c Country
		var s State

		err = rows.Scan(
			&c.Id,
			&c.Country,
			&c.Abbreviation,
			&s.Id,
			&s.State,
			&s.Abbreviation,
		)
		if err != nil {
			return
		}

		country, exists := countryMap[c.Id]

		if !exists {
			c.States = &States{s}
			countryMap[c.Id] = c
		} else {
			*country.States = append(*country.States, s)
		}
	}
	defer rows.Close()

	for _, c := range countryMap {
		countries = append(countries, c)
	}

	sortutil.AscByField(countries, "Id")
	return
}
Example #4
0
func GetAllCategories(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, dtx *apicontext.DataContext) string {
	cats, err := blog_model.GetAllCategories(dtx)
	if err != nil {
		apierror.GenerateError("Trouble getting blog categories", err, rw, r)
	}
	sort := r.FormValue("sort")
	direction := r.FormValue("direction")
	if sort != "" {
		if strings.ContainsAny(direction, "esc") {
			sortutil.DescByField(cats, sort)
		} else {
			sortutil.AscByField(cats, sort)
		}

	}
	return encoding.Must(enc.Encode(cats))
}
Example #5
0
func GetAll(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, dtx *apicontext.DataContext) string {
	props, err := webProperty_model.GetAll(dtx)
	if err != nil {
		apierror.GenerateError("Trouble getting all web properties", err, rw, r)
		return ""
	}
	sort := r.FormValue("sort")
	direction := r.FormValue("direction")
	if sort != "" {
		if strings.ContainsAny(direction, "esc") {
			sortutil.DescByField(props, sort)
		} else {
			sortutil.AscByField(props, sort)
		}
	}
	return encoding.Must(enc.Encode(props))
}
Example #6
0
func GetAll(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, dtx *apicontext.DataContext) string {
	var fs faq_model.Faqs
	var err error

	fs, err = faq_model.GetAll(dtx)
	if err != nil {
		apierror.GenerateError("Trouble getting all faqs", err, rw, r)
	}

	sort := r.FormValue("sort")
	direction := r.FormValue("direction")
	if sort != "" {
		if strings.ContainsAny(direction, "esc") {
			sortutil.DescByField(fs, sort)
		} else {
			sortutil.AscByField(fs, sort)
		}

	}

	return encoding.Must(enc.Encode(fs))
}
Example #7
0
File: geo.go Project: ninnemana/API
func GetAllCountries() (countries Countries, err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return
	}
	defer db.Close()

	stmt, err := db.Prepare(getAllCountriesStmt)
	if err != nil {
		return
	}
	defer stmt.Close()

	rows, err := stmt.Query()
	if err != nil {
		return
	}

	for rows.Next() {
		var c Country
		err = rows.Scan(
			&c.Id,
			&c.Country,
			&c.Abbreviation,
		)
		if err != nil {
			return
		}
		countries = append(countries, c)
	}
	defer rows.Close()

	sortutil.AscByField(countries, "Id")

	return
}
Example #8
0
func CategoryFilter(cat products.Category, specs *map[string][]string) ([]Options, error) {

	var filtered FilteredOptions

	attrChan := make(chan error)

	go func() {
		if results, err := filtered.categoryGroupAttributes(cat, specs); err == nil {
			filtered = append(filtered, results...)
		}
		attrChan <- nil
	}()

	select {
	case <-attrChan:

	case <-time.After(5 * time.Second):
		return FilteredOptions{}, nil
	}

	sortutil.AscByField(filtered, "Key")

	return filtered, nil
}
Example #9
0
func GetAllBusinessClasses(dtx *apicontext.DataContext) (classes BusinessClasses, err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return
	}
	defer db.Close()

	stmt, err := db.Prepare(getBusinessClassesStmt)
	if err != nil {
		return
	}
	defer stmt.Close()

	rows, err := stmt.Query(dtx.APIKey, dtx.BrandID, dtx.BrandID)
	if err != nil {
		return
	}
	var bc BusinessClass
	for rows.Next() {
		bc = BusinessClass{}
		err = rows.Scan(
			&bc.ID,
			&bc.Name,
			&bc.Sort,
			&bc.ShowOnWebsite,
		)
		if err != nil {
			return
		}
		classes = append(classes, bc)
	}
	defer rows.Close()

	sortutil.AscByField(classes, "Sort")
	return
}
Example #10
0
func (l *Lookup) LoadParts(ch chan []Part, page int, count int, dtx *apicontext.DataContext) {
	if count == 0 {
		count = 50
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		ch <- nil
		return
	}
	defer db.Close()

	stmt, err := db.Prepare(partMatcherStmt)
	if err != nil {
		ch <- nil
		return
	}
	defer stmt.Close()

	brands := make([]string, 0)
	for _, b := range dtx.BrandArray {
		brands = append(brands, strconv.Itoa(b))
	}

	rows, err := stmt.Query(l.Vehicle.Base.Year, l.Vehicle.Base.Make, l.Vehicle.Base.Model, l.Vehicle.Submodel, strings.Join(brands, ","))
	if err != nil || rows == nil {
		ch <- nil
		return
	}
	defer rows.Close()

	maps := make(map[int][]ConfigurationOption, 0)
	parts := make([]int, 0)

	// Compile configuration map from database results
	for rows.Next() {
		var part int
		var config_type *string
		var config_val *string
		if err := rows.Scan(&part, &config_type, &config_val); err != nil {
			continue
		}
		if part == 0 {
			continue
		}
		if config_type == nil || config_val == nil || *config_type == "" || *config_val == "" {
			parts = append(parts, part)
			continue
		}

		opt := ConfigurationOption{
			Type:    strings.TrimSpace(*config_type),
			Options: make([]string, 0),
		}

		if maps[part] == nil {
			maps[part] = make([]ConfigurationOption, 0)
			opt.Options = append(opt.Options, *config_val)
			maps[part] = append(maps[part], opt)
		} else {
			for i, conf := range maps[part] {
				if strings.ToLower(strings.TrimSpace(conf.Type)) == strings.ToLower(strings.TrimSpace(*config_type)) {
					maps[part][i].Options = append(maps[part][i].Options, *config_val)
					continue
				}
			}
		}
	}

	// index the qualified configurations
	confIndex := make(map[string]string, 0)
	for _, c := range l.Vehicle.Configurations {
		confIndex[strings.ToLower(strings.TrimSpace(c.Key))] = strings.TrimSpace(c.Value)
	}

	// run a comparison of the database results
	// against the qualified configurations,
	// storing part numbers that are fully matched.
	for part, configs := range maps {
		qualified := 0
		for _, config := range configs {
			if val := confIndex[strings.ToLower(strings.TrimSpace(config.Type))]; val != "" {

				for _, opt := range config.Options {
					if strings.ToLower(strings.TrimSpace(val)) == strings.ToLower(strings.TrimSpace(opt)) {
						qualified = qualified + 1
					}
				}
			}
		}

		if qualified == len(configs) {
			parts = append(parts, part)
		}
	}

	sort.Ints(parts)

	partCount := len(parts)
	pagedParts := parts
	if partCount > count {
		start := 0
		if page > 1 {
			start = count * (page - 1)
		}
		end := start + count
		if len(parts) <= end {
			pagedParts = parts[start:]
		} else {
			pagedParts = parts[start : start+count]
		}
	}

	l.Parts = make([]Part, 0)
	perChan := make(chan int)
	for i, p := range pagedParts {
		go func(j int, prt Part) {
			if err := prt.Get(dtx); err == nil && prt.ShortDesc != "" {
				l.Parts = append(l.Parts, prt)
			}
			perChan <- 1
		}(i, Part{ID: p})
	}

	for _, _ = range pagedParts {
		<-perChan
	}

	sortutil.AscByField(l.Parts, "ID")

	mod := math.Mod(float64(partCount), float64(count))
	totalPages := partCount / count
	if mod > 0 {
		totalPages++
	}
	if page == 0 {
		page = 1
	}

	l.Pagination = Pagination{
		TotalItems:    partCount,
		ReturnedCount: len(l.Parts),
		Page:          page,
		PerPage:       count,
		TotalPages:    totalPages,
	}

	ch <- nil
	return
}
Example #11
0
func (filtered FilteredOptions) categoryGroupAttributes(cat products.Category, specs *map[string][]string) (FilteredOptions, error) {

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return FilteredOptions{}, err
	}
	defer db.Close()

	idQry, err := db.Prepare(GetCategoryGroup)
	if err != nil {
		return FilteredOptions{}, err
	}
	defer idQry.Close()

	idRows, err := idQry.Query(cat.CategoryID)
	if err != nil {
		return FilteredOptions{}, err
	}
	defer idRows.Close()

	ids := make([]int, 0)
	// ids = append(ids, cat.ID) // don't forget the given category
	for idRows.Next() {
		var i *int
		err := idRows.Scan(&i)
		if err == nil && i != nil {
			ids = append(ids, *i)
		}
	}

	excludedAttributeTypes := getExcludedAttributeTypes()
	filterResults := make(map[string]Options, 0)
	attrCh := make(chan error)
	for _, id := range ids {
		go func(catID int) {
			if results, err := categoryAttributes(catID, excludedAttributeTypes); err == nil {
				for key, result := range results {
					filterResults[key] = result
				}
			}
			attrCh <- nil
		}(id)
	}

	priceCh := make(chan error)
	filterResults["Price"] = Options{
		Key:     "Price",
		Options: make([]Option, 0),
	}

	for _, id := range ids {
		go func(catID int) {
			if results, err := categoryPrices(catID); err == nil {
				opts := make([]Option, 0)
				for _, res := range results {
					opts = append(opts, res.Options...)
				}

				fr := filterResults["Price"]
				fr.Options = append(fr.Options, opts...)
				filterResults["Price"] = fr
			}
			priceCh <- nil
		}(id)
	}

	for _, _ = range ids {
		<-attrCh
		<-priceCh
	}
	close(attrCh)
	close(priceCh)

	for key, res := range filterResults {
		indexed := make(map[string]int, 0)
		opts := make(map[string]Option, 0)
		for i, opt := range res.Options {
			if _, ok := indexed[opt.Value]; ok {
				idxOpt := opts[opt.Value]
				idxOpt.Products = append(idxOpt.Products, opt.Products...)
				curOpt := opts[opt.Value]
				curOpt.Products = removeDuplicates(idxOpt.Products)
				opts[opt.Value] = curOpt
			} else {
				res.Options = append(res.Options, opt)
				if specs != nil {
					for k, vals := range *specs {
						if strings.ToLower(key) == strings.ToLower(k) {
							for _, val := range vals {
								if strings.ToLower(opt.Value) == strings.ToLower(val) {
									opt.Selected = true
								}
							}
							break
						}
					}
				}
				opts[opt.Value] = opt
				indexed[opt.Value] = i
			}
		}

		res.Options = make([]Option, 0)
		mapped := make(map[string]string, 0)
		for _, opt := range opts {
			if _, ok := mapped[opt.Value]; !ok {
				sort.Ints(opt.Products)
				res.Options = append(res.Options, opt)
				mapped[opt.Value] = opt.Value
			}
		}
		if len(res.Options) > 1 {
			sortutil.AscByField(res.Options, "Value")
		}
		if len(res.Options) > 0 {
			filtered = append(filtered, res)
		}
	}

	sortutil.AscByField(filtered, "Key")
	return filtered, nil
}