From bc35785f80f8124d321fcd50e81090a3f2b793fc Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Tue, 14 Jan 2025 18:23:37 +0530 Subject: [PATCH] update --- .../positive-externality-rpc/src/lib.rs | 30 +++++++++++++++++++ .../src/lib.rs | 2 ++ .../positive-externality/src/extras.rs | 19 ++++++++++++ runtime/src/lib.rs | 4 +++ 4 files changed, 55 insertions(+) diff --git a/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs b/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs index 69d0217..b2281ea 100644 --- a/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs +++ b/custom-pallets/positive-externality/positive-externality-rpc/src/lib.rs @@ -65,6 +65,15 @@ pub trait PositiveExternalityApi { at: Option, ) -> RpcResult>>; + #[method(name = "positiveexternality_paginateposts_latest")] + fn paginate_posts_by_address_latest( + &self, + user: AccountId, + page: u64, + page_size: u64, + at: Option, + ) -> RpcResult>>; + } @@ -265,5 +274,26 @@ where Ok(res) } + fn paginate_posts_by_address_latest( + &self, + user: AccountId, + page: u64, + page_size: u64, + at: Option, + ) -> RpcResult>> { + 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) + } + } diff --git a/custom-pallets/positive-externality/positive-externality-runtime-api/src/lib.rs b/custom-pallets/positive-externality/positive-externality-runtime-api/src/lib.rs index 72f48ce..4c3fe54 100644 --- a/custom-pallets/positive-externality/positive-externality-runtime-api/src/lib.rs +++ b/custom-pallets/positive-externality/positive-externality-runtime-api/src/lib.rs @@ -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>; + fn paginate_posts_by_address_latest(user: AccountId, page: u64, page_size: u64) -> Option>; + } } diff --git a/custom-pallets/positive-externality/src/extras.rs b/custom-pallets/positive-externality/src/extras.rs index a41103a..38003ef 100644 --- a/custom-pallets/positive-externality/src/extras.rs +++ b/custom-pallets/positive-externality/src/extras.rs @@ -189,4 +189,23 @@ impl Pallet { 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> { + let mut all_posts = PostByAddresss::::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()) + } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index dfd7b61..0e9b15d 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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>{ + PositiveExternality::paginate_posts_by_address_latest(user, page, page_size) + } + } impl project_tips_runtime_api::ProjectTipsApi for Runtime {