83 lines
No EOL
1.8 KiB
Go
83 lines
No EOL
1.8 KiB
Go
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))
|
|
} |