Skip to content

Commit

Permalink
fix(handlers): update paginated record handler to be more durable
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Jul 10, 2024
1 parent 5d46ce4 commit fa53e1f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
25 changes: 19 additions & 6 deletions src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function arns.addRecord(name, record)
end
end

function arns.getSortedRecords(page, pageLimit, sortBy, sortDirection)
function arns.getSortedRecords(page, pageSize, sortBy, sortOrder)
local records = arns.getRecords()
-- sort the reocrds map by the keys alphabeticcally
local sortedRecords = {}
Expand All @@ -83,20 +83,33 @@ function arns.getSortedRecords(page, pageLimit, sortBy, sortDirection)
table.sort(sortedRecords, function(recordA, recordB)
local nameAString = recordA[sortBy]
local nameBString = recordB[sortBy]
if sortDirection == "desc" then

if not nameAString or not nameBString then
error(
"Invalid sort by field, not every item has field "
.. sortBy
.. " Comparing:"
.. recordA.name
.. " to "
.. recordB.name
)
end

if sortOrder == "desc" then
nameAString, nameBString = nameBString, nameAString
end
return nameAString < nameBString
end)

return {
records = utils.slice(sortedRecords, (page - 1) * pageLimit + 1, page * pageLimit),
items = utils.slice(sortedRecords, (page - 1) * pageSize + 1, page * pageSize),
page = page,
pageSize = pageSize,
totalItems = #sortedRecords,
totalPages = math.ceil(#sortedRecords / pageLimit),
totalPages = math.ceil(#sortedRecords / pageSize),
sortBy = sortBy,
sortDirection = sortDirection,
hasNextPage = page * pageLimit < #sortedRecords,
sortOrder = sortOrder,
hasNextPage = page * pageSize < #sortedRecords,
}
end

Expand Down
19 changes: 14 additions & 5 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1276,11 +1276,20 @@ end)

Handlers.add("paginatedRecords", utils.hasMatchingTag("Action", "Paginated-Records"), function(msg)
local page = tonumber(msg.Tags.Page) or 1
local pageLimit = tonumber(msg.Tags["Page-Size"]) or 10
local sortOrder = msg.Tags.SortOrder and string.lower(msg.Tags["Sort-Order"]) or "asc"
local sortBy = msg.Tags.SortBy and string.lower(msg.Tags["Sort-By"]) or "name"
local sortedRecords = arns.getSortedRecords(page, pageLimit, sortBy, sortOrder)
ao.send({ Target = msg.From, Data = json.encode(sortedRecords) })
local pageSize = tonumber(msg.Tags["Page-Size"]) or 10
local sortOrder = msg.Tags["Sort-Order"] and string.lower(msg.Tags["Sort-Order"]) or "asc"
local sortBy = msg.Tags["Sort-By"] and msg.Tags["Sort-By"] or "name"
local status, result = pcall(arns.getSortedRecords, page, pageSize, sortBy, sortOrder)
if not status then
ao.send({
Target = msg.From,
Action = "Invalid-Records-Notice",
Error = "Pagination-Error",
Data = json.encode(result),
})
else
ao.send({ Target = msg.From, Action = "Records-Notice", Data = json.encode(result) })
end
end)

-- END UTILITY HANDLERS USED FOR MIGRATION
Expand Down

0 comments on commit fa53e1f

Please sign in to comment.