/
msg_server_supplychain.go
78 lines (61 loc) · 2.1 KB
/
msg_server_supplychain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package keeper
import (
"context"
"fmt"
"github.com/BitCannaGlobal/bcna/x/bcna/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
func (k msgServer) CreateSupplychain(goCtx context.Context, msg *types.MsgCreateSupplychain) (*types.MsgCreateSupplychainResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
supplychain := types.Supplychain{
Creator: msg.Creator,
Product: msg.Product,
Info: msg.Info,
Supplyinfo: msg.Supplyinfo,
Supplyextra: msg.Supplyextra,
}
id := k.AppendSupplychain(
ctx,
supplychain,
)
return &types.MsgCreateSupplychainResponse{
Id: id,
}, nil
}
func (k msgServer) UpdateSupplychain(goCtx context.Context, msg *types.MsgUpdateSupplychain) (*types.MsgUpdateSupplychainResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
supplychain := types.Supplychain{
Creator: msg.Creator,
Id: msg.Id,
Product: msg.Product,
Info: msg.Info,
Supplyinfo: msg.Supplyinfo,
Supplyextra: msg.Supplyextra,
}
// Checks that the element exists
val, found := k.GetSupplychain(ctx, msg.Id)
if !found {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
}
// Checks if the msg creator is the same as the current owner
if msg.Creator != val.Creator {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
}
k.SetSupplychain(ctx, supplychain)
return &types.MsgUpdateSupplychainResponse{}, nil
}
func (k msgServer) DeleteSupplychain(goCtx context.Context, msg *types.MsgDeleteSupplychain) (*types.MsgDeleteSupplychainResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Checks that the element exists
val, found := k.GetSupplychain(ctx, msg.Id)
if !found {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
}
// Checks if the msg creator is the same as the current owner
if msg.Creator != val.Creator {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
}
k.RemoveSupplychain(ctx, msg.Id)
return &types.MsgDeleteSupplychainResponse{}, nil
}