Skip to content

Commit

Permalink
docs: fix missing err check (#2835)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle authored Sep 19, 2022
1 parent 529407a commit f5fe088
Show file tree
Hide file tree
Showing 14 changed files with 311 additions and 306 deletions.
20 changes: 10 additions & 10 deletions docs/docs/guide/02-hello.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (k Keeper) Hello(c context.Context, req *types.QueryHelloRequest) (*types.Q
}
ctx := sdk.UnwrapSDKContext(goCtx)
_ = ctx
return &types.QueryHelloResponse{Text: "Hello, Ignite CLI!"}, nil // <--
return &types.QueryHelloResponse{Text: "Hello, Ignite CLI!"}, nil // <--
}
```

Expand All @@ -238,13 +238,13 @@ Make the required changes to the `x/hello/module.go` file.
1. Add `"context"` to the list of packages in the import statement.

```go
import (
// ...
import (
// ...

"context"
"context"

// ...
)
// ...
)
```

Do not save the file yet, you need to continue with these modifications.
Expand All @@ -254,9 +254,9 @@ Make the required changes to the `x/hello/module.go` file.
1. Register the query handlers:

```go
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
}
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
}
```

2. After the chain has been started, visit [http://localhost:1317/hello/hello/hello](http://localhost:1317/hello/hello/hello) and see your text displayed:
Expand All @@ -269,7 +269,7 @@ Make the required changes to the `x/hello/module.go` file.

The `query` command has also scaffolded `x/hello/client/cli/query_hello.go` that implements a CLI equivalent of the hello query and mounted this command in `x/hello/client/cli/query.go` . Run the following command and get the same JSON response:

```go
```bash
hellod q hello hello
```

Expand Down
190 changes: 95 additions & 95 deletions docs/docs/guide/03-blog/00-build-blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ message MsgCreatePostResponse {

Review the Cosmos SDK message type with proto `message`. The `MsgCreatePost` has three fields: creator, title, and body. Since the purpose of the `MsgCreatePost` message is to create new posts in the store, the only thing the message needs to return is an ID of a created post. The `CreatePost` rpc was already added to the `Msg` service:

```go
```protobuf
service Msg {
rpc CreatePost(MsgCreatePost) returns (MsgCreatePostResponse);
}
Expand All @@ -117,21 +117,21 @@ You need to do two things:

```go
func (k msgServer) CreatePost(goCtx context.Context, msg *types.MsgCreatePost) (*types.MsgCreatePostResponse, error) {
// Get the context
ctx := sdk.UnwrapSDKContext(goCtx)
// Get the context
ctx := sdk.UnwrapSDKContext(goCtx)

// Create variable of type Post
var post = types.Post{
Creator: msg.Creator,
Title: msg.Title,
Body: msg.Body,
}
// Create variable of type Post
var post = types.Post{
Creator: msg.Creator,
Title: msg.Title,
Body: msg.Body,
}

// Add a post to the store and get back the ID
id := k.AppendPost(ctx, post)
// Add a post to the store and get back the ID
id := k.AppendPost(ctx, post)

// Return the ID of the post
return &types.MsgCreatePostResponse{Id: id}, nil
// Return the ID of the post
return &types.MsgCreatePostResponse{Id: id}, nil
}
```

Expand Down Expand Up @@ -180,11 +180,11 @@ Then, add these prefixes to the `x/blog/types/keys.go` file in the `const` and a

```go
const (
// ...
// ...

// Keep track of the index of posts
PostKey = "Post/value/"
PostCountKey = "Post/count/"
// Keep track of the index of posts
PostKey = "Post/value/"
PostCountKey = "Post/count/"
)
```

Expand All @@ -203,12 +203,12 @@ In the `x/blog/keeper/post.go` file, draft the `AppendPost` function. You can ad
package keeper

import (
"encoding/binary"
"encoding/binary"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"

"blog/x/blog/types"
"blog/x/blog/types"
)

// func (k Keeper) AppendPost() uint64 {
Expand All @@ -223,70 +223,70 @@ First, implement `GetPostCount`:

```go
func (k Keeper) GetPostCount(ctx sdk.Context) uint64 {
// Get the store using storeKey (which is "blog") and PostCountKey (which is "Post/count/")
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.PostCountKey))
// Get the store using storeKey (which is "blog") and PostCountKey (which is "Post/count/")
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.PostCountKey))

// Convert the PostCountKey to bytes
byteKey := []byte(types.PostCountKey)
// Convert the PostCountKey to bytes
byteKey := []byte(types.PostCountKey)

// Get the value of the count
bz := store.Get(byteKey)
// Get the value of the count
bz := store.Get(byteKey)

// Return zero if the count value is not found (for example, it's the first post)
if bz == nil {
return 0
}
// Return zero if the count value is not found (for example, it's the first post)
if bz == nil {
return 0
}

// Convert the count into a uint64
return binary.BigEndian.Uint64(bz)
// Convert the count into a uint64
return binary.BigEndian.Uint64(bz)
}
```

Now that `GetPostCount` returns the correct number of posts in the store, implement `SetPostCount`:

```go
func (k Keeper) SetPostCount(ctx sdk.Context, count uint64) {
// Get the store using storeKey (which is "blog") and PostCountKey (which is "Post/count/")
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.PostCountKey))
// Get the store using storeKey (which is "blog") and PostCountKey (which is "Post/count/")
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.PostCountKey))

// Convert the PostCountKey to bytes
byteKey := []byte(types.PostCountKey)
// Convert the PostCountKey to bytes
byteKey := []byte(types.PostCountKey)

// Convert count from uint64 to string and get bytes
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
// Convert count from uint64 to string and get bytes
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)

// Set the value of Post/count/ to count
store.Set(byteKey, bz)
// Set the value of Post/count/ to count
store.Set(byteKey, bz)
}
```

Now that you have implemented functions for getting the number of posts and setting the post count, at the top of the same `x/blog/keeper/post.go` file, implement the logic behind the `AppendPost` function:

```go
func (k Keeper) AppendPost(ctx sdk.Context, post types.Post) uint64 {
// Get the current number of posts in the store
count := k.GetPostCount(ctx)
// Get the current number of posts in the store
count := k.GetPostCount(ctx)

// Assign an ID to the post based on the number of posts in the store
post.Id = count
// Assign an ID to the post based on the number of posts in the store
post.Id = count

// Get the store
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.PostKey))
// Get the store
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.PostKey))

// Convert the post ID into bytes
byteKey := make([]byte, 8)
binary.BigEndian.PutUint64(byteKey, post.Id)
// Convert the post ID into bytes
byteKey := make([]byte, 8)
binary.BigEndian.PutUint64(byteKey, post.Id)

// Marshal the post into bytes
appendedValue := k.cdc.MustMarshal(&post)
// Marshal the post into bytes
appendedValue := k.cdc.MustMarshal(&post)

// Insert the post bytes using post ID as a key
store.Set(byteKey, appendedValue)
// Insert the post bytes using post ID as a key
store.Set(byteKey, appendedValue)

// Update the post count
k.SetPostCount(ctx, count+1)
return count
// Update the post count
k.SetPostCount(ctx, count+1)
return count
}
```

Expand Down Expand Up @@ -331,7 +331,7 @@ message QueryPostsRequest {

3. Add pagination to the post response:

```go
```protobuf
message QueryPostsResponse {
// Returning a list of posts
repeated Post Post = 1;
Expand All @@ -347,54 +347,54 @@ To implement post querying logic in the `x/blog/keeper/grpc_query_posts.go` file
package keeper

import (
"context"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"blog/x/blog/types"
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"blog/x/blog/types"
)

func (k Keeper) Posts(c context.Context, req *types.QueryPostsRequest) (*types.QueryPostsResponse, error) {
// Throw an error if request is nil
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
// Throw an error if request is nil
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

// Define a variable that will store a list of posts
var posts []*types.Post
// Define a variable that will store a list of posts
var posts []*types.Post

// Get context with the information about the environment
ctx := sdk.UnwrapSDKContext(c)
// Get context with the information about the environment
ctx := sdk.UnwrapSDKContext(c)

// Get the key-value module store using the store key (in our case store key is "chain")
store := ctx.KVStore(k.storeKey)
// Get the key-value module store using the store key (in our case store key is "chain")
store := ctx.KVStore(k.storeKey)

// Get the part of the store that keeps posts (using post key, which is "Post-value-")
postStore := prefix.NewStore(store, []byte(types.PostKey))
// Get the part of the store that keeps posts (using post key, which is "Post-value-")
postStore := prefix.NewStore(store, []byte(types.PostKey))

// Paginate the posts store based on PageRequest
pageRes, err := query.Paginate(postStore, req.Pagination, func(key []byte, value []byte) error {
var post types.Post
if err := k.cdc.Unmarshal(value, &post); err != nil {
return err
}
// Paginate the posts store based on PageRequest
pageRes, err := query.Paginate(postStore, req.Pagination, func(key []byte, value []byte) error {
var post types.Post
if err := k.cdc.Unmarshal(value, &post); err != nil {
return err
}

posts = append(posts, &post)
posts = append(posts, &post)

return nil
})
return nil
})

// Throw an error if pagination failed
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
// Throw an error if pagination failed
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

// Return a struct containing a list of posts and pagination info
return &types.QueryPostsResponse{Post: posts, Pagination: pageRes}, nil
// Return a struct containing a list of posts and pagination info
return &types.QueryPostsResponse{Post: posts, Pagination: pageRes}, nil
}
```

Expand Down
Loading

0 comments on commit f5fe088

Please sign in to comment.