fix: post id's

This commit is contained in:
maksaid 2024-01-16 14:53:55 +03:00
parent cee71a5c35
commit 22b871e07e

View file

@ -1,11 +1,8 @@
package Post
import (
"strconv"
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
"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/storage"
)
@ -13,67 +10,48 @@ type Post struct {
postName string
header string
text string
authorId string
category string
login string
likes int
dislikes int
id string
}
const (
lastIndex = "_lastIndex"
all_posts_key = "all_posts"
lastIndex = "user_post_index"
id = "current_post_id"
)
func NewPost(authorId string, text string, postName string) {
func NewPost(login string, text string, postName string) {
ctx := storage.GetContext()
updatePostId()
id := storage.Get(ctx, id).(int)
post_id := "post_" + std.Itoa10(id)
post := Post{
text: text,
authorId: authorId,
login: login,
dislikes: 0,
likes: 0,
category: "none",
postName: postName,
id: "none",
id: post_id,
}
storeURL := "https://www.uuidgenerator.net/api/version7"
oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas)
post.id = storage.Get(ctx, "lastPostId").(string)
storage.Put(ctx, post.id, std.Serialize(post))
updatePostIndex(authorId)
lastPostIndex := storage.Get(ctx, authorId+lastIndex).(string)
updatePostIndex(login)
lastPostIndex := storage.Get(ctx, login+lastIndex).(string)
storage.Put(ctx, authorId+lastPostIndex, post.id)
}
func CbGetUUID(url string, postData any, code int, result []byte) {
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(oracle.Hash) {
panic("not called from the oracle contract")
}
if code != oracle.Success {
panic("request failed for " + url + " with code " + std.Itoa(code, 10))
}
runtime.Log("result for " + url + " is: " + string(result))
ctx := storage.GetContext()
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)
storage.Put(ctx, login+"_p_"+lastPostIndex, post.id)
}
func GetAllPosts() []Post {
ctx := storage.GetContext()
posts := std.Deserialize(storage.Get(ctx, all_posts_key).([]byte)).([]Post)
posts := make([]Post, 0)
ctx := storage.GetReadOnlyContext()
it := storage.Find(ctx, "post", storage.ValuesOnly|storage.DeserializeValues)
for iterator.Next(it) {
post := iterator.Value(it).(Post)
posts = append(posts, post)
}
return posts
}
@ -88,13 +66,13 @@ func GetPost(postId string) Post {
return std.Deserialize(data.([]byte)).(Post)
}
func GetAllPostsByUser(userId string) []Post {
func GetAllPostsByUser(login string) []Post {
ctx := storage.GetReadOnlyContext()
var postsByUser []Post
i := 0
n := getPostIndex(userId)
n := getPostIndex(login)
for i < n {
post := storage.Get(ctx, userId+strconv.Itoa(i))
post := storage.Get(ctx, login+"_p_"+std.Itoa10(i))
i++
postsByUser = append(postsByUser, std.Deserialize(post.([]byte)).(Post))
}
@ -123,3 +101,15 @@ func updatePostIndex(userId string) {
index := getPostIndex(userId)
storage.Put(ctx, userId+lastIndex, index+1)
}
func getPostId() int {
ctx := storage.GetContext()
current_id := storage.Get(ctx, id)
return current_id.(int)
}
func updatePostId() {
ctx := storage.GetContext()
cur_id := getPostId()
storage.Put(ctx, id, cur_id+1)
}