Skip to content

Commit

Permalink
Merge pull request #171 from BitCannaGlobal/Check-and-avoid-duplicate…
Browse files Browse the repository at this point in the history
…s-at-BitCannaID.Bcnaid

Check and avoid duplicates at Bitcannaid.bcnaid
  • Loading branch information
RaulBernal authored May 2, 2023
2 parents 4702c9d + 8365dad commit ee1771e
Show file tree
Hide file tree
Showing 11 changed files with 724 additions and 61 deletions.
8 changes: 4 additions & 4 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func (app App) RegisterUpgradeHandlers() {
panic(err)
}

app.wakeandbake46_8(upgradeInfo)
app.wakeandbake_test2(upgradeInfo)
app.wakeandbake46(upgradeInfo)
}

func (app *App) wakeandbake46_8(_ upgradetypes.Plan) {
planName := "wakeandbake46_8"
func (app *App) wakeandbake_test2(_ upgradetypes.Plan) {
planName := "wakeandbake_test2"
app.UpgradeKeeper.SetUpgradeHandler(planName, func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("No-Op, only for keep the consensus...")
ctx.Logger().Info("BCNA module upgrade... searching new consensus")
// no-op - only for consensus.
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})
Expand Down
62 changes: 62 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,53 @@ paths:
type: boolean
tags:
- Query
/BitCannaGlobal/bcna/bcna/bitcannaid/bcnaid/{bcnaid}:
get:
summary: Queries a Bitcannaid by bcnaid.
operationId: BitcannaglobalBcnaBcnaBitcannaidByBcnaid
responses:
'200':
description: A successful response.
schema:
type: object
properties:
Bitcannaid:
type: object
properties:
id:
type: string
format: uint64
bcnaid:
type: string
address:
type: string
creator:
type: string
default:
description: An unexpected error response.
schema:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
'@type':
type: string
additionalProperties: {}
parameters:
- name: bcnaid
in: path
required: true
type: string
tags:
- Query
/BitCannaGlobal/bcna/bcna/bitcannaid/{id}:
get:
summary: Queries a Bitcannaid by id.
Expand Down Expand Up @@ -45670,6 +45717,21 @@ definitions:
repeated Bar results = 1;
PageResponse page = 2;
}
bitcannaglobal.bcna.bcna.QueryGetBitcannaidByBcnaidResponse:
type: object
properties:
Bitcannaid:
type: object
properties:
id:
type: string
format: uint64
bcnaid:
type: string
address:
type: string
creator:
type: string
bitcannaglobal.bcna.bcna.QueryGetBitcannaidResponse:
type: object
properties:
Expand Down
15 changes: 15 additions & 0 deletions proto/bcna/bcna/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ service Query {
option (google.api.http).get = "/BitCannaGlobal/bcna/bcna/bitcannaid";
}

// Queries a Bitcannaid by bcnaid.
rpc BitcannaidByBcnaid(QueryGetBitcannaidByBcnaidRequest) returns (QueryGetBitcannaidByBcnaidResponse) {
option (google.api.http).get = "/BitCannaGlobal/bcna/bcna/bitcannaid/bcnaid/{bcnaid}";
}


// Queries a Supplychain by id.
rpc Supplychain(QueryGetSupplychainRequest) returns (QueryGetSupplychainResponse) {
option (google.api.http).get = "/BitCannaGlobal/bcna/bcna/supplychain/{id}";
Expand All @@ -47,6 +53,15 @@ message QueryParamsResponse {
Params params = 1 [(gogoproto.nullable) = false];
}

message QueryGetBitcannaidByBcnaidRequest {
string bcnaid = 1;
}

message QueryGetBitcannaidByBcnaidResponse {
Bitcannaid Bitcannaid = 1 [(gogoproto.nullable) = true];
}


message QueryGetBitcannaidRequest {
uint64 id = 1;
}
Expand Down
13 changes: 8 additions & 5 deletions x/bcna/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdListBitcannaid())
cmd.AddCommand(CmdShowBitcannaid())
cmd.AddCommand(CmdListSupplychain())
cmd.AddCommand(CmdShowSupplychain())
cmd.AddCommand(
CmdQueryParams(),
CmdListBitcannaid(),
CmdShowBitcannaid(),
CmdSearchBitcannaidByBcnaid(),
CmdListSupplychain(),
CmdShowSupplychain(),
)

