mukan-ignite/docs/versioned_docs/version-v0.26/02-guide/04-blog/05-delete.md
Mukan Erkin Törük 26b204bd04
Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
feat: fork Ignite CLI v29 as Mukan Ignite — remove cosmos-sdk restrictions
2026-05-11 03:31:37 +03:00

2.8 KiB

Deleting posts

In this chapter, we will be focusing on the process of handling a "delete post" message.

Removing posts

func (k Keeper) RemovePost(ctx sdk.Context, id uint64) {
	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PostKey))
	store.Delete(GetPostIDBytes(id))
}

RemovePost function takes in two arguments: a context object ctx and an unsigned integer id. The function removes a post from a key-value store by deleting the key-value pair associated with the given id. The key-value store is accessed using the store variable, which is created by using the prefix package to create a new store using the context's key-value store and a prefix based on the PostKey constant. The Delete method is then called on the store object, using the GetPostIDBytes function to convert the id to a byte slice as the key to delete.

Deleting posts

package keeper

import (
	"context"
	"fmt"

	"blog/x/blog/types"

	sdk "github.com/cosmos/cosmos-sdk/types"
	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

func (k msgServer) DeletePost(goCtx context.Context, msg *types.MsgDeletePost) (*types.MsgDeletePostResponse, error) {
	ctx := sdk.UnwrapSDKContext(goCtx)
	val, found := k.GetPost(ctx, msg.Id)
	if !found {
		return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
	}
	if msg.Creator != val.Creator {
		return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
	}
	k.RemovePost(ctx, msg.Id)
	return &types.MsgDeletePostResponse{}, nil
}

DeletePost takes in two arguments: a context goCtx of type context.Context and a pointer to a message of type *types.MsgDeletePost. The function returns a pointer to a message of type *types.MsgDeletePostResponse and an error.

Inside the function, the context is unwrapped using the sdk.UnwrapSDKContext function and the value of the post with the ID specified in the message is retrieved using the GetPost function. If the post is not found, an error is returned using the sdkerrors.Wrap function. If the creator of the message does not match the creator of the post, another error is returned. If both of these checks pass, the RemovePost function is called with the context and the ID of the post to delete the post. Finally, the function returns a response message with no data and a nil error.

In short, DeletePost handles a request to delete a post, ensuring that the requester is the creator of the post before deleting it.

Summary

Congratulations on completing the implementation of the RemovePost and DeletePost methods in the keeper package! These methods provide functionality for removing a post from a store and handling a request to delete a post, respectively.