- 新增收藏功能:favorite 服务与 handler、FavoriteButton 组件 - 新增关注功能:follow 服务与 handler、FollowButton 组件 - 用户主页适配关注/粉丝与收藏展示 - flash 消息工具、广告与侧边栏组件微调
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package handler
|
||
|
||
import (
|
||
"net/http"
|
||
"strconv"
|
||
|
||
"github.com/freefire/jiang13-bbs/middleware"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// ToggleFavorite 切换当前登录用户对帖子的收藏状态
|
||
func (h *Handlers) ToggleFavorite(c *gin.Context) {
|
||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
||
return
|
||
}
|
||
claims := middleware.CurrentUser(c)
|
||
if claims == nil {
|
||
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
|
||
return
|
||
}
|
||
favorited, err := h.Favorite.Toggle(uint(id), claims.ID)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{"favorited": favorited})
|
||
}
|
||
|
||
// UserFavorites 获取用户收藏的帖子列表(公开可见,仅 published)
|
||
func (h *Handlers) UserFavorites(c *gin.Context) {
|
||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的用户 ID"})
|
||
return
|
||
}
|
||
if _, err := h.Auth.GetUserByID(uint(id)); err != nil {
|
||
c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"})
|
||
return
|
||
}
|
||
|
||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
||
|
||
posts, total, err := h.Favorite.List(uint(id), page, size)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
// 填充点赞状态(仅登录用户)
|
||
if claims := middleware.CurrentUser(c); claims != nil {
|
||
ids := make([]uint, 0, len(posts))
|
||
for _, p := range posts {
|
||
ids = append(ids, p.ID)
|
||
}
|
||
likedMap := h.Like.BatchHasLiked(ids, claims.ID)
|
||
for i := range posts {
|
||
posts[i].Liked = likedMap[posts[i].ID]
|
||
}
|
||
}
|
||
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"posts": nonNilSlice(posts),
|
||
"total": total,
|
||
"page": page,
|
||
"size": size,
|
||
})
|
||
}
|