Skip to content

Commit

Permalink
Adapt to cosmos sdk v0.50
Browse files Browse the repository at this point in the history
  • Loading branch information
toschdev committed Nov 21, 2023
1 parent 74f3ec7 commit 08f9083
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions docs/docs/02-guide/04-blog/00-express.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (

"blog/x/blog/types"

"github.com/cosmos/cosmos-sdk/store/prefix"
"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -67,9 +67,7 @@ func (k Keeper) GetPostCount(ctx sdk.Context) uint64 {
if bz == nil {
return 0
}
return binary.BigEndi

an.Uint64(bz)
return binary.BigEndian.Uint64(bz)
}

func GetPostIDBytes(id uint64) []byte {
Expand All @@ -85,6 +83,29 @@ func (k Keeper) SetPostCount(ctx sdk.Context, count uint64) {
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}

func (k Keeper) GetPost(ctx sdk.Context, id uint64) (val types.Post, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PostKey))
b := store.Get(GetPostIDBytes(id))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
```

3. **Add Post key prefix:**

Add the `PostKey` and `PostCountKey` to the `x/blog/types/keys.go` file:

```go title="x/blog/types/keys.go"
// PostKey is used to uniquely identify posts within the system.
// It will be used as the beginning of the key for each post, followed bei their unique ID
PostKey = "Post/value/"

// This key will be used to keep track of the ID of the latest post added to the store.
PostCountKey = "Post/count/"
```

**Updating Posts**
Expand Down

0 comments on commit 08f9083

Please sign in to comment.