Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PE-6331): support paginated records #14

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ function arns.addRecord(name, record)
end
end

function arns.getSortedRecords(page, pageLimit, sortBy, sortDirection)
local records = arns.getRecords()
-- sort the reocrds map by the keys alphabeticcally
local sortedRecords = {}
for name, record in pairs(records) do
record.name = name
table.insert(sortedRecords, record)
end

-- sort the records by the named
table.sort(sortedRecords, function(recordA, recordB)
local nameAString = recordA[sortBy]
local nameBString = recordB[sortBy]
if sortDirection == "desc" then
nameAString, nameBString = nameBString, nameAString
end
return nameAString < nameBString
end)

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

function arns.extendLease(from, name, years, currentTimestamp)
local record = arns.getRecord(name)
-- throw error if invalid
Expand Down
9 changes: 9 additions & 0 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,15 @@ Handlers.add("addReservedName", utils.hasMatchingTag("Action", "AddReservedName"
end
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) })
end)

-- END UTILITY HANDLERS USED FOR MIGRATION

return process
9 changes: 9 additions & 0 deletions src/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ function utils.isInteger(value)
return value % 1 == 0
end

function utils.slice(tbl, first, last, step)
local sliced = {}

for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced + 1] = tbl[i]
end

return sliced
end
function utils.isValidArweaveAddress(address)
return #address == 43 and string.match(address, "^[%w-_]+$") ~= nil
end
Expand Down
Loading