Path: blob/main/docs/versioned_docs/version-v0.26/02-guide/04-blog/07-list.md
1007 views
List posts
In this chapter, you will develop a feature that enables users to retrieve all of the blog posts stored on your blockchain application. The feature will allow users to perform a query and receive a paginated response, which means that the output will be divided into smaller chunks or "pages" of data. This will allow users to more easily navigate and browse through the list of posts, as they will be able to view a specific number of posts at a time rather than having to scroll through a potentially lengthy list all at once.
List posts
Let's implement the ListPost
keeper method that will be called when a user makes a query to the blockchain application, requesting a paginated list of all the posts stored on chain.
ListPost
takes in two arguments: a context object and a request object of type QueryListPostRequest
. It returns a response object of type QueryListPostResponse
and an error.
The function first checks if the request object is nil
and returns an error with a InvalidArgument
code if it is. It then initializes an empty slice of Post
objects and unwraps the context object.
It retrieves a key-value store from the context using the storeKey
field of the keeper struct and creates a new store using a prefix of the PostKey
. It then calls the Paginate
function from the query
package on the store and the pagination information in the request object. The function passed as an argument to Paginate iterates over the key-value pairs in the store and unmarshals the values into Post
objects, which are then appended to the posts
slice.
If an error occurs during pagination, the function returns an Internal error
with the error message. Otherwise, it returns a QueryListPostResponse
object with the list of posts and pagination information.
Modify QueryListPostResponse
Add a repeated
keyword to return a list of posts and include the option [(gogoproto.nullable) = false]
to generate the field without a pointer.
Run the command to generate Go files from proto: