/
class.go
67 lines (60 loc) · 1.94 KB
/
class.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/nft"
)
// SaveClass defines a method for creating a new nft class
func (k Keeper) SaveClass(ctx sdk.Context, class nft.Class) error {
if k.HasClass(ctx, class.Id) {
return sdkerrors.Wrap(nft.ErrClassExists, class.Id)
}
bz, err := k.cdc.Marshal(&class)
if err != nil {
return sdkerrors.Wrap(err, "Marshal nft.Class failed")
}
store := ctx.KVStore(k.storeKey)
store.Set(classStoreKey(class.Id), bz)
return nil
}
// UpdateClass defines a method for updating a exist nft class
func (k Keeper) UpdateClass(ctx sdk.Context, class nft.Class) error {
if !k.HasClass(ctx, class.Id) {
return sdkerrors.Wrap(nft.ErrClassNotExists, class.Id)
}
bz, err := k.cdc.Marshal(&class)
if err != nil {
return sdkerrors.Wrap(err, "Marshal nft.Class failed")
}
store := ctx.KVStore(k.storeKey)
store.Set(classStoreKey(class.Id), bz)
return nil
}
// GetClass defines a method for returning the class information of the specified id
func (k Keeper) GetClass(ctx sdk.Context, classID string) (nft.Class, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(classStoreKey(classID))
var class nft.Class
if len(bz) == 0 {
return class, false
}
k.cdc.MustUnmarshal(bz, &class)
return class, true
}
// GetClasses defines a method for returning all classes information
func (k Keeper) GetClasses(ctx sdk.Context) (classes []*nft.Class) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, ClassKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var class nft.Class
k.cdc.MustUnmarshal(iterator.Value(), &class)
classes = append(classes, &class)
}
return
}
// HasClass determines whether the specified classID exist
func (k Keeper) HasClass(ctx sdk.Context, classID string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(classStoreKey(classID))
}