feat: get by login, get comment in post, add saving comment by comment id
This commit is contained in:
parent
2a22f14509
commit
1c7d169c21
1 changed files with 64 additions and 42 deletions
|
@ -1,69 +1,73 @@
|
||||||
package Comment
|
package Comment
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"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/native/std"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Comment struct {
|
type Comment struct {
|
||||||
id string
|
id string
|
||||||
authorId string
|
userLogin string
|
||||||
postId string
|
postId string
|
||||||
text string
|
text string
|
||||||
likes int
|
likes int
|
||||||
dislikes int
|
dislikes int
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateNewComment(authorId string, postId string, text string) {
|
const (
|
||||||
|
comment_prefix = "comment_"
|
||||||
|
post_prefix = "post_"
|
||||||
|
user_comment_count_prefix = "comment_count_"
|
||||||
|
)
|
||||||
|
|
||||||
|
func _deploy(data interface{}, isUpdate bool) {
|
||||||
|
ctx := storage.GetContext()
|
||||||
|
storage.Put(ctx, "index_comment", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateNewComment(userLogin string, postId string, text string) {
|
||||||
ctx := storage.GetContext()
|
ctx := storage.GetContext()
|
||||||
|
|
||||||
newComment := Comment{
|
id := storage.Get(ctx, "index_comment").(int)
|
||||||
id: "none",
|
id++
|
||||||
authorId: authorId,
|
storage.Put(ctx, "index_comment", id)
|
||||||
postId: postId,
|
|
||||||
text: text,
|
if storage.Get(ctx, user_comment_count_prefix+userLogin) == nil {
|
||||||
likes: 0,
|
storage.Put(ctx, user_comment_count_prefix+userLogin, 0)
|
||||||
dislikes: 0,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
storeURL := "https://www.uuidgenerator.net/api/version7"
|
commentId := comment_prefix + std.Itoa10(id)
|
||||||
oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas)
|
newComment := Comment{
|
||||||
|
id: commentId,
|
||||||
newComment.id = storage.Get(ctx, "lastCommentId").(string)
|
userLogin: userLogin,
|
||||||
|
postId: postId,
|
||||||
|
text: text,
|
||||||
|
likes: 0,
|
||||||
|
dislikes: 0,
|
||||||
|
}
|
||||||
|
|
||||||
comments := GetByPostId(postId)
|
comments := GetByPostId(postId)
|
||||||
comments = append(comments, newComment)
|
comments = append(comments, newComment)
|
||||||
storage.Put(ctx, postId+"_comment", std.Serialize(comments))
|
storage.Put(ctx, comment_prefix+postId, std.Serialize(comments)) // добавление комментов в общий список комментов поста
|
||||||
}
|
storage.Put(ctx, commentId, std.Serialize(newComment)) // добавление коммента отдельно по ключу
|
||||||
|
|
||||||
func cbGetUUID(url string, commentData any, code int, result []byte) {
|
commentCount := storage.Get(ctx, user_comment_count_prefix+userLogin).(int)
|
||||||
callingHash := runtime.GetCallingScriptHash()
|
commentCount++
|
||||||
if !callingHash.Equals(oracle.Hash) {
|
storage.Put(ctx, user_comment_count_prefix+userLogin, commentCount) // обновление счетчика коммента юзера
|
||||||
panic("not called from the oracle contract")
|
storage.Put(ctx, userLogin+"_c_"+std.Itoa10(commentCount), commentId) // хранение айдишника коммента по юзеру
|
||||||
}
|
|
||||||
if code != oracle.Success {
|
|
||||||
panic("request failed for " + url + " with code " + std.Itoa(code, 10))
|
|
||||||
}
|
|
||||||
runtime.Log("result for " + url + " is: " + string(result))
|
|
||||||
|
|
||||||
runtime.Log("Last Comment id is: " + string(result))
|
|
||||||
|
|
||||||
ctx := storage.GetContext()
|
|
||||||
storage.Put(ctx, "lastCommentId", string(result))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetByPostId(postId string) []Comment {
|
func GetByPostId(postId string) []Comment {
|
||||||
ctx := storage.GetContext()
|
ctx := storage.GetContext()
|
||||||
return std.Deserialize(storage.Get(ctx, postId+"_comment").([]byte)).([]Comment)
|
return std.Deserialize(storage.Get(ctx, comment_prefix+postId).([]byte)).([]Comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetByAuthorId(postId string, authorId string) []Comment {
|
func GetByLoginInPost(postId string, login string) []Comment {
|
||||||
comments := GetByPostId(postId)
|
comments := GetByPostId(postId)
|
||||||
var commentsByAuthor []Comment
|
var commentsByAuthor []Comment
|
||||||
for _, comment := range comments {
|
for _, comment := range comments {
|
||||||
if comment.authorId == authorId {
|
if comment.userLogin == login {
|
||||||
commentsByAuthor = append(commentsByAuthor, comment)
|
commentsByAuthor = append(commentsByAuthor, comment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,7 +75,24 @@ func GetByAuthorId(postId string, authorId string) []Comment {
|
||||||
return commentsByAuthor
|
return commentsByAuthor
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetComment(commentId string, postId string) Comment {
|
func GetByLogin(login string) []Comment {
|
||||||
|
var comments []Comment
|
||||||
|
ctx := storage.GetContext()
|
||||||
|
it := storage.Find(ctx, login+"_c_", storage.ValuesOnly|storage.DeserializeValues)
|
||||||
|
for iterator.Next(it) {
|
||||||
|
commentId := iterator.Value(it).(string)
|
||||||
|
comments = append(comments, GetComment(commentId))
|
||||||
|
}
|
||||||
|
|
||||||
|
return comments
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetComment(commentId string) Comment {
|
||||||
|
ctx := storage.GetContext()
|
||||||
|
return storage.Get(ctx, commentId).(Comment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCommentInPost(commentId string, postId string) Comment {
|
||||||
comments := GetByPostId(postId)
|
comments := GetByPostId(postId)
|
||||||
for _, comment := range comments {
|
for _, comment := range comments {
|
||||||
if comment.id == commentId {
|
if comment.id == commentId {
|
||||||
|
@ -83,7 +104,7 @@ func GetComment(commentId string, postId string) Comment {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RateComment(isLike bool, postId string, commentId string) {
|
func RateComment(isLike bool, postId string, commentId string) {
|
||||||
comment := GetComment(commentId, postId)
|
comment := GetComment(commentId)
|
||||||
if isLike {
|
if isLike {
|
||||||
comment.likes++
|
comment.likes++
|
||||||
} else {
|
} else {
|
||||||
|
@ -102,5 +123,6 @@ func UpdateComment(comment Comment, postId string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
storage.Put(ctx, postId+"_comment", std.Serialize(comments))
|
storage.Put(ctx, comment_prefix+postId, std.Serialize(comments))
|
||||||
|
storage.Put(ctx, comment.id, std.Serialize(comment))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue