后端 Go+Gin:认证/CSRF/限流、板块、帖子、评论、点赞、通知、用户资料、置顶推荐;前端 Next.js 16:发帖/编辑/删除、搜索、分页、点赞、通知中心、设置;基础设施 docker-compose 与配置模板;添加 .gitignore 与专有许可证(保留所有权利)
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/freefire/jiang13-bbs/middleware"
|
|
"github.com/freefire/jiang13-bbs/model"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// PostComments 获取帖子评论
|
|
func (h *Handlers) PostComments(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
|
return
|
|
}
|
|
comments, err := h.Comment.ListByPost(uint(id))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"comments": comments})
|
|
}
|
|
|
|
// CreateCommentRequest 评论请求
|
|
type CreateCommentRequest struct {
|
|
Content string `json:"content" binding:"required,min=1"`
|
|
}
|
|
|
|
// CreateComment 创建评论
|
|
func (h *Handlers) CreateComment(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
|
return
|
|
}
|
|
var req CreateCommentRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
comment, err := h.Comment.Create(claims.ID, uint(id), req.Content)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
// 通知帖子作者(排除自己评论自己的帖子)
|
|
if authorID, e := h.Post.GetAuthorID(uint(id)); e == nil {
|
|
h.Notification.Create(authorID, claims.ID, model.NotificationTypeComment, uint(id), comment.ID, req.Content)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"comment": comment})
|
|
}
|
|
|
|
// DeleteComment 删除评论
|
|
func (h *Handlers) DeleteComment(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
cid, err := strconv.ParseUint(c.Param("cid"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的评论 ID"})
|
|
return
|
|
}
|
|
if err := h.Comment.Delete(uint(cid), claims.ID, claims.Role); err != nil {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "已删除"})
|
|
}
|