/
keeper.go
153 lines (117 loc) · 3.69 KB
/
keeper.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package magpie
import (
"time"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/bank"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Keeper maintains the link to data storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
coinKeeper bank.Keeper
storeKey sdk.StoreKey // Unexposed key to access store from sdk.Context
cdc *codec.Codec // The wire codec for binary encoding/decoding.
}
// NewKeeper creates new instances of the magpie Keeper
func NewKeeper(coinKeeper bank.Keeper, storeKey sdk.StoreKey, cdc *codec.Codec) Keeper {
return Keeper{
coinKeeper: coinKeeper,
storeKey: storeKey,
cdc: cdc,
}
}
func (k Keeper) SetPost(ctx sdk.Context, post Post) (sdk.Error, bool) {
if post.Owner.Empty() {
return sdk.ErrInvalidAddress("No address found."), false
}
store := ctx.KVStore(k.storeKey)
store.Set([]byte(post.ID), k.cdc.MustMarshalBinaryBare(post))
return nil, true
}
func (k Keeper) GetPost(ctx sdk.Context, id string) Post {
store := ctx.KVStore(k.storeKey)
if !store.Has([]byte(id)) {
return NewPost()
}
bz := store.Get([]byte(id))
var post Post
k.cdc.MustUnmarshalBinaryBare(bz, &post)
return post
}
func (k Keeper) EditPost(ctx sdk.Context, id string, message string) (sdk.Error, bool) {
post := k.GetPost(ctx, id)
post.Message = message
err, success := k.SetPost(ctx, post)
if err != nil {
return sdk.ErrUnknownRequest("Cannot save post."), false
}
return nil, success
}
func (k Keeper) GetPostOwner(ctx sdk.Context, id string) sdk.AccAddress {
return k.GetPost(ctx, id).Owner
}
// func (k Keeper) GetPostsByOwner(ctx sdk.Context, owner sdk.AccAddress) []Post {
// matchingPosts := []Post{}
// return matchingPosts
// }
func (k Keeper) GetPostLikes(ctx sdk.Context, id string) uint {
return k.GetPost(ctx, id).Likes
}
func (k Keeper) AddPostLike(ctx sdk.Context, id string) {
post := k.GetPost(ctx, id)
post.Likes = post.Likes + 1
k.SetPost(ctx, post)
}
func (k Keeper) SetLike(ctx sdk.Context, id string, like Like) (sdk.Error, bool) {
if like.Owner.Empty() || (len(like.PostID) == 0) {
return sdk.ErrUnauthorized("Liker and post id must exist."), false
}
post := k.GetPost(ctx, like.PostID)
if len(post.ID) == 0 {
return sdk.ErrUnknownRequest("The post requested does not exist."), false
}
store := ctx.KVStore(k.storeKey)
store.Set([]byte(id), k.cdc.MustMarshalBinaryBare(like))
k.AddPostLike(ctx, like.PostID)
return nil, true
}
func (k Keeper) GetLike(ctx sdk.Context, id string) Like {
store := ctx.KVStore(k.storeKey)
if !store.Has([]byte(id)) {
return NewLike()
}
bz := store.Get([]byte(id))
var like Like
k.cdc.MustUnmarshalBinaryBare(bz, &like)
return like
}
func (k Keeper) GetPostsIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, []byte{})
}
func (k Keeper) SetSession(ctx sdk.Context, session Session) (sdk.Error, bool) {
if session.Owner.Empty() {
return sdk.ErrInvalidAddress("No address found."), false
}
store := ctx.KVStore(k.storeKey)
store.Set([]byte(session.ID), k.cdc.MustMarshalBinaryBare(session))
return nil, true
}
func (k Keeper) GetSession(ctx sdk.Context, id string) Session {
store := ctx.KVStore(k.storeKey)
if !store.Has([]byte(id)) {
return NewSession()
}
bz := store.Get([]byte(id))
var session Session
k.cdc.MustUnmarshalBinaryBare(bz, &session)
return session
}
func (k Keeper) EditSession(ctx sdk.Context, id string, expiry time.Time) (sdk.Error, bool) {
session := k.GetSession(ctx, id)
session.Expiry = expiry
err, success := k.SetSession(ctx, session)
if err != nil {
return sdk.ErrUnknownRequest("Cannot update session."), false
}
return nil, success
}