SocialRepository.Follow 和 Unfollow 返回本次是否真的写入。判重交给唯一索引 idx_social_follower_vlogger。 因此重复关注和重复取关返回 200。重复请求不再落成 500。 缓存失效与通知只在真的变更时发生。自关注返回 400。响应体新增 created 与 deleted 字段。 SocialWorker 的调用只适配新签名,职责不变。删除它由后续提交完成。 验证:go build ./... 通过,go test -count=1 ./internal/social/... ./internal/worker/... 全绿。
151 lines
4.2 KiB
Go
151 lines
4.2 KiB
Go
package social
|
|
|
|
import (
|
|
"errors"
|
|
"feedsystem_video_go/internal/account"
|
|
"feedsystem_video_go/internal/apierror"
|
|
"feedsystem_video_go/internal/middleware/jwt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// classifySocialError 只补一条本地语义:自关注是客户端错误,不是数据库故障。
|
|
func classifySocialError(err error) int {
|
|
if errors.Is(err, ErrCannotFollowSelf) {
|
|
return http.StatusBadRequest
|
|
}
|
|
return apierror.ClassifyHTTPStatus(err)
|
|
}
|
|
|
|
type SocialHandler struct {
|
|
service *SocialService
|
|
}
|
|
|
|
func NewSocialHandler(service *SocialService) *SocialHandler {
|
|
return &SocialHandler{service: service}
|
|
}
|
|
|
|
func (h *SocialHandler) Follow(c *gin.Context) {
|
|
var req FollowRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if req.VloggerID <= 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "vlogger_id is required"})
|
|
return
|
|
}
|
|
FollowerID, err := jwt.GetAccountID(c)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
social := &Social{
|
|
FollowerID: FollowerID,
|
|
VloggerID: req.VloggerID,
|
|
}
|
|
created, err := h.service.Follow(c.Request.Context(), social)
|
|
if err != nil {
|
|
c.JSON(classifySocialError(err), gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "followed", "created": created})
|
|
}
|
|
|
|
func (h *SocialHandler) Unfollow(c *gin.Context) {
|
|
var req UnfollowRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if req.VloggerID <= 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "vlogger_id is required"})
|
|
return
|
|
}
|
|
FollowerID, err := jwt.GetAccountID(c)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
social := &Social{
|
|
FollowerID: FollowerID,
|
|
VloggerID: req.VloggerID,
|
|
}
|
|
deleted, err := h.service.Unfollow(c.Request.Context(), social)
|
|
if err != nil {
|
|
c.JSON(classifySocialError(err), gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "unfollowed", "deleted": deleted})
|
|
}
|
|
|
|
func (h *SocialHandler) GetAllFollowers(c *gin.Context) {
|
|
var req GetAllFollowersRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
vloggerID := req.VloggerID
|
|
if vloggerID == 0 {
|
|
accountID, err := jwt.GetAccountID(c)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
vloggerID = accountID
|
|
}
|
|
|
|
followers, err := h.service.GetAllFollowers(c.Request.Context(), vloggerID)
|
|
if err != nil {
|
|
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if followers == nil {
|
|
followers = []*account.Account{}
|
|
}
|
|
followerCount, _ := h.service.CountFollowers(c.Request.Context(), vloggerID)
|
|
c.JSON(http.StatusOK, GetAllFollowersResponse{Followers: followers, FollowerCount: followerCount})
|
|
}
|
|
|
|
func (h *SocialHandler) GetAllVloggers(c *gin.Context) {
|
|
var req GetAllVloggersRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
followerID := req.FollowerID
|
|
if followerID == 0 {
|
|
accountID, err := jwt.GetAccountID(c)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
followerID = accountID
|
|
}
|
|
|
|
vloggers, err := h.service.GetAllVloggers(c.Request.Context(), followerID)
|
|
if err != nil {
|
|
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if vloggers == nil {
|
|
vloggers = []*account.Account{}
|
|
}
|
|
vloggerCount, _ := h.service.CountVloggers(c.Request.Context(), followerID)
|
|
c.JSON(http.StatusOK, GetAllVloggersResponse{Vloggers: vloggers, VloggerCount: vloggerCount})
|
|
}
|
|
|
|
func (h *SocialHandler) GetCounts(c *gin.Context) {
|
|
accountID, err := jwt.GetAccountID(c)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
followerCount, _ := h.service.CountFollowers(c.Request.Context(), accountID)
|
|
vloggerCount, _ := h.service.CountVloggers(c.Request.Context(), accountID)
|
|
c.JSON(http.StatusOK, SocialCounts{FollowerCount: followerCount, VloggerCount: vloggerCount})
|
|
}
|