后端 Go+Gin:认证/CSRF/限流、板块、帖子、评论、点赞、通知、用户资料、置顶推荐;前端 Next.js 16:发帖/编辑/删除、搜索、分页、点赞、通知中心、设置;基础设施 docker-compose 与配置模板;添加 .gitignore 与专有许可证(保留所有权利)
33 lines
888 B
Go
33 lines
888 B
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/freefire/jiang13-bbs/middleware"
|
|
"github.com/freefire/jiang13-bbs/model"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ToggleLike 点赞/取消点赞
|
|
func (h *Handlers) ToggleLike(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
|
|
}
|
|
liked, count, err := h.Like.Toggle(uint(id), claims.ID)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
// 仅点赞时通知帖子作者(取消点赞不通知)
|
|
if liked {
|
|
if authorID, e := h.Post.GetAuthorID(uint(id)); e == nil {
|
|
h.Notification.Create(authorID, claims.ID, model.NotificationTypeLike, uint(id), 0, "")
|
|
}
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"liked": liked, "like_count": count})
|
|
}
|