예제 #1
0
func (n *NoSqlVehicle) Update(collection string) error {
	session, err := mgo.DialWithInfo(database.AriesMongoConnectionString())
	if err != nil {
		return err
	}
	defer session.Close()

	update := make(map[string]interface{})
	if n.Year != "" {
		update["year"] = n.Year
	}
	if n.Make != "" {
		update["make"] = n.Make
	}
	if n.Model != "" {
		update["model"] = n.Model
	}
	if n.Style != "" {
		update["style"] = n.Style
	}
	if n.Make != "" {
		update["make"] = n.Make
	}
	if len(n.PartIdentifiers) > 0 {
		update["parts"] = n.PartIdentifiers
	}
	return session.DB(database.AriesMongoConnectionString().Database).C(collection).UpdateId(n.ID, update)
}
예제 #2
0
func (n *NoSqlVehicle) Create(collection string) error {
	session, err := mgo.DialWithInfo(database.AriesMongoConnectionString())
	if err != nil {
		return err
	}
	defer session.Close()
	return session.DB(database.AriesMongoConnectionString().Database).C(collection).Insert(n)
}
예제 #3
0
func GetAllCollectionApplications(collection string) ([]NoSqlVehicle, error) {
	var apps []NoSqlVehicle
	session, err := mgo.DialWithInfo(database.AriesMongoConnectionString())
	if err != nil {
		return apps, err
	}
	defer session.Close()

	err = session.DB(database.AriesMongoConnectionString().Database).C(collection).Find(bson.M{}).Sort("-year", "make", "model", "style").All(&apps)
	return apps, err
}
예제 #4
0
func FindVehiclesFromAllCategories(v NoSqlVehicle, dtx *apicontext.DataContext) (map[string]NoSqlLookup, error) {
	var l NoSqlLookup
	lookupMap := make(map[string]NoSqlLookup)

	session, err := mgo.DialWithInfo(database.AriesMongoConnectionString())
	if err != nil {
		return lookupMap, err
	}
	defer session.Close()

	//Get all collections
	cols, err := GetAriesVehicleCollections()
	if err != nil {
		return lookupMap, err
	}

	//from each category
	for _, col := range cols {

		c := session.DB(AriesDb).C(col)
		queryMap := make(map[string]interface{})
		//query base vehicle
		queryMap["year"] = strings.ToLower(v.Year)
		queryMap["make"] = strings.ToLower(v.Make)
		queryMap["model"] = strings.ToLower(v.Model)
		if (v.Style) != "" {
			queryMap["style"] = strings.ToLower(v.Style)
		} else {
			_, l.Styles, err = GetApps(v, col)
		}

		var ids []int
		c.Find(queryMap).Distinct("parts", &ids)
		//add parts
		var partsArray []Part
		for _, id := range ids {
			p := Part{
				ID: id,
			}
			err = p.Get(dtx)
			if err != nil {
				continue
			}
			l.Parts = append(l.Parts, p)
			partsArray = append(partsArray, p)
		}
		if len(partsArray) > 0 {
			var tmp = lookupMap[col]
			tmp.Parts = partsArray
			tmp.Styles = l.Styles
			lookupMap[col] = tmp
			partsArray = nil
		}
	}
	return lookupMap, err
}
예제 #5
0
func FindVehicles(v NoSqlVehicle, collection string, dtx *apicontext.DataContext) (l NoSqlLookup, err error) {

	l = NoSqlLookup{}

	stage, vals, err := GetApps(v, collection)
	if err != nil {
		return
	}

	if stage != "" {
		switch stage {
		case "year":
			l.Years = vals
		case "make":
			l.Makes = vals
		case "model":
			l.Models = vals
		case "style":
			l.Styles = vals
		}

		if stage != "style" || len(l.Styles) > 0 {
			return
		}
	}

	session, err := mgo.DialWithInfo(database.AriesMongoConnectionString())
	if err != nil {
		return
	}
	defer session.Close()

	c := session.DB(AriesDb).C(collection)
	queryMap := make(map[string]interface{})

	ids := make([]int, 0)
	queryMap["year"] = strings.ToLower(v.Year)
	queryMap["make"] = strings.ToLower(v.Make)
	queryMap["model"] = strings.ToLower(v.Model)
	queryMap["style"] = strings.ToLower(v.Style)

	c.Find(queryMap).Distinct("parts", &ids)

	//add parts
	for _, id := range ids {
		p := Part{ID: id}
		if err := p.Get(dtx); err != nil {
			continue
		}
		l.Parts = append(l.Parts, p)
	}

	return l, err
}
예제 #6
0
func GetApps(v NoSqlVehicle, collection string) (stage string, vals []string, err error) {

	if v.Year != "" && v.Make != "" && v.Model != "" && v.Style != "" {
		return
	}

	session, err := mgo.DialWithInfo(database.AriesMongoConnectionString())
	if err != nil {
		return
	}
	defer session.Close()

	c := session.DB(AriesDb).C(collection)

	queryMap := make(map[string]interface{})

	if v.Year != "" {
		queryMap["year"] = strings.ToLower(v.Year)
	} else {
		c.Find(queryMap).Distinct("year", &vals)
		sort.Sort(sort.Reverse(sort.StringSlice(vals)))
		stage = "year"
		return
	}

	if v.Make != "" {
		queryMap["make"] = strings.ToLower(v.Make)
	} else {
		c.Find(queryMap).Sort("make").Distinct("make", &vals)
		sort.Strings(vals)
		stage = "make"
		return
	}

	if v.Model != "" {
		queryMap["model"] = strings.ToLower(v.Model)
	} else {
		c.Find(queryMap).Sort("model").Distinct("model", &vals)
		sort.Strings(vals)
		stage = "model"
		return
	}

	c.Find(queryMap).Distinct("style", &vals)
	if len(vals) == 1 && vals[0] == "" {
		vals = []string{}
	}

	sort.Strings(vals)
	stage = "style"

	return
}
예제 #7
0
func FindApplications(collection string, skip, limit int) (Result, error) {
	initMap.Do(initMaps)

	if limit == 0 || limit > 100 {
		limit = 100
	}

	res := Result{
		Applications: make([]NoSqlVehicle, 0),
		Finishes:     make([]string, 0),
		Colors:       make([]string, 0),
	}

	var apps []NoSqlVehicle
	var err error

	session, err := mgo.DialWithInfo(database.AriesMongoConnectionString())
	if err != nil {
		return res, err
	}
	defer session.Close()

	c := session.DB(AriesDb).C(collection)

	err = c.Find(nil).Sort("make", "model", "style", "-year").Skip(skip).Limit(limit).All(&apps)

	existingFinishes := make(map[string]string, 0)
	existingColors := make(map[string]string, 0)
	for _, app := range apps {
		for _, p := range app.PartIdentifiers {
			if part, ok := partMap[p]; ok {
				app.Parts = append(app.Parts, part)

				_, ok := existingFinishes[part.Finish]
				if part.Finish != "" && !ok {
					res.Finishes = append(res.Finishes, part.Finish)
					existingFinishes[part.Finish] = part.Finish
				}
				_, ok = existingColors[part.Color]
				if part.Color != "" && !ok {
					res.Colors = append(res.Colors, part.Color)
					existingColors[part.Color] = part.Color
				}
			}
		}
		if len(app.Parts) > 0 {
			res.Applications = append(res.Applications, app)
		}
	}

	return res, err
}
예제 #8
0
func GetAriesVehicleCollections() ([]string, error) {
	session, err := mgo.DialWithInfo(database.AriesMongoConnectionString())
	if err != nil {
		return []string{}, err
	}
	defer session.Close()

	cols, err := session.DB(AriesDb).CollectionNames()
	if err != nil {
		return []string{}, err
	}

	validCols := make([]string, 0)
	for _, col := range cols {
		if !strings.Contains(col, "system") {
			validCols = append(validCols, col)
		}
	}

	return validCols, nil
}
예제 #9
0
	Part  string
}

type Application struct {
	Year  string `bson:"year"`
	Make  string `bson:"make"`
	Model string `bson:"model"`
	Style string `bson:"style"`
	Parts []int  `bson:"parts"`
}

var (
	VehicleApplications map[string]Application
	PartConversion      map[string]int
	Session             *mgo.Session
	inf                 = database.AriesMongoConnectionString()
)

func Import(f multipart.File, collectionName string) ([]error, []error, error) {
	var err error
	var conversionErrs []error
	var insertErrs []error
	VehicleApplications = make(map[string]Application)
	PartConversion = make(map[string]int)
	Session, err = mgo.DialWithInfo(inf)
	es, err := CaptureCsv(f)
	if err != nil {
		return conversionErrs, insertErrs, err
	}

	for _, e := range es {