fix(social): 关注与取关改为幂等写入
SocialRepository.Follow 和 Unfollow 返回本次是否真的写入。判重交给唯一索引 idx_social_follower_vlogger。 因此重复关注和重复取关返回 200。重复请求不再落成 500。 缓存失效与通知只在真的变更时发生。自关注返回 400。响应体新增 created 与 deleted 字段。 SocialWorker 的调用只适配新签名,职责不变。删除它由后续提交完成。 验证:go build ./... 通过,go test -count=1 ./internal/social/... ./internal/worker/... 全绿。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package social
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"feedsystem_video_go/internal/account"
|
||||
"feedsystem_video_go/internal/apierror"
|
||||
"feedsystem_video_go/internal/middleware/jwt"
|
||||
@@ -9,6 +10,14 @@ import (
|
||||
"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
|
||||
}
|
||||
@@ -36,11 +45,12 @@ func (h *SocialHandler) Follow(c *gin.Context) {
|
||||
FollowerID: FollowerID,
|
||||
VloggerID: req.VloggerID,
|
||||
}
|
||||
if err := h.service.Follow(c.Request.Context(), social); err != nil {
|
||||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||||
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"})
|
||||
c.JSON(http.StatusOK, gin.H{"message": "followed", "created": created})
|
||||
}
|
||||
|
||||
func (h *SocialHandler) Unfollow(c *gin.Context) {
|
||||
@@ -62,11 +72,12 @@ func (h *SocialHandler) Unfollow(c *gin.Context) {
|
||||
FollowerID: FollowerID,
|
||||
VloggerID: req.VloggerID,
|
||||
}
|
||||
if err := h.service.Unfollow(c.Request.Context(), social); err != nil {
|
||||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||||
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"})
|
||||
c.JSON(http.StatusOK, gin.H{"message": "unfollowed", "deleted": deleted})
|
||||
}
|
||||
|
||||
func (h *SocialHandler) GetAllFollowers(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user