Merge pull request 'add: post methods, posts storage' (#5) from post-branch into master

Reviewed-on: https://codeberg.org/NaMe2te/Blog/pulls/5
This commit is contained in:
RustamOper05 2024-01-15 17:00:18 +00:00
commit e819d8ce2c

View file

@ -1,6 +1,8 @@
package Post
import (
"strconv"
"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/runtime"
@ -19,7 +21,8 @@ type Post struct {
}
const (
lastIndex = "_lastIndex"
lastIndex = "_lastIndex"
all_posts_key = "all_posts"
)
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))
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) {
@ -61,6 +64,19 @@ func CbGetUUID(url string, postData any, code int, result []byte) {
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 {
ctx := storage.GetReadOnlyContext()
@ -72,6 +88,19 @@ func GetPost(postId string) 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) {
ctx := storage.GetContext()
post := GetPost(postId)