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

@@ -3,7 +3,7 @@ package account
import ( import (
"errors" "errors"
httputil "feedsystem_video_go/internal/http" "feedsystem_video_go/internal/apierror"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/gorm" "gorm.io/gorm"
@@ -19,7 +19,7 @@ func NewAccountHandler(accountService *AccountService) *AccountHandler {
func (h *AccountHandler) CreateAccount(c *gin.Context) { func (h *AccountHandler) CreateAccount(c *gin.Context) {
var req CreateAccountRequest var req CreateAccountRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if err := h.accountService.CreateAccount(c.Request.Context(), &Account{ if err := h.accountService.CreateAccount(c.Request.Context(), &Account{
@@ -35,18 +35,18 @@ func (h *AccountHandler) CreateAccount(c *gin.Context) {
func (h *AccountHandler) Rename(c *gin.Context) { func (h *AccountHandler) Rename(c *gin.Context) {
var req RenameRequest var req RenameRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
accountID, err := getAccountID(c) accountID, err := getAccountID(c)
if err != nil { if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()}) c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return return
} }
token, err := h.accountService.Rename(c.Request.Context(), accountID, req.NewUsername) token, err := h.accountService.Rename(c.Request.Context(), accountID, req.NewUsername)
if err != nil { if err != nil {
if errors.Is(err, ErrNewUsernameRequired) { if errors.Is(err, ErrNewUsernameRequired) {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()}) c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return return
} }
if errors.Is(err, ErrUsernameTaken) { if errors.Is(err, ErrUsernameTaken) {
@@ -66,7 +66,7 @@ func (h *AccountHandler) Rename(c *gin.Context) {
func (h *AccountHandler) ChangePassword(c *gin.Context) { func (h *AccountHandler) ChangePassword(c *gin.Context) {
var req ChangePasswordRequest var req ChangePasswordRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if err := h.accountService.ChangePassword(c.Request.Context(), req.Username, req.OldPassword, req.NewPassword); err != nil { if err := h.accountService.ChangePassword(c.Request.Context(), req.Username, req.OldPassword, req.NewPassword); err != nil {
@@ -79,7 +79,7 @@ func (h *AccountHandler) ChangePassword(c *gin.Context) {
func (h *AccountHandler) FindByID(c *gin.Context) { func (h *AccountHandler) FindByID(c *gin.Context) {
var req FindByIDRequest var req FindByIDRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if account, err := h.accountService.FindByID(c.Request.Context(), req.ID); err != nil { if account, err := h.accountService.FindByID(c.Request.Context(), req.ID); err != nil {
@@ -93,7 +93,7 @@ func (h *AccountHandler) FindByID(c *gin.Context) {
func (h *AccountHandler) FindByUsername(c *gin.Context) { func (h *AccountHandler) FindByUsername(c *gin.Context) {
var req FindByUsernameRequest var req FindByUsernameRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if account, err := h.accountService.FindByUsername(c.Request.Context(), req.Username); err != nil { if account, err := h.accountService.FindByUsername(c.Request.Context(), req.Username); err != nil {
@@ -107,7 +107,7 @@ func (h *AccountHandler) FindByUsername(c *gin.Context) {
func (h *AccountHandler) Login(c *gin.Context) { func (h *AccountHandler) Login(c *gin.Context) {
var req LoginRequest var req LoginRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if token, err := h.accountService.Login(c.Request.Context(), req.Username, req.Password); err != nil { if token, err := h.accountService.Login(c.Request.Context(), req.Username, req.Password); err != nil {
@@ -121,7 +121,7 @@ func (h *AccountHandler) Login(c *gin.Context) {
func (h *AccountHandler) Logout(c *gin.Context) { func (h *AccountHandler) Logout(c *gin.Context) {
accountID, err := getAccountID(c) accountID, err := getAccountID(c)
if err != nil { if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()}) c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return return
} }
if err := h.accountService.Logout(c.Request.Context(), accountID); err != nil { if err := h.accountService.Logout(c.Request.Context(), accountID); err != nil {

View File

@@ -1,4 +1,4 @@
package http package apierror
import ( import (
"errors" "errors"

View File

@@ -2,7 +2,7 @@ package feed
import ( import (
"feedsystem_video_go/internal/middleware/jwt" "feedsystem_video_go/internal/middleware/jwt"
httputil "feedsystem_video_go/internal/http" "feedsystem_video_go/internal/apierror"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -19,7 +19,7 @@ func NewFeedHandler(service *FeedService) *FeedHandler {
func (f *FeedHandler) ListLatest(c *gin.Context) { func (f *FeedHandler) ListLatest(c *gin.Context) {
var req ListLatestRequest var req ListLatestRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if req.Limit <= 0 || req.Limit > 50 { if req.Limit <= 0 || req.Limit > 50 {
@@ -45,7 +45,7 @@ func (f *FeedHandler) ListLatest(c *gin.Context) {
func (f *FeedHandler) ListLikesCount(c *gin.Context) { func (f *FeedHandler) ListLikesCount(c *gin.Context) {
var req ListLikesCountRequest var req ListLikesCountRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if req.Limit <= 0 || req.Limit > 50 { if req.Limit <= 0 || req.Limit > 50 {
@@ -94,7 +94,7 @@ func (f *FeedHandler) ListLikesCount(c *gin.Context) {
func (f *FeedHandler) ListByFollowing(c *gin.Context) { func (f *FeedHandler) ListByFollowing(c *gin.Context) {
var req ListByFollowingRequest var req ListByFollowingRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if req.Limit <= 0 || req.Limit > 50 { if req.Limit <= 0 || req.Limit > 50 {
@@ -120,7 +120,7 @@ func (f *FeedHandler) ListByFollowing(c *gin.Context) {
func (f *FeedHandler) ListByPopularity(c *gin.Context) { func (f *FeedHandler) ListByPopularity(c *gin.Context) {
var req ListByPopularityRequest var req ListByPopularityRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if req.Limit <= 0 || req.Limit > 50 { if req.Limit <= 0 || req.Limit > 50 {

View File

@@ -2,7 +2,7 @@ package social
import ( import (
"feedsystem_video_go/internal/account" "feedsystem_video_go/internal/account"
httputil "feedsystem_video_go/internal/http" "feedsystem_video_go/internal/apierror"
"feedsystem_video_go/internal/middleware/jwt" "feedsystem_video_go/internal/middleware/jwt"
"net/http" "net/http"
@@ -20,7 +20,7 @@ func NewSocialHandler(service *SocialService) *SocialHandler {
func (h *SocialHandler) Follow(c *gin.Context) { func (h *SocialHandler) Follow(c *gin.Context) {
var req FollowRequest var req FollowRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if req.VloggerID <= 0 { if req.VloggerID <= 0 {
@@ -37,7 +37,7 @@ func (h *SocialHandler) Follow(c *gin.Context) {
VloggerID: req.VloggerID, VloggerID: req.VloggerID,
} }
if err := h.service.Follow(c.Request.Context(), social); err != nil { if err := h.service.Follow(c.Request.Context(), social); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()}) c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return return
} }
c.JSON(http.StatusOK, gin.H{"message": "followed"}) c.JSON(http.StatusOK, gin.H{"message": "followed"})
@@ -46,7 +46,7 @@ func (h *SocialHandler) Follow(c *gin.Context) {
func (h *SocialHandler) Unfollow(c *gin.Context) { func (h *SocialHandler) Unfollow(c *gin.Context) {
var req UnfollowRequest var req UnfollowRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
if req.VloggerID <= 0 { if req.VloggerID <= 0 {
@@ -63,7 +63,7 @@ func (h *SocialHandler) Unfollow(c *gin.Context) {
VloggerID: req.VloggerID, VloggerID: req.VloggerID,
} }
if err := h.service.Unfollow(c.Request.Context(), social); err != nil { if err := h.service.Unfollow(c.Request.Context(), social); err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()}) c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return return
} }
c.JSON(http.StatusOK, gin.H{"message": "unfollowed"}) c.JSON(http.StatusOK, gin.H{"message": "unfollowed"})
@@ -72,7 +72,7 @@ func (h *SocialHandler) Unfollow(c *gin.Context) {
func (h *SocialHandler) GetAllFollowers(c *gin.Context) { func (h *SocialHandler) GetAllFollowers(c *gin.Context) {
var req GetAllFollowersRequest var req GetAllFollowersRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
@@ -88,7 +88,7 @@ func (h *SocialHandler) GetAllFollowers(c *gin.Context) {
followers, err := h.service.GetAllFollowers(c.Request.Context(), vloggerID) followers, err := h.service.GetAllFollowers(c.Request.Context(), vloggerID)
if err != nil { if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()}) c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return return
} }
if followers == nil { if followers == nil {
@@ -100,7 +100,7 @@ func (h *SocialHandler) GetAllFollowers(c *gin.Context) {
func (h *SocialHandler) GetAllVloggers(c *gin.Context) { func (h *SocialHandler) GetAllVloggers(c *gin.Context) {
var req GetAllVloggersRequest var req GetAllVloggersRequest
if err := c.ShouldBindJSON(&req); err != nil { 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 return
} }
@@ -116,7 +116,7 @@ func (h *SocialHandler) GetAllVloggers(c *gin.Context) {
vloggers, err := h.service.GetAllVloggers(c.Request.Context(), followerID) vloggers, err := h.service.GetAllVloggers(c.Request.Context(), followerID)
if err != nil { if err != nil {
c.JSON(httputil.ClassifyHTTPStatus(err), gin.H{"error": err.Error()}) c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
return return
} }
if vloggers == nil { if vloggers == nil {

View File

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

View File

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

View File

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

View File

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

View File

@@ -11,7 +11,7 @@ import (
"feedsystem_video_go/internal/middleware/rabbitmq" "feedsystem_video_go/internal/middleware/rabbitmq"
rediscache "feedsystem_video_go/internal/middleware/redis" rediscache "feedsystem_video_go/internal/middleware/redis"
httputil "feedsystem_video_go/internal/http" "feedsystem_video_go/internal/apierror"
"gorm.io/gorm" "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") return errors.New("video not found")
} }
if video.AuthorID != authorID { if video.AuthorID != authorID {
return httputil.ErrUnauthorized return apierror.ErrUnauthorized
} }
if err := vs.repo.DeleteVideo(ctx, id); err != nil { if err := vs.repo.DeleteVideo(ctx, id); err != nil {
return err return err