Support comment reply.

This commit is contained in:
2025-07-04 15:24:23 +08:00
parent 1f22367cc4
commit 54ab93ea7b
11 changed files with 243 additions and 92 deletions

View File

@@ -74,14 +74,24 @@ func CreateComment(req CommentRequest, userID uint, refID uint, ip string, cType
return nil, model.NewRequestError("Comment content exceeds maximum length of 1024 characters")
}
resourceExists, err := dao.ExistsResource(refID)
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")
switch cType {
case model.CommentTypeResource:
resourceExists, err := dao.ExistsResource(refID)
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")
}
case model.CommentTypeReply:
_, err := dao.GetCommentByID(refID)
if err != nil {
log.Error("Error getting reply comment:", err)
return nil, model.NewNotFoundError("Reply comment not found")
}
}
userExists, err := dao.ExistsUserByID(userID)
if err != nil {
log.Error("Error checking user existence:", err)
@@ -166,15 +176,6 @@ func ListResourceComments(resourceID uint, page int) ([]model.CommentView, int,
}
func ListCommentReplies(commentID uint, page int) ([]model.CommentView, int, error) {
comment, err := dao.GetCommentByID(commentID)
if err != nil {
log.Error("Error getting comment:", err)
return nil, 0, model.NewNotFoundError("Comment not found")
}
if comment.Type != model.CommentTypeReply {
return nil, 0, model.NewRequestError("This comment is not a reply")
}
replies, totalPages, err := dao.GetCommentReplies(commentID, page, pageSize)
if err != nil {
log.Error("Error getting replies:", err)

View File

@@ -1,6 +1,7 @@
package utils
import (
"errors"
"os"
"runtime"
)
@@ -19,7 +20,16 @@ func GetStoragePath() string {
}
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, os.ModePerm); err != nil {
panic("failed to create storage directory")
if errors.Is(err, os.ErrPermission) {
// Fallback to home directory if permission is denied
userDir, _ := os.UserHomeDir()
path = userDir + "/.nysoure"
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, os.ModePerm); err != nil {
panic("Failed to create storage directory: " + err.Error())
}
}
}
}
}
return path