Example #1
0
//listParse takes a collection and some query values and generates an iterator
//for the objects that should be returned on that page
func listParse(c *mgo.Collection, v url.Values) (*mgo.Iter, int, int) {
	//parse out sorting
	sort := map[string]int{}
	for key, _ := range v {
		val := v.Get(key)
		if len(key) > 5 && strings.HasPrefix(key, "sort_") {
			field := key[5:]
			switch strings.ToLower(val) {
			case "asc":
				sort[field] = 1
			case "desc":
				sort[field] = -1
			}
		}
	}
	//set up the query with the correct sort order
	query := c.Find(nil).Sort(sort)

	//pagination
	page, numpage := grabInt(v, "page", 0), grabInt(v, "numpage", 20)
	if page < 1 {
		page = 1
	}
	if numpage < 1 {
		numpage = 1
	}
	//pages are 1 indexed.
	query = query.Skip(numpage * (page - 1)).Limit(numpage)

	return query.Iter(), page, numpage
}
Example #2
0
func NewMongoStore(DBCollection *mgo.Collection, keyPairs ...[]byte) *MongoStore {
	index := mgo.Index{Unique: true, Key: []string{"sessionid"}}
	DBCollection.EnsureIndex(index)

	return &MongoStore{
		Codecs: securecookie.CodecsFromPairs(keyPairs...),
		Options: &sessions.Options{
			Path:   "/",
			MaxAge: 86400 * 30,
		},
		DBCollection: DBCollection,
	}
}
Example #3
0
func insert(data interface{}, c *mgo.Collection, w http.ResponseWriter) {
	err := c.Insert(data)
	if err != nil {
		writeError(w, 500, "Document insertion failed")
		return
	}

	res, err := json.Marshal(data)
	if err != nil {
		writeError(w, 500, "Error stringifying query result")
		return
	}

	writeJSON(w, 201, string(res))
}