Skip to content

Commit

Permalink
add create post event
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Jan 6, 2023
1 parent 6df92a8 commit 03df1c1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions testutil/constants.go
@@ -0,0 +1,7 @@
package testutil

const (
Alice = "cosmos1jmjfq0tplp9tmx4v9uemw72y4d2wa5nr3xn9d3"
Bob = "cosmos1xyxs3skf3f4jfqeuv89yyaqvjc6lffavxqhc8g"
Carol = "cosmos1e0w5t53nrq7p66fye6c8p0ynyhf6y24l4yuxd7"
)
9 changes: 9 additions & 0 deletions x/blog/keeper/msg_server_create_post.go
Expand Up @@ -33,6 +33,15 @@ func (k msgServer) CreatePost(goCtx context.Context, msg *types.MsgCreatePost) (
postCount.Count++
k.Keeper.SetPostCount(ctx, postCount)

ctx.EventManager().EmitEvent(
sdk.NewEvent(types.PostCreatedEventType,
sdk.NewAttribute(types.PostCreatedCreator, msg.Creator),
sdk.NewAttribute(types.PostCreatedPostIndex, newIndex),
sdk.NewAttribute(types.PostCreatedTitle, msg.Title),
sdk.NewAttribute(types.PostCreatedBody, msg.Body),
),
)

return &types.MsgCreatePostResponse{
PostIndex: newIndex,
}, nil
Expand Down
26 changes: 26 additions & 0 deletions x/blog/keeper/msg_server_create_post_test.go
@@ -1,6 +1,7 @@
package keeper_test

import (
"blog/testutil"
keepertest "blog/testutil/keeper"
"blog/x/blog"
"blog/x/blog/keeper"
Expand Down Expand Up @@ -49,3 +50,28 @@ func TestCreatePostBadBody(t *testing.T) {
require.Nil(t, createResponse)
require.EqualError(t, err, "index = 0: body is missing")
}

func TestCreatePostEmitted(t *testing.T) {
msgServer, _, context := setupMsgServerCreatePost(t)
_, err := msgServer.CreatePost(context, &types.MsgCreatePost{
Creator: testutil.Alice,
Title: "Test",
Body: "This is a test",
})
require.Nil(t, err)

ctx := sdk.UnwrapSDKContext(context)
require.NotNil(t, ctx)
events := sdk.StringifyEvents(ctx.EventManager().ABCIEvents())
require.Len(t, events, 1)
event := events[0]
require.EqualValues(t, sdk.StringEvent{
Type: "new-post-created",
Attributes: []sdk.Attribute{
{Key: "creator", Value: testutil.Alice},
{Key: "post-index", Value: "0"},
{Key: "title", Value: "Test"},
{Key: "body", Value: "This is a test"},
},
}, event)
}
9 changes: 9 additions & 0 deletions x/blog/types/keys.go
Expand Up @@ -21,3 +21,12 @@ func KeyPrefix(p string) []byte {
const (
PostCountKey = "PostCount/value/"
)

// CreatePost Events
const (
PostCreatedEventType = "new-post-created"
PostCreatedCreator = "creator"
PostCreatedPostIndex = "post-index"
PostCreatedTitle = "title"
PostCreatedBody = "body"
)

0 comments on commit 03df1c1

Please sign in to comment.