コード例 #1
0
ファイル: list.go プロジェクト: repos-go/admin
//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
}