From 1c7d169c2179ccfc09c9618e83c34f3a30658d48 Mon Sep 17 00:00:00 2001 From: NaMe2te Date: Tue, 16 Jan 2024 16:00:16 +0300 Subject: [PATCH] feat: get by login, get comment in post, add saving comment by comment id --- Comment/comment.go | 106 +++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 42 deletions(-) diff --git a/Comment/comment.go b/Comment/comment.go index 8b89e79..a0343f5 100755 --- a/Comment/comment.go +++ b/Comment/comment.go @@ -1,69 +1,73 @@ package Comment 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/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/storage" ) type Comment struct { - id string - authorId string - postId string - text string - likes int - dislikes int + id string + userLogin string + postId string + text string + likes 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() - newComment := Comment{ - id: "none", - authorId: authorId, - postId: postId, - text: text, - likes: 0, - dislikes: 0, + id := storage.Get(ctx, "index_comment").(int) + id++ + storage.Put(ctx, "index_comment", id) + + if storage.Get(ctx, user_comment_count_prefix+userLogin) == nil { + storage.Put(ctx, user_comment_count_prefix+userLogin, 0) } - storeURL := "https://www.uuidgenerator.net/api/version7" - oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas) - - newComment.id = storage.Get(ctx, "lastCommentId").(string) + commentId := comment_prefix + std.Itoa10(id) + newComment := Comment{ + id: commentId, + userLogin: userLogin, + postId: postId, + text: text, + likes: 0, + dislikes: 0, + } comments := GetByPostId(postId) 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) { - 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)) - - runtime.Log("Last Comment id is: " + string(result)) - - ctx := storage.GetContext() - storage.Put(ctx, "lastCommentId", string(result)) + commentCount := storage.Get(ctx, user_comment_count_prefix+userLogin).(int) + commentCount++ + storage.Put(ctx, user_comment_count_prefix+userLogin, commentCount) // обновление счетчика коммента юзера + storage.Put(ctx, userLogin+"_c_"+std.Itoa10(commentCount), commentId) // хранение айдишника коммента по юзеру } func GetByPostId(postId string) []Comment { 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) var commentsByAuthor []Comment for _, comment := range comments { - if comment.authorId == authorId { + if comment.userLogin == login { commentsByAuthor = append(commentsByAuthor, comment) } } @@ -71,7 +75,24 @@ func GetByAuthorId(postId string, authorId string) []Comment { 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) for _, comment := range comments { if comment.id == commentId { @@ -83,7 +104,7 @@ func GetComment(commentId string, postId string) Comment { } func RateComment(isLike bool, postId string, commentId string) { - comment := GetComment(commentId, postId) + comment := GetComment(commentId) if isLike { comment.likes++ } 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)) }