// Search all entities from the database
func (d *Default) Search(results interface{}, filter interface{}, sortParams *api.SortParameters, pagination *api.Pagination) error {
	session := d.session.Clone()
	defer session.Close()

	// Apply Filter
	if filter == nil {
		filter = bson.M{}
	}

	// Get total
	if pagination != nil {
		total, err := d.WhereCount(filter)
		if err != nil {
			return api.NewDatabaseError(d, err, "")
		}
		pagination.SetTotal(uint(total))
	}

	// Prepare the query
	query := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter)

	// Apply sorts
	if sortParams != nil {
		sort := ConvertSortParameters(*sortParams)
		if len(sort) > 0 {
			query = query.Sort(sort...)
		}
	}

	// Paginate
	if pagination != nil {
		query = query.Limit(int(pagination.PerPage)).Skip(int(pagination.Offset()))
	}

	err := query.All(results)
	if err != nil {
		return api.NewDatabaseError(d, err, "")
	}

	return err
}
// Search all entities in the database
func (d *Default) Search(results interface{}, filter interface{}, sortParams *api.SortParameters, pagination *api.Pagination) error {
	term := r.Table(d.table)

	// Filter
	if filter != nil {
		term = term.Filter(filter)
	}

	// Get total
	if pagination != nil {
		total, err := d.WhereCount(filter)
		if err != nil {
			return api.NewDatabaseError(d, err, "")
		}
		pagination.SetTotal(uint(total))
	}

	// Sort
	if sortParams != nil {
		term = term.OrderBy(ConvertSortParameters(*sortParams)...)
	}

	// Slice result
	if pagination != nil {
		term = term.Slice(pagination.Offset(), pagination.Offset()+pagination.PerPage)
	}

	// Run the query
	cursor, err := term.Run(d.session)
	if err != nil {
		return api.NewDatabaseError(d, err, "")
	}

	// Fetch cursor
	err = cursor.All(results)
	if err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return err
	}

	return nil
}
Exemple #3
0
// SetPaginator defines values of the JSONLD Collection according to pagination request.
func (j *CollectionResource) SetPaginator(r *http.Request, paginator *api.Pagination) {

	j.TotalItems = paginator.Count()

	if paginator.HasOtherPages() {
		j.Type = "PagedCollection"
		j.ItemPerPage = paginator.PerPage
		j.CurrentPage = paginator.Page
	}

	q := r.URL.Query()

	if paginator.HasOtherPages() {
		q.Set("page", fmt.Sprintf("%d", 1))
		r.URL.RawQuery = q.Encode()
		j.FirstPage = r.URL.String()
	}
	if paginator.HasPrev() {
		q.Set("page", fmt.Sprintf("%d", paginator.Page-1))
		r.URL.RawQuery = q.Encode()
		j.PreviousPage = r.URL.String()
	}
	if paginator.HasNext() {
		q.Set("page", fmt.Sprintf("%d", paginator.Page+1))
		r.URL.RawQuery = q.Encode()
		j.NextPage = r.URL.String()
	}
	if paginator.HasOtherPages() {
		q.Set("page", fmt.Sprintf("%d", paginator.NumPages()))
		r.URL.RawQuery = q.Encode()
		j.LastPage = r.URL.String()
	}

}
// WhereAndFetchLimit filters with multiple fields and then fills results with all found resources
func (d *Default) WhereAndFetchLimit(filter interface{}, paginator *api.Pagination, results interface{}) error {
	session := d.session.Clone()
	defer session.Close()

	err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter).Limit(int(paginator.PerPage)).Skip(int(paginator.Offset())).All(results)
	if err != nil {
		if err == mgo.ErrNotFound {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}