Blog/Post/post_contract.go

122 lines
2.4 KiB
Go
Raw Permalink Normal View History

2024-01-02 19:31:11 +03:00
package Post
import (
2024-01-16 14:53:55 +03:00
"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/storage"
)
type Post struct {
2024-01-02 19:31:11 +03:00
postName string
header string
text string
2024-01-16 14:53:55 +03:00
login string
likes int
dislikes int
2024-01-02 19:31:11 +03:00
id string
}
2024-01-02 19:31:11 +03:00
const (
2024-01-16 14:53:55 +03:00
lastIndex = "user_post_index"
id = "current_post_id"
2024-01-02 19:31:11 +03:00
)
2024-01-16 14:53:55 +03:00
func NewPost(login string, text string, postName string) {
ctx := storage.GetContext()
2024-01-16 14:53:55 +03:00
updatePostId()
id := storage.Get(ctx, id).(int)
post_id := "post_" + std.Itoa10(id)
post := Post{
text: text,
2024-01-16 14:53:55 +03:00
login: login,
dislikes: 0,
2023-12-22 16:09:35 +03:00
likes: 0,
2024-01-02 19:31:11 +03:00
postName: postName,
2024-01-16 14:53:55 +03:00
id: post_id,
}
2024-01-14 21:28:44 +03:00
storage.Put(ctx, post.id, std.Serialize(post))
2024-01-14 21:28:44 +03:00
2024-01-16 14:53:55 +03:00
updatePostIndex(login)
lastPostIndex := storage.Get(ctx, login+lastIndex).(string)
2024-01-14 21:28:44 +03:00
2024-01-16 14:53:55 +03:00
storage.Put(ctx, login+"_p_"+lastPostIndex, post.id)
2024-01-15 15:26:08 +03:00
}
func GetAllPosts() []Post {
2024-01-16 14:53:55 +03:00
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)
}
2024-01-15 15:26:08 +03:00
return posts
}
func GetPost(postId string) Post {
ctx := storage.GetReadOnlyContext()
data := storage.Get(ctx, postId)
if data == nil {
2024-01-14 21:28:44 +03:00
panic("post not found")
}
return std.Deserialize(data.([]byte)).(Post)
}
2024-01-16 14:53:55 +03:00
func GetAllPostsByUser(login string) []Post {
2024-01-15 15:26:08 +03:00
ctx := storage.GetReadOnlyContext()
var postsByUser []Post
i := 0
2024-01-16 14:53:55 +03:00
n := getPostIndex(login)
2024-01-15 15:26:08 +03:00
for i < n {
2024-01-16 14:53:55 +03:00
post := storage.Get(ctx, login+"_p_"+std.Itoa10(i))
2024-01-15 15:26:08 +03:00
i++
postsByUser = append(postsByUser, std.Deserialize(post.([]byte)).(Post))
}
return postsByUser
}
func RatePost(isLike bool, postId string) {
2024-01-02 19:31:11 +03:00
ctx := storage.GetContext()
post := GetPost(postId)
if isLike {
post.likes++
} else {
post.dislikes++
}
storage.Put(ctx, post.id, std.Serialize(post))
}
2024-01-02 19:31:11 +03:00
func getPostIndex(userId string) int {
ctx := storage.GetContext()
index := storage.Get(ctx, userId+lastIndex)
2024-01-16 15:11:13 +03:00
if index == nil {
index = 0
}
2024-01-02 19:31:11 +03:00
return index.(int)
}
func updatePostIndex(userId string) {
ctx := storage.GetContext()
index := getPostIndex(userId)
storage.Put(ctx, userId+lastIndex, index+1)
}
2024-01-16 14:53:55 +03:00
func getPostId() int {
ctx := storage.GetContext()
current_id := storage.Get(ctx, id)
2024-01-16 15:11:13 +03:00
if current_id == nil {
current_id = 0
}
2024-01-16 14:53:55 +03:00
return current_id.(int)
}
func updatePostId() {
ctx := storage.GetContext()
cur_id := getPostId()
storage.Put(ctx, id, cur_id+1)
}