return cmd
}
33 changes: 33 additions & 0 deletions x/bcna/client/cli/query_bitcannaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,36 @@ func CmdShowBitcannaid() *cobra.Command {

return cmd
}

func CmdSearchBitcannaidByBcnaid() *cobra.Command {
cmd := &cobra.Command{
Use: "search-bitcannaid [bcnaid]",
Short: "search a bitcannaid by its bcnaid",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

bcnaid := args[0]

params := &types.QueryGetBitcannaidByBcnaidRequest{
Bcnaid: bcnaid,
}

res, err := queryClient.BitcannaidByBcnaid(context.Background(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
35 changes: 35 additions & 0 deletions x/bcna/keeper/bitcannaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Check if the BitCannaID exist previously
func (k Keeper) HasBitcannaidWithBcnaid(ctx sdk.Context, bcnaid string) bool {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.BitcannaidKey))

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var bitcannaid types.Bitcannaid
k.cdc.MustUnmarshal(iterator.Value(), &bitcannaid)

if bitcannaid.Bcnaid == bcnaid {
return true
}
}

return false
}

func (k Keeper) GetBitcannaidByBcnaid(ctx sdk.Context, bcnaid string) (val *types.Bitcannaid, found bool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.BitcannaidKey))

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var bitcannaid types.Bitcannaid
k.cdc.MustUnmarshal(iterator.Value(), &bitcannaid)

if bitcannaid.Bcnaid == bcnaid {
return &bitcannaid, true
}
}

return nil, false
}

// GetBitcannaidCount get the total number of bitcannaid
func (k Keeper) GetBitcannaidCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
Expand Down
8 changes: 8 additions & 0 deletions x/bcna/keeper/msg_server_bitcannaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
func (k msgServer) CreateBitcannaid(goCtx context.Context, msg *types.MsgCreateBitcannaid) (*types.MsgCreateBitcannaidResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// Check if a BitCannaID with the same Bcnaid already exists
if k.HasBitcannaidWithBcnaid(ctx, msg.Bcnaid) {
return nil, sdkerrors.Wrapf(types.ErrDuplicateBitcannaid, "BitCannaID with Bcnaid %s already exists", msg.Bcnaid)
}
var bitcannaid = types.Bitcannaid{
Creator: msg.Creator,
Bcnaid: msg.Bcnaid,
Expand All @@ -31,6 +35,10 @@ func (k msgServer) CreateBitcannaid(goCtx context.Context, msg *types.MsgCreateB
func (k msgServer) UpdateBitcannaid(goCtx context.Context, msg *types.MsgUpdateBitcannaid) (*types.MsgUpdateBitcannaidResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// Check if a BitCannaID with the same Bcnaid already exists
if k.HasBitcannaidWithBcnaid(ctx, msg.Bcnaid) {
return nil, sdkerrors.Wrapf(types.ErrDuplicateBitcannaid, "BitCannaID with Bcnaid %s already exists", msg.Bcnaid)
}
var bitcannaid = types.Bitcannaid{
Creator: msg.Creator,
Id: msg.Id,
Expand Down
17 changes: 17 additions & 0 deletions x/bcna/keeper/query_bitcannaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,20 @@ func (k Keeper) Bitcannaid(c context.Context, req *types.QueryGetBitcannaidReque

return &types.QueryGetBitcannaidResponse{Bitcannaid: bitcannaid}, nil
}

func (k Keeper) BitcannaidByBcnaid(c context.Context, req *types.QueryGetBitcannaidByBcnaidRequest) (*types.QueryGetBitcannaidByBcnaidResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var bitcannaid *types.Bitcannaid
ctx := sdk.UnwrapSDKContext(c)

if found := k.HasBitcannaidWithBcnaid(ctx, req.Bcnaid); found {
bitcannaid, _ = k.GetBitcannaidByBcnaid(ctx, req.Bcnaid)
} else {
return nil, status.Error(codes.NotFound, "bitcannaid not found")
}

return &types.QueryGetBitcannaidByBcnaidResponse{Bitcannaid: bitcannaid}, nil
}
4 changes: 1 addition & 3 deletions x/bcna/types/errors.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package types

// DONTCOVER

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

// x/bcna module sentinel errors
var (
ErrSample = sdkerrors.Register(ModuleName, 1100, "sample error")
ErrDuplicateBitcannaid = sdkerrors.Register(ModuleName, 1101, "BitCannaID already exists")
)
Loading

0 comments on commit ee1771e

Please sign in to comment.