fix(P2): 修复 import cycle — 将 apierror 移出 internal/http 避免循环依赖

This commit is contained in:
Sisyphus
2026-04-25 16:18:57 +08:00
parent 4094a13846
commit 15f86f08ae
9 changed files with 61 additions and 61 deletions

View File

@@ -2,7 +2,7 @@ package video
import (
"feedsystem_video_go/internal/account"
httputil "feedsystem_video_go/internal/http"
"feedsystem_video_go/internal/apierror"
"feedsystem_video_go/internal/middleware/jwt"
"github.com/gin-gonic/gin"
@@ -19,7 +19,7 @@ func NewCommentHandler(service *CommentService, accountService *account.AccountS
func (h *CommentHandler) PublishComment(c *gin.Context) {
var req PublishCommentRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if req.Content == "" {
@@ -32,12 +32,12 @@ func (h *CommentHandler) PublishComment(c *gin.Context) {
}
authorId, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
user, err := h.accountService.FindByID(c.Request.Context(), authorId)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
comment := &Comment{
@@ -47,7 +47,7 @@ func (h *CommentHandler) PublishComment(c *gin.Context) {
Content: req.Content,
}
if err := h.service.Publish(c.Request.Context(), comment); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "comment published successfully"})
@@ -56,12 +56,12 @@ func (h *CommentHandler) PublishComment(c *gin.Context) {
func (h *CommentHandler) DeleteComment(c *gin.Context) {
var req DeleteCommentRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
accountID, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if req.CommentID <= 0 {
@@ -69,7 +69,7 @@ func (h *CommentHandler) DeleteComment(c *gin.Context) {
return
}
if err := h.service.Delete(c.Request.Context(), req.CommentID, accountID); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
@@ -79,7 +79,7 @@ func (h *CommentHandler) DeleteComment(c *gin.Context) {
func (h *CommentHandler) GetAllComments(c *gin.Context) {
var req GetAllCommentsRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if req.VideoID == 0 {
@@ -88,7 +88,7 @@ func (h *CommentHandler) GetAllComments(c *gin.Context) {
}
comments, err := h.service.GetAll(c.Request.Context(), req.VideoID)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if comments == nil {

View File

@@ -5,7 +5,7 @@ import (
"errors"
"feedsystem_video_go/internal/middleware/rabbitmq"
rediscache "feedsystem_video_go/internal/middleware/redis"
httputil "feedsystem_video_go/internal/http"
"feedsystem_video_go/internal/apierror"
"strings"
"gorm.io/gorm"
@@ -95,7 +95,7 @@ func (s *CommentService) Delete(ctx context.Context, commentID uint, accountID u
return errors.New("comment not found")
}
if comment.AuthorID != accountID {
return httputil.ErrUnauthorized
return apierror.ErrUnauthorized
}
if s.commentMQ != nil {
if err := s.commentMQ.Delete(ctx, commentID); err == nil {

View File

@@ -2,7 +2,7 @@ package video
import (
"feedsystem_video_go/internal/middleware/jwt"
httputil "feedsystem_video_go/internal/http"
"feedsystem_video_go/internal/apierror"
"github.com/gin-gonic/gin"
)
@@ -18,7 +18,7 @@ func NewLikeHandler(service *LikeService) *LikeHandler {
func (lh *LikeHandler) Like(c *gin.Context) {
var req LikeRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if req.VideoID <= 0 {
@@ -28,7 +28,7 @@ func (lh *LikeHandler) Like(c *gin.Context) {
accountID, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
@@ -46,7 +46,7 @@ func (lh *LikeHandler) Like(c *gin.Context) {
func (lh *LikeHandler) Unlike(c *gin.Context) {
var req LikeRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if req.VideoID <= 0 {
@@ -56,7 +56,7 @@ func (lh *LikeHandler) Unlike(c *gin.Context) {
accountID, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
@@ -74,7 +74,7 @@ func (lh *LikeHandler) Unlike(c *gin.Context) {
func (lh *LikeHandler) IsLiked(c *gin.Context) {
var req LikeRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if req.VideoID <= 0 {
@@ -84,7 +84,7 @@ func (lh *LikeHandler) IsLiked(c *gin.Context) {
accountID, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
isLiked, err := lh.service.IsLiked(c.Request.Context(), req.VideoID, accountID)
@@ -98,7 +98,7 @@ func (lh *LikeHandler) IsLiked(c *gin.Context) {
func (lh *LikeHandler) ListMyLikedVideos(c *gin.Context) {
accountID, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}

View File

@@ -12,7 +12,7 @@ import (
"time"
"feedsystem_video_go/internal/account"
httputil "feedsystem_video_go/internal/http"
"feedsystem_video_go/internal/apierror"
"feedsystem_video_go/internal/middleware/jwt"
"github.com/gin-gonic/gin"
@@ -30,18 +30,18 @@ func NewVideoHandler(service *VideoService, accountService *account.AccountServi
func (vh *VideoHandler) PublishVideo(c *gin.Context) {
var req PublishVideoRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
authorId, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
username, err := jwt.GetUsername(c)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
video := &Video{
@@ -54,7 +54,7 @@ func (vh *VideoHandler) PublishVideo(c *gin.Context) {
CreateTime: time.Now(),
}
if err := vh.service.Publish(c.Request.Context(), video); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
c.JSON(200, video)
@@ -194,16 +194,16 @@ func buildAbsoluteURL(c *gin.Context, p string) string {
func (vh *VideoHandler) DeleteVideo(c *gin.Context) {
var req DeleteVideoRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
authorId, err := jwt.GetAccountID(c)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if err := vh.service.Delete(c.Request.Context(), req.ID, authorId); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "video deleted"})
@@ -212,12 +212,12 @@ func (vh *VideoHandler) DeleteVideo(c *gin.Context) {
func (vh *VideoHandler) ListByAuthorID(c *gin.Context) {
var req ListByAuthorIDRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
videos, err := vh.service.ListByAuthorID(c.Request.Context(), req.AuthorID)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if videos == nil {
@@ -229,12 +229,12 @@ func (vh *VideoHandler) ListByAuthorID(c *gin.Context) {
func (vh *VideoHandler) GetDetail(c *gin.Context) {
var req GetDetailRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
video, err := vh.service.GetDetail(c.Request.Context(), req.ID)
if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
c.JSON(200, video)
@@ -243,11 +243,11 @@ func (vh *VideoHandler) GetDetail(c *gin.Context) {
func (vh *VideoHandler) UpdateLikesCount(c *gin.Context) {
var req UpdateLikesCountRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
if err := vh.service.UpdateLikesCount(c.Request.Context(), req.ID, req.LikesCount); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "likes count updated"})

View File

@@ -11,7 +11,7 @@ import (
"feedsystem_video_go/internal/middleware/rabbitmq"
rediscache "feedsystem_video_go/internal/middleware/redis"
httputil "feedsystem_video_go/internal/http"
"feedsystem_video_go/internal/apierror"
"gorm.io/gorm"
)
@@ -77,7 +77,7 @@ func (vs *VideoService) Delete(ctx context.Context, id uint, authorID uint) erro
return errors.New("video not found")
}
if video.AuthorID != authorID {
return httputil.ErrUnauthorized
return apierror.ErrUnauthorized
}
if err := vs.repo.DeleteVideo(ctx, id); err != nil {
return err