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

Added nft pagination #68

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions candid/nft.did
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ service : {

// Canister ethods
"get_all" : () -> (vec nft_canister) query;
"get_all_paginated" : (offset: opt nat64, limit: opt nat64) -> (variant { Ok: vec nft_canister; Err: operation_error });

"add_admin" : (principal) -> (operation_response);
}
7 changes: 7 additions & 0 deletions registries/nft/src/common_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ pub struct NftCanister {
pub details: Vec<(String, DetailValue)>,
}

#[derive(CandidType, Clone, Debug, PartialEq)]
pub struct GetAllPaginatedResponse {
pub amount: usize,
pub nfts: Vec<&'static NftCanister>,
}

#[derive(CandidType, Debug, PartialEq, Deserialize, Clone)]
pub enum OperationError {
NotAuthorized,
Expand All @@ -43,3 +49,4 @@ pub enum RegistryResponse {
}

pub const CANISTER_REGISTRY_ID: &'static str = "curr3-vaaaa-aaaah-abbdq-cai";
pub const DEFAULT_LIMIT: usize = 20;
33 changes: 33 additions & 0 deletions registries/nft/src/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ impl Registry {
pub fn get_all(&self) -> Vec<&NftCanister> {
self.0.values().collect()
}

pub fn get_all_paginated(&self, offset: usize, _limit: usize) -> Result<Vec<&NftCanister>, OperationError> {

let nfts: Vec<&NftCanister> = self.0.values().collect();

if offset > nfts.len() {
return Err(OperationError::BadParameters);
}

let mut limit = _limit;

if offset + _limit > nfts.len() {
limit = nfts.len() - offset;
}

return Ok(nfts[offset..(offset + limit)].to_vec());
}

pub fn get_amount(&self) -> usize {
return self.0.values().len();
}
}

#[query]
Expand Down Expand Up @@ -116,3 +137,15 @@ pub fn get_all() -> Vec<&'static NftCanister> {
let db = ic::get_mut::<Registry>();
db.get_all()
}

#[query]
pub fn get_all_paginated(offset: Option<usize>, limit: Option<usize>) -> Result<GetAllPaginatedResponse>, OperationError> {
let db = ic::get_mut::<Registry>();
let nfts = db.get_all_paginated(offset.unwrap_or(0), limit.unwrap_or(DEFAULT_LIMIT))?;
let amount = db.get_amount();

return Ok(GetAllPaginatedResponse{
nfts,
amount,
});
}