func (res *Resource) ApiFind(query *db.Query, r kit.Request) kit.Response { // If query is empty, query for all records. if query == nil { query = res.Q() } apiFindHook, ok := res.hooks.(ApiFindHook) if ok { return apiFindHook.ApiFind(res, query, r) } if alterQuery, ok := res.hooks.(ApiAlterQueryHook); ok { alterQuery.ApiAlterQuery(res, query, r) } result, err := res.Query(query) if err != nil { return kit.NewErrorResponse(err) } user := r.GetUser() if allowFind, ok := res.hooks.(AllowFindHook); ok { finalItems := make([]kit.Model, 0) for _, item := range result { if allowFind.AllowFind(res, item, user) { finalItems = append(finalItems, item) } } result = finalItems } response := &kit.AppResponse{ Data: result, } // If a limit was set, count the total number of results // and set count parameter in metadata. limit := query.GetLimit() if limit > 0 { query.Limit(0).Offset(0) count, err := res.backend.Count(query) if err != nil { return &kit.AppResponse{ Error: apperror.Wrap(err, "count_error", ""), } } response.SetMeta(map[string]interface{}{ "count": count, "total_pages": math.Ceil(float64(count) / float64(limit)), }) } if hook, ok := res.hooks.(ApiAfterFindHook); ok { if err := hook.ApiAfterFind(res, result, r, response); err != nil { return kit.NewErrorResponse(err) } } return response }