예제 #1
0
// 创建一个分页结构体
func NewPagination(query *mgo.Query, url string, prePage int) *Pagination {
	p := Pagination{}
	p.query = query
	p.count, _ = query.Count()
	p.prePage = prePage
	return &p
}
예제 #2
0
func (self *Service) NameSearch(ctx *gin.Context) {
	c := self.db().C(models.Collection)

	query := ctx.Param("query")

	response := response{
		NameSearch: []models.Name{},
	}
	response.Offset, _ = strconv.Atoi(ctx.Query("offset"))
	response.Limit, _ = strconv.Atoi(ctx.Query("limit"))
	if response.Limit == 0 {
		response.Limit = 10
	}

	// For scans...
	c.EnsureIndex(mgo.Index{
		Key: []string{"totalCount"},
	})

	// For prefix scans...
	c.EnsureIndex(mgo.Index{
		Key: []string{"lowerName", "totalCount"},
	})

	var q *mgo.Query
	if query == "" {
		q = c.Find(nil)
	} else {
		q = c.Find(bson.M{
			"lowerName": bson.RegEx{
				Pattern: "^" + regexp.QuoteMeta(strings.ToLower(query)),
			},
		})
	}

	var err error

	if response.Total, err = q.Count(); err != nil {
		ctx.String(500, "%v", err)
		return
	}

	var name models.Name
	iter := q.Skip(response.Offset).Limit(response.Limit).Sort("-totalCount").Iter()
	for iter.Next(&name) {
		if name.RelatedNames == nil {
			name.RelatedNames = []string{}
		}
		response.NameSearch = append(response.NameSearch, name)
	}

	if err := iter.Close(); err != nil {
		ctx.String(500, "%v", err)
	}

	ctx.JSON(200, response)
}