Skip to content

Commit

Permalink
Merge pull request #636 from Mashape/refactor/count
Browse files Browse the repository at this point in the history
Refactoring the count function in DAO and API
  • Loading branch information
thibaultcha committed Oct 16, 2015
2 parents 94747d8 + 15addbf commit d0edd94
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ function _M.paginated_set(self, dao_collection)
return app_helpers.yield_error(err)
end

local count, err = dao_collection:count_by_keys(self.params)
local total, err = dao_collection:count_by_keys(self.params)
if err then
return app_helpers.yield_error(err)
end

local next_url
if data.next_page then
-- Parse next URL, if there are no elements then don't append it
local next_total, err = dao_collection:count_by_keys(self.params, size, data.next_page)
local next_total, err = dao_collection:count_by_keys(self.params, data.next_page)
if err then
return app_helpers.yield_error(err)
end
Expand All @@ -80,7 +80,7 @@ function _M.paginated_set(self, dao_collection)
-- This check is required otherwise the response is going to be a
-- JSON Object and not a JSON array. The reason is because an empty Lua array `{}`
-- will not be translated as an empty array by cjson, but as an empty object.
local result = #data == 0 and "{\"data\":[],\"total\":0}" or {data=data, ["next"]=next_url, total=count}
local result = #data == 0 and "{\"data\":[],\"total\":0}" or {data=data, ["next"]=next_url, total=total}

return responses.send_HTTP_OK(result, type(result) ~= "table")
end
Expand Down
8 changes: 4 additions & 4 deletions kong/dao/cassandra/base_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,13 @@ end

-- Retrieve the number of rows from the given columns/value table.
-- @param `where_t` (Optional) columns/values table by which to count entities.
-- @param `paging_state` Start page from given offset. See lua-resty-cassandra's :execute() option.
-- @return `res`
-- @return `err`
-- @return `filtering` A boolean indicating if ALLOW FILTERING was needed by the query
function BaseDao:count_by_keys(where_t, page_size, paging_state)
local select_q, where_columns, filtering = query_builder.count(self._table, where_t, self._column_family_details)
local res, err = self:execute(select_q, where_columns, where_t, {
page_size = page_size,
function BaseDao:count_by_keys(where_t, paging_state)
local count_q, where_columns, filtering = query_builder.count(self._table, where_t, self._column_family_details)
local res, err = self:execute(count_q, where_columns, where_t, {
paging_state = paging_state
})

Expand Down

0 comments on commit d0edd94

Please sign in to comment.