fix: comment contract and compile a comment contract

This commit is contained in:
NaMe2te 2024-01-05 02:14:02 +03:00
parent 1a2161fbb2
commit 315a5860d5
5 changed files with 27 additions and 83 deletions

View file

@ -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))
}

15
comment/comment.go Normal file
View file

@ -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)
}

1
comment/comment.json Executable file
View file

@ -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}

BIN
comment/comment.nef Executable file

Binary file not shown.

11
comment/comment.yml Normal file
View file

@ -0,0 +1,11 @@
name: comment
sourceurl: http://example.com/
safemethods: []
supportedstandards: []
events:
- name: Hello world!
parameters:
- name: args
type: Array
permissions:
- methods: '*'