2024-01-04 23:14:02 +00:00
|
|
|
package comment
|
|
|
|
|
2024-01-05 13:39:52 +00:00
|
|
|
import (
|
2024-01-05 15:07:07 +00:00
|
|
|
guuid "github.com/google/uuid"
|
2024-01-05 13:39:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
)
|
2024-01-04 23:14:02 +00:00
|
|
|
|
2024-01-05 13:39:52 +00:00
|
|
|
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{
|
2024-01-05 15:07:07 +00:00
|
|
|
id: guuid.New().String(),
|
2024-01-05 13:39:52 +00:00
|
|
|
authorId: authorId,
|
|
|
|
postId: postId,
|
|
|
|
text: text,
|
|
|
|
likes: 0,
|
|
|
|
dislikes: 0,
|
|
|
|
}
|
2024-01-04 23:14:02 +00:00
|
|
|
|
2024-01-05 13:39:52 +00:00
|
|
|
comments := GetByPostId(postId)
|
|
|
|
comments = append(comments, newComment)
|
|
|
|
storage.Put(ctx, postId+"_comment", std.Serialize(comments))
|
2024-01-04 23:14:02 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 13:39:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 15:07:07 +00:00
|
|
|
panic("Коммента с таким айдишником нету")
|
2024-01-05 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2024-01-05 15:07:07 +00:00
|
|
|
}
|