Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Jan 14, 2025
1 parent 34e32c8 commit bc35785
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ pub trait PositiveExternalityApi<BlockHash, AccountId> {
at: Option<BlockHash>,
) -> RpcResult<Option<Vec<u64>>>;

#[method(name = "positiveexternality_paginateposts_latest")]
fn paginate_posts_by_address_latest(
&self,
user: AccountId,
page: u64,
page_size: u64,
at: Option<BlockHash>,
) -> RpcResult<Option<Vec<u64>>>;


}

Expand Down Expand Up @@ -265,5 +274,26 @@ where
Ok(res)
}

fn paginate_posts_by_address_latest(
&self,
user: AccountId,
page: u64,
page_size: u64,
at: Option<Block::Hash>,
) -> RpcResult<Option<Vec<u64>>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.paginate_posts_by_address_latest(at, user, page, page_size);

fn map_err(error: impl ToString, desc: &'static str) -> ErrorObjectOwned {
ErrorObject::owned(Error::RuntimeError.into(), desc, Some(error.to_string()))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ sp_api::decl_runtime_apis! {
fn selected_as_juror(user_to_calculate: AccountId, who: AccountId) -> bool;
fn post_by_address_length(user: AccountId) -> u64;
fn paginate_posts_by_address(user: AccountId, page: u64, page_size: u64) -> Option<Vec<u64>>;
fn paginate_posts_by_address_latest(user: AccountId, page: u64, page_size: u64) -> Option<Vec<u64>>;

}
}
19 changes: 19 additions & 0 deletions custom-pallets/positive-externality/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,23 @@ impl<T: Config> Pallet<T> {
let end = (start + page_size).min(all_posts.len() as u64);
Some(all_posts[start as usize..end as usize].to_vec())
}

pub fn paginate_posts_by_address_latest(
user: T::AccountId,
page: u64,
page_size: u64,
) -> Option<Vec<u64>> {
let mut all_posts = PostByAddresss::<T>::get(user);
all_posts.reverse();

let start = (page - 1) * page_size;

if start >= all_posts.len() as u64 {
// If start exceeds available posts, return None (no more pages).
return None;
}

let end = (start + page_size).min(all_posts.len() as u64);
Some(all_posts[start as usize..end as usize].to_vec())
}
}
4 changes: 4 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ impl_runtime_apis! {
PositiveExternality::paginate_posts_by_address(user, page, page_size)
}

fn paginate_posts_by_address_latest(user: AccountId, page: u64, page_size: u64) -> Option<Vec<u64>>{
PositiveExternality::paginate_posts_by_address_latest(user, page, page_size)
}

}

impl project_tips_runtime_api::ProjectTipsApi<Block, AccountId> for Runtime {
Expand Down

0 comments on commit bc35785

Please sign in to comment.