Add comment functionality.

This commit is contained in:
2025-05-13 17:11:48 +08:00
parent 545432b4f1
commit 0dd2143664
16 changed files with 406 additions and 53 deletions

53
server/service/comment.go Normal file
View File

@@ -0,0 +1,53 @@
package service
import (
"github.com/gofiber/fiber/v3/log"
"nysoure/server/dao"
"nysoure/server/model"
)
func CreateComment(content string, userID uint, resourceID uint) (*model.CommentView, error) {
resourceExists, err := dao.ExistsResource(resourceID)
if err != nil {
log.Error("Error checking resource existence:", err)
return nil, model.NewInternalServerError("Error checking resource existence")
}
if !resourceExists {
return nil, model.NewNotFoundError("Resource not found")
}
userExists, err := dao.ExistsUserByID(userID)
if err != nil {
log.Error("Error checking user existence:", err)
return nil, model.NewInternalServerError("Error checking user existence")
}
if !userExists {
return nil, model.NewNotFoundError("User not found")
}
c, err := dao.CreateComment(content, userID, resourceID)
if err != nil {
log.Error("Error creating comment:", err)
return nil, model.NewInternalServerError("Error creating comment")
}
return c.ToView(), nil
}
func ListComments(resourceID uint, page int) ([]model.CommentView, int, error) {
resourceExists, err := dao.ExistsResource(resourceID)
if err != nil {
log.Error("Error checking resource existence:", err)
return nil, 0, model.NewInternalServerError("Error checking resource existence")
}
if !resourceExists {
return nil, 0, model.NewNotFoundError("Resource not found")
}
comments, totalPages, err := dao.GetCommentByResourceID(resourceID, page, pageSize)
if err != nil {
log.Error("Error getting comments:", err)
return nil, 0, model.NewInternalServerError("Error getting comments")
}
res := make([]model.CommentView, 0, len(comments))
for _, c := range comments {
res = append(res, *c.ToView())
}
return res, totalPages, nil
}