/
subspaces.go
106 lines (84 loc) · 3.43 KB
/
subspaces.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/desmos-labs/desmos/v4/x/subspaces/types"
)
// SetSubspaceID sets the new subspace id to the store
func (k Keeper) SetSubspaceID(ctx sdk.Context, subspaceID uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.SubspaceIDKey, types.GetSubspaceIDBytes(subspaceID))
}
// GetSubspaceID gets the highest subspace id
func (k Keeper) GetSubspaceID(ctx sdk.Context) (subspaceID uint64, err error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.SubspaceIDKey)
if bz == nil {
return 0, sdkerrors.Wrap(types.ErrInvalidGenesis, "initial subspace ID hasn't been set")
}
subspaceID = types.GetSubspaceIDFromBytes(bz)
return subspaceID, nil
}
// --------------------------------------------------------------------------------------------------------------------
// SaveSubspace saves the given subspace inside the current context.
func (k Keeper) SaveSubspace(ctx sdk.Context, subspace types.Subspace) {
store := ctx.KVStore(k.storeKey)
// Store the subspace
store.Set(types.SubspaceStoreKey(subspace.ID), k.cdc.MustMarshal(&subspace))
// If the initial section id does not exist, create it now
if !k.HasNextSectionID(ctx, subspace.ID) {
k.SetNextSectionID(ctx, subspace.ID, 1)
}
// If the subspace does not have the default section, create it now
if !k.HasSection(ctx, subspace.ID, 0) {
k.SaveSection(ctx, types.DefaultSection(subspace.ID))
}
// If the initial group id does not exist, create it now
if !k.HasNextGroupID(ctx, subspace.ID) {
k.SetNextGroupID(ctx, subspace.ID, 1)
}
// If the subspace does not have the default group, create it now
if !k.HasUserGroup(ctx, subspace.ID, 0) {
k.SaveUserGroup(ctx, types.DefaultUserGroup(subspace.ID))
}
k.Logger(ctx).Info("subspace saved", "id", subspace.ID)
k.AfterSubspaceSaved(ctx, subspace.ID)
}
// HasSubspace tells whether the given subspace exists or not
func (k Keeper) HasSubspace(ctx sdk.Context, subspaceID uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.SubspaceStoreKey(subspaceID))
}
// GetSubspace returns the subspace associated with the given id.
// If there is no subspace associated with the given id the function will return an empty subspace and false.
func (k Keeper) GetSubspace(ctx sdk.Context, subspaceID uint64) (subspace types.Subspace, found bool) {
store := ctx.KVStore(k.storeKey)
key := types.SubspaceStoreKey(subspaceID)
if !store.Has(key) {
return types.Subspace{}, false
}
k.cdc.MustUnmarshal(store.Get(key), &subspace)
return subspace, true
}
// DeleteSubspace allows to delete the subspace with the given id
func (k Keeper) DeleteSubspace(ctx sdk.Context, subspaceID uint64) {
// Delete the subspace
store := ctx.KVStore(k.storeKey)
store.Delete(types.SubspaceStoreKey(subspaceID))
// Delete the section and group id
k.DeleteNextSectionID(ctx, subspaceID)
k.DeleteNextGroupID(ctx, subspaceID)
// Delete all sections
k.IterateSubspaceSections(ctx, subspaceID, func(section types.Section) (stop bool) {
k.DeleteSection(ctx, section.SubspaceID, section.ID)
return false
})
// Delete all user groups
k.IterateSubspaceUserGroups(ctx, subspaceID, func(group types.UserGroup) (stop bool) {
k.DeleteUserGroup(ctx, group.SubspaceID, group.ID)
return false
})
// Log the subspace deletion
k.Logger(ctx).Info("subspace deleted", "id", subspaceID)
k.AfterSubspaceDeleted(ctx, subspaceID)
}