Merge branch 'comments'

This commit is contained in:
NaMe2te 2024-01-08 16:05:41 +03:00
commit 94e9ea947c
5 changed files with 95 additions and 8 deletions

83
Comment/comment.go Normal file
View file

@ -0,0 +1,83 @@
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"
)
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: guuid.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))
}

1
Comment/comment.json Executable file
View file

@ -0,0 +1 @@
{"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}

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: '*'

View file

@ -1,8 +0,0 @@
package Comments
type Comments struct {
authorId string
text string
likes int
dislikes int
}