add: post methods, posts storage

This commit is contained in:
maksaid 2024-01-15 15:26:08 +03:00
parent 7346f728e9
commit cee71a5c35

View file

@ -1,6 +1,8 @@
package Post package Post
import ( import (
"strconv"
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle" "github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std" "github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
@ -20,6 +22,7 @@ type Post struct {
const ( const (
lastIndex = "_lastIndex" lastIndex = "_lastIndex"
all_posts_key = "all_posts"
) )
func NewPost(authorId string, text string, postName string) { func NewPost(authorId string, text string, postName string) {
@ -42,9 +45,9 @@ func NewPost(authorId string, text string, postName string) {
storage.Put(ctx, post.id, std.Serialize(post)) storage.Put(ctx, post.id, std.Serialize(post))
updatePostIndex(authorId) updatePostIndex(authorId)
lastPostIndex := storage.Get(ctx, authorId+lastIndex) lastPostIndex := storage.Get(ctx, authorId+lastIndex).(string)
storage.Put(ctx, authorId+lastPostIndex.(string), post.id) storage.Put(ctx, authorId+lastPostIndex, post.id)
} }
func CbGetUUID(url string, postData any, code int, result []byte) { func CbGetUUID(url string, postData any, code int, result []byte) {
@ -61,6 +64,19 @@ func CbGetUUID(url string, postData any, code int, result []byte) {
storage.Put(ctx, "lastPostId", string(result)) storage.Put(ctx, "lastPostId", string(result))
} }
func appendPostToAllPosts(post Post) {
ctx := storage.GetContext()
all_posts := GetAllPosts()
posts := append(all_posts, post)
storage.Put(ctx, all_posts_key, posts)
}
func GetAllPosts() []Post {
ctx := storage.GetContext()
posts := std.Deserialize(storage.Get(ctx, all_posts_key).([]byte)).([]Post)
return posts
}
func GetPost(postId string) Post { func GetPost(postId string) Post {
ctx := storage.GetReadOnlyContext() ctx := storage.GetReadOnlyContext()
@ -72,6 +88,19 @@ func GetPost(postId string) Post {
return std.Deserialize(data.([]byte)).(Post) return std.Deserialize(data.([]byte)).(Post)
} }
func GetAllPostsByUser(userId string) []Post {
ctx := storage.GetReadOnlyContext()
var postsByUser []Post
i := 0
n := getPostIndex(userId)
for i < n {
post := storage.Get(ctx, userId+strconv.Itoa(i))
i++
postsByUser = append(postsByUser, std.Deserialize(post.([]byte)).(Post))
}
return postsByUser
}
func RatePost(isLike bool, postId string) { func RatePost(isLike bool, postId string) {
ctx := storage.GetContext() ctx := storage.GetContext()
post := GetPost(postId) post := GetPost(postId)