From 1a2161fbb202095e3594488d1b13a87b0d212c05 Mon Sep 17 00:00:00 2001 From: NaMe2te Date: Thu, 4 Jan 2024 03:27:51 +0300 Subject: [PATCH 1/5] feat: comment contract --- Comment/comment_contract.go | 79 ++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/Comment/comment_contract.go b/Comment/comment_contract.go index f7dfe42..c9deeaf 100644 --- a/Comment/comment_contract.go +++ b/Comment/comment_contract.go @@ -1,8 +1,83 @@ -package Comments +package Comment -type Comments struct { +import ( + "github.com/nspcc-dev/neo-go/pkg/interop/native/std" + "github.com/nspcc-dev/neo-go/pkg/interop/storage" + "github.com/rs/xid" +) + +type Comment struct { + id string authorId string + postId string text string likes int dislikes int } + +func CreateNewComment(authorId string, postId string, text string) { + ctx := storage.GetContext() + newComment := Comment{ + id: xid.New().String(), + authorId: authorId, + postId: postId, + text: text, + likes: 0, + dislikes: 0, + } + + comments := GetByPostId(postId) + comments = append(comments, newComment) + storage.Put(ctx, postId+"_comment", std.Serialize(comments)) +} + +func GetByPostId(postId string) []Comment { + ctx := storage.GetContext() + return std.Deserialize(storage.Get(ctx, postId+"_comment").([]byte)).([]Comment) +} + +func GetByAuthorId(postId string, authorId string) []Comment { + comments := GetByPostId(postId) + var commentsByAuthor []Comment + for _, comment := range comments { + if comment.authorId == authorId { + commentsByAuthor = append(commentsByAuthor, comment) + } + } + + return commentsByAuthor +} + +func GetComment(commentId string, postId string) Comment { + comments := GetByPostId(postId) + for _, comment := range comments { + if comment.id == commentId { + return comment + } + } + + panic("Коммента с таким айдишником нету") +} + +func RateComment(isLike bool, postId string, commentId string) { + comment := GetComment(commentId, postId) + if isLike { + comment.likes++ + } else { + comment.dislikes++ + } + + UpdateComment(comment, postId) +} + +func UpdateComment(comment Comment, postId string) { + ctx := storage.GetContext() + comments := GetByPostId(postId) + for i := 0; i < len(comments); i++ { + if comments[i].id == comment.id { + comments[i] = comment + } + } + + storage.Put(ctx, postId+"_comment", std.Serialize(comments)) +} From 315a5860d516c33043357a261aaf2e1ab8cabc5b Mon Sep 17 00:00:00 2001 From: NaMe2te Date: Fri, 5 Jan 2024 02:14:02 +0300 Subject: [PATCH 2/5] fix: comment contract and compile a comment contract --- Comment/comment_contract.go | 83 ------------------------------------ comment/comment.go | 15 +++++++ comment/comment.json | 1 + comment/comment.nef | Bin 0 -> 134 bytes comment/comment.yml | 11 +++++ 5 files changed, 27 insertions(+), 83 deletions(-) delete mode 100644 Comment/comment_contract.go create mode 100644 comment/comment.go create mode 100755 comment/comment.json create mode 100755 comment/comment.nef create mode 100644 comment/comment.yml diff --git a/Comment/comment_contract.go b/Comment/comment_contract.go deleted file mode 100644 index c9deeaf..0000000 --- a/Comment/comment_contract.go +++ /dev/null @@ -1,83 +0,0 @@ -package Comment - -import ( - "github.com/nspcc-dev/neo-go/pkg/interop/native/std" - "github.com/nspcc-dev/neo-go/pkg/interop/storage" - "github.com/rs/xid" -) - -type Comment struct { - id string - authorId string - postId string - text string - likes int - dislikes int -} - -func CreateNewComment(authorId string, postId string, text string) { - ctx := storage.GetContext() - newComment := Comment{ - id: xid.New().String(), - authorId: authorId, - postId: postId, - text: text, - likes: 0, - dislikes: 0, - } - - comments := GetByPostId(postId) - comments = append(comments, newComment) - storage.Put(ctx, postId+"_comment", std.Serialize(comments)) -} - -func GetByPostId(postId string) []Comment { - ctx := storage.GetContext() - return std.Deserialize(storage.Get(ctx, postId+"_comment").([]byte)).([]Comment) -} - -func GetByAuthorId(postId string, authorId string) []Comment { - comments := GetByPostId(postId) - var commentsByAuthor []Comment - for _, comment := range comments { - if comment.authorId == authorId { - commentsByAuthor = append(commentsByAuthor, comment) - } - } - - return commentsByAuthor -} - -func GetComment(commentId string, postId string) Comment { - comments := GetByPostId(postId) - for _, comment := range comments { - if comment.id == commentId { - return comment - } - } - - panic("Коммента с таким айдишником нету") -} - -func RateComment(isLike bool, postId string, commentId string) { - comment := GetComment(commentId, postId) - if isLike { - comment.likes++ - } else { - comment.dislikes++ - } - - UpdateComment(comment, postId) -} - -func UpdateComment(comment Comment, postId string) { - ctx := storage.GetContext() - comments := GetByPostId(postId) - for i := 0; i < len(comments); i++ { - if comments[i].id == comment.id { - comments[i] = comment - } - } - - storage.Put(ctx, postId+"_comment", std.Serialize(comments)) -} diff --git a/comment/comment.go b/comment/comment.go new file mode 100644 index 0000000..8dc2ccd --- /dev/null +++ b/comment/comment.go @@ -0,0 +1,15 @@ +package comment + +import "github.com/nspcc-dev/neo-go/pkg/interop/runtime" + +var notificationName string + +// init initializes notificationName before calling any other smart-contract method +func init() { + notificationName = "Hello world!" +} + +// RuntimeNotify sends runtime notification with "Hello world!" name +func RuntimeNotify(args []any) { + runtime.Notify(notificationName, args) +} \ No newline at end of file diff --git a/comment/comment.json b/comment/comment.json new file mode 100755 index 0000000..76f4d83 --- /dev/null +++ b/comment/comment.json @@ -0,0 +1 @@ +{"name":"comment","abi":{"methods":[{"name":"_initialize","offset":0,"parameters":[],"returntype":"Void","safe":false},{"name":"runtimeNotify","offset":21,"parameters":[{"name":"args","type":"Array"}],"returntype":"Void","safe":false}],"events":[{"name":"Hello world!","parameters":[{"name":"args","type":"Array"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file diff --git a/comment/comment.nef b/comment/comment.nef new file mode 100755 index 0000000000000000000000000000000000000000..22d6ebdba40089f382826cb61da02cb138c7c7a2 GIT binary patch literal 134 zcmeZsbu-RO&DTxO*EP^HG%(RK)Ga7V)ip8GO;1U+v`8|xOk^MggfmJ?3as??Q!5g4 z3vyERlJj%*L3&if7HMWCS=)Wz0`>SW+4} F7XbU&9pL}~ literal 0 HcmV?d00001 diff --git a/comment/comment.yml b/comment/comment.yml new file mode 100644 index 0000000..0553d67 --- /dev/null +++ b/comment/comment.yml @@ -0,0 +1,11 @@ +name: comment +sourceurl: http://example.com/ +safemethods: [] +supportedstandards: [] +events: + - name: Hello world! + parameters: + - name: args + type: Array +permissions: + - methods: '*' From eadfefe4a5b1d6bf059583b99a3b5f5af50d8cb0 Mon Sep 17 00:00:00 2001 From: NaMe2te Date: Fri, 5 Jan 2024 16:39:52 +0300 Subject: [PATCH 3/5] fix: comment.go --- comment/comment.go | 94 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/comment/comment.go b/comment/comment.go index 8dc2ccd..a5c996c 100644 --- a/comment/comment.go +++ b/comment/comment.go @@ -1,15 +1,89 @@ package comment -import "github.com/nspcc-dev/neo-go/pkg/interop/runtime" +import ( + "github.com/nspcc-dev/neo-go/pkg/interop/native/std" + "github.com/nspcc-dev/neo-go/pkg/interop/storage" + "github.com/rs/xid" +) -var notificationName string - -// init initializes notificationName before calling any other smart-contract method -func init() { - notificationName = "Hello world!" +type Comment struct { + id string + authorId string + postId string + text string + likes int + dislikes int } -// RuntimeNotify sends runtime notification with "Hello world!" name -func RuntimeNotify(args []any) { - runtime.Notify(notificationName, args) -} \ No newline at end of file +func CreateNewComment(authorId string, postId string, text string) { + ctx := storage.GetContext() + newComment := Comment{ + id: xid.New().String(), + authorId: authorId, + 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, newComment.id, std.Serialize(newComment)) +} + +func GetByPostId(postId string) []Comment { + ctx := storage.GetContext() + return std.Deserialize(storage.Get(ctx, postId+"_comment").([]byte)).([]Comment) +} + +func GetByAuthorId(postId string, authorId string) []Comment { + comments := GetByPostId(postId) + var commentsByAuthor []Comment + for _, comment := range comments { + if comment.authorId == authorId { + commentsByAuthor = append(commentsByAuthor, comment) + } + } + + return commentsByAuthor +} + +func GetComment(commentId string, postId string) Comment { + comments := GetByPostId(postId) + for _, comment := range comments { + if comment.id == commentId { + return comment + } + } + + panic("Коммента с таким айдишником нету v poste nety") +} + +func GetComment(commentId string) Comment { + ctx := storage.GetContext() + return std.Deserialize(storage.Get(ctx, commentId).([]byte)).(Comment) +} + +func RateComment(isLike bool, postId string, commentId string) { + comment := GetComment(commentId, postId) + if isLike { + comment.likes++ + } else { + comment.dislikes++ + } + + UpdateComment(comment, postId) +} + +func UpdateComment(comment Comment, postId string) { + ctx := storage.GetContext() + comments := GetByPostId(postId) + for i := 0; i < len(comments); i++ { + if comments[i].id == comment.id { + comments[i] = comment + } + } + + storage.Put(ctx, postId+"_comment", std.Serialize(comments)) +} From a3df37d7c2bb6d7f88040e4766eed6a56aa85698 Mon Sep 17 00:00:00 2001 From: NaMe2te Date: Fri, 5 Jan 2024 18:07:07 +0300 Subject: [PATCH 4/5] fix: added update comment method --- comment/comment.go | 14 ++++---------- comment/comment.json | 2 +- comment/comment.nef | Bin 134 -> 526 bytes 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/comment/comment.go b/comment/comment.go index a5c996c..81ecbf5 100644 --- a/comment/comment.go +++ b/comment/comment.go @@ -1,9 +1,9 @@ package comment import ( + guuid "github.com/google/uuid" "github.com/nspcc-dev/neo-go/pkg/interop/native/std" "github.com/nspcc-dev/neo-go/pkg/interop/storage" - "github.com/rs/xid" ) type Comment struct { @@ -18,7 +18,7 @@ type Comment struct { func CreateNewComment(authorId string, postId string, text string) { ctx := storage.GetContext() newComment := Comment{ - id: xid.New().String(), + id: guuid.New().String(), authorId: authorId, postId: postId, text: text, @@ -29,7 +29,6 @@ func CreateNewComment(authorId string, postId string, text string) { comments := GetByPostId(postId) comments = append(comments, newComment) storage.Put(ctx, postId+"_comment", std.Serialize(comments)) - storage.Put(ctx, newComment.id, std.Serialize(newComment)) } func GetByPostId(postId string) []Comment { @@ -57,12 +56,7 @@ func GetComment(commentId string, postId string) Comment { } } - panic("Коммента с таким айдишником нету v poste nety") -} - -func GetComment(commentId string) Comment { - ctx := storage.GetContext() - return std.Deserialize(storage.Get(ctx, commentId).([]byte)).(Comment) + panic("Коммента с таким айдишником нету") } func RateComment(isLike bool, postId string, commentId string) { @@ -86,4 +80,4 @@ func UpdateComment(comment Comment, postId string) { } storage.Put(ctx, postId+"_comment", std.Serialize(comments)) -} +} \ No newline at end of file diff --git a/comment/comment.json b/comment/comment.json index 76f4d83..e876375 100755 --- a/comment/comment.json +++ b/comment/comment.json @@ -1 +1 @@ -{"name":"comment","abi":{"methods":[{"name":"_initialize","offset":0,"parameters":[],"returntype":"Void","safe":false},{"name":"runtimeNotify","offset":21,"parameters":[{"name":"args","type":"Array"}],"returntype":"Void","safe":false}],"events":[{"name":"Hello world!","parameters":[{"name":"args","type":"Array"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file +{"name":"comment","abi":{"methods":[{"name":"createNewComment","offset":0,"parameters":[{"name":"authorId","type":"String"},{"name":"postId","type":"String"},{"name":"text","type":"String"}],"returntype":"Void","safe":false},{"name":"getByAuthorId","offset":109,"parameters":[{"name":"postId","type":"String"},{"name":"authorId","type":"String"}],"returntype":"Array","safe":false},{"name":"getByPostId","offset":61,"parameters":[{"name":"postId","type":"String"}],"returntype":"Array","safe":false},{"name":"getComment","offset":157,"parameters":[{"name":"commentId","type":"String"},{"name":"postId","type":"String"}],"returntype":"Array","safe":false},{"name":"rateComment","offset":259,"parameters":[{"name":"isLike","type":"Boolean"},{"name":"postId","type":"String"},{"name":"commentId","type":"String"}],"returntype":"Void","safe":false}],"events":[{"name":"Hello world!","parameters":[{"name":"args","type":"Array"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file diff --git a/comment/comment.nef b/comment/comment.nef index 22d6ebdba40089f382826cb61da02cb138c7c7a2..62776e66ec27a3c54275b8cd8f24d1966c87decc 100755 GIT binary patch literal 526 zcmeZsbu-RO&DTxO*EP^HG%(RK)Ga7V)ip8GO;1U+v`8|xOk^MggfmJ?3as??Q!5g4 z3vyERlJj%*8JG^dw>;iAGVD*9|om*F#>J=Zmt;85oK)DtS2K zfwt$S=9P432RlBqmuPVaXJUkC&Zt0=4RD+k`@+!crhy8}ZG*y0b4CWQn+`y>gF`qo zQ-#Ty0`9^LuTuiv-bOM)zTW4GvINgnPFLg2gxZ*OzHpAx6IWN)OrZHpl_qlvpsK|{ zstYp(&Q(lTV+YI6QF;ZWd2BAsy0GuU9w6L$VeiGJ3mX(JHY$MV3%f4txUfe7$k=&d z%Y_{mJ1*=63hV+YRsb@$UTnJ9Y~>Kn%3M_$U^1nkLX9IsE<@lP&> Date: Mon, 8 Jan 2024 15:47:46 +0300 Subject: [PATCH 5/5] style: rename comment to Comment --- {comment => Comment}/comment.go | 2 +- {comment => Comment}/comment.json | 0 {comment => Comment}/comment.nef | Bin {comment => Comment}/comment.yml | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename {comment => Comment}/comment.go (99%) rename {comment => Comment}/comment.json (100%) rename {comment => Comment}/comment.nef (100%) rename {comment => Comment}/comment.yml (100%) diff --git a/comment/comment.go b/Comment/comment.go similarity index 99% rename from comment/comment.go rename to Comment/comment.go index 81ecbf5..66789f3 100644 --- a/comment/comment.go +++ b/Comment/comment.go @@ -1,4 +1,4 @@ -package comment +package Comment import ( guuid "github.com/google/uuid" diff --git a/comment/comment.json b/Comment/comment.json similarity index 100% rename from comment/comment.json rename to Comment/comment.json diff --git a/comment/comment.nef b/Comment/comment.nef similarity index 100% rename from comment/comment.nef rename to Comment/comment.nef diff --git a/comment/comment.yml b/Comment/comment.yml similarity index 100% rename from comment/comment.yml rename to Comment/comment.yml