Blog/Post/post_contract.go
2024-01-02 19:31:11 +03:00

83 lines
1.5 KiB
Go

package Post
import (
"strconv"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
"github.com/rs/xid"
)
type Post struct {
postName string
header string
text string
authorId string
category string
likes int
dislikes int
id string
}
const (
lastIndex = "_lastIndex"
)
/*
func _deploy(data interface{}, isUpdate bool, userId string) {
if isUpdate {
return
}
}
*/
func NewPost(authorId string, text string, postName string) {
ctx := storage.GetContext()
post := Post{
text: text,
authorId: authorId,
dislikes: 0,
likes: 0,
category: "none",
postName: postName,
id: xid.New().String(),
}
storage.Put(ctx, post.id, std.Serialize(post))
updatePostIndex(authorId)
storage.Put(ctx, authorId+strconv.Itoa(getPostIndex(authorId)), post.id)
}
func GetPost(postId string) Post {
ctx := storage.GetReadOnlyContext()
data := storage.Get(ctx, postId)
if data == nil {
panic("player not found")
}
return std.Deserialize(data.([]byte)).(Post)
}
func RatePost(isLike bool, postId string) {
ctx := storage.GetContext()
post := GetPost(postId)
if isLike {
post.likes++
} else {
post.dislikes++
}
storage.Put(ctx, post.id, std.Serialize(post))
}
func getPostIndex(userId string) int {
ctx := storage.GetContext()
index := storage.Get(ctx, userId+lastIndex)
return index.(int)
}
func updatePostIndex(userId string) {
ctx := storage.GetContext()
index := getPostIndex(userId)
storage.Put(ctx, userId+lastIndex, index+1)
}