Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Feat: Add the ability to create a comment to the post #13

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
.vscode
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ The entire task should only take 2-3 hours, but you’re free to take it as far
With this take home task we would like to understand how you tackle tasks like the above. In order for us to easier understand what your reasoning for certain decisions is, please make sure to write good code comments and documentation and maintain a proper Git history with commit messages explaining each step.

Feel free to include a list of ideas on how to improve your final project under the assumption that you have unlimited time and resources to spend on it.

### CosmosSDK 0.45 Documentation
In official cosmos website does not have documentation for 0.44
Steps described for 0.45 will work for 0.44 also:
https://docs.cosmos.network/v0.45/run-node/keyring.html
14 changes: 14 additions & 0 deletions proto/blog/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "blog/v1/types.proto";
// Query defines the gRPC querier service.
service Query {
rpc AllPosts(QueryAllPostsRequest) returns (QueryAllPostsResponse);
rpc AllPostComments(QueryAllPostCommentsRequest) returns (QueryAllPostCommentsResponse);
}

message QueryAllPostsRequest {
Expand All @@ -20,3 +21,16 @@ message QueryAllPostsResponse {

cosmos.base.query.v1beta1.PageResponse pagination = 2;
}


message QueryAllPostCommentsRequest {
string postSlug = 1;

cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message QueryAllPostCommentsResponse {
repeated PostComment postComments = 1;

cosmos.base.query.v1beta1.PageRequest pagination = 2;
}
11 changes: 11 additions & 0 deletions proto/blog/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option go_package = "github.com/regen-network/bec/x/blog";
// Msg is the blog.v1 Msg service
service Msg {
rpc CreatePost(MsgCreatePost) returns (MsgCreatePostResponse);
rpc CreatePostComment(MsgCreatePostComment) returns (MsgCreatePostCommentResponse);
}

// MsgCreatePost is the Msg/CreatePost request type.
Expand All @@ -18,3 +19,13 @@ message MsgCreatePost {

// MsgCreatePostResponse is the Msg/CreatePost response type.
message MsgCreatePostResponse {}

// MsgCreatePostComment is the Msg/CreatePostCOmment request type.
message MsgCreatePostComment {
string slug = 1;
string author = 2;
string body = 3;
}

// MsgCreatePostCommentResponse is the Msg/CreatePostResponse response type.
message MsgCreatePostCommentResponse {}
8 changes: 8 additions & 0 deletions proto/blog/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ message Post {
string title = 3;
string body = 4;
}

message PostComment {
// slug is a short human-readable string used in commect for connecting with post.
// Used as unique.
string slug = 1;
string author = 2;
string body = 3;
}
43 changes: 41 additions & 2 deletions x/blog/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cli
import (
"fmt"

// "strings"

"github.com/regen-network/bec/x/blog"
"github.com/spf13/cobra"

Expand All @@ -24,6 +22,7 @@ func GetQueryCmd() *cobra.Command {
}

cmd.AddCommand(CmdAllPosts())
cmd.AddCommand(CmdAllPostComments())

return cmd
}
Expand Down Expand Up @@ -63,3 +62,43 @@ func CmdAllPosts() *cobra.Command {

return cmd
}

func CmdAllPostComments() *cobra.Command {
cmd := &cobra.Command{
Use: "list-comments [post_slug]",
Short: "list all comments by post",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
argsPostSlug := string(args[0])

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := blog.NewQueryClient(clientCtx)

params := &blog.QueryAllPostCommentsRequest{
Pagination: pageReq,
PostSlug: argsPostSlug,
}

res, err := queryClient.AllPostComments(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "blog")

return cmd
}
35 changes: 35 additions & 0 deletions x/blog/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func GetTxCmd() *cobra.Command {
}

cmd.AddCommand(CmdCreatePost())
cmd.AddCommand(CmdCreatePostComment())

return cmd
}
Expand Down Expand Up @@ -61,3 +62,37 @@ func CmdCreatePost() *cobra.Command {

return cmd
}

func CmdCreatePostComment() *cobra.Command {
cmd := &cobra.Command{
Use: "create-comment [author] [slug] [body]",
Short: "Creates a new comment for post",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Flags().Set(flags.FlagFrom, args[0])
if err != nil {
return err
}

argsSlug := string(args[1])
argsBody := string(args[2])

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := &blog.MsgCreatePostComment{
Author: clientCtx.GetFromAddress().String(),
Slug: argsSlug,
Body: argsBody,
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
3 changes: 2 additions & 1 deletion x/blog/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const (
ModuleName = "blog"
StoreKey = ModuleName

PostKey = "post"
PostKey = "post"
PostCommentKey = "post_comment"
)

func KeyPrefix(p string) []byte {
Expand Down
Loading