diff --git a/handler/admin/photo.go b/handler/admin/photo.go index e213ce6..1b0488b 100644 --- a/handler/admin/photo.go +++ b/handler/admin/photo.go @@ -122,11 +122,3 @@ func (p *PhotoHandler) DeletePhoto(ctx *gin.Context) (interface{}, error) { func (p *PhotoHandler) ListPhotoTeams(ctx *gin.Context) (interface{}, error) { return p.PhotoService.ListTeams(ctx) } - -func (p *PhotoHandler) IncreasePhotoLike(ctx *gin.Context) (interface{}, error) { - id, err := util.ParamInt32(ctx, "id") - if err != nil { - return nil, err - } - return nil, p.PhotoService.IncreaseLike(ctx, id) -} diff --git a/handler/admin/post.go b/handler/admin/post.go index 35a13a9..5d8f883 100644 --- a/handler/admin/post.go +++ b/handler/admin/post.go @@ -150,14 +150,6 @@ func (p *PostHandler) GetByPostID(ctx *gin.Context) (interface{}, error) { return postDetailVO, nil } -func (p *PostHandler) LikePost(ctx *gin.Context) (interface{}, error) { - postID, err := util.ParamInt32(ctx, "postID") - if err != nil { - return nil, err - } - return nil, p.PostService.IncreaseLike(ctx, postID) -} - func (p *PostHandler) CreatePost(ctx *gin.Context) (interface{}, error) { var postParam param.Post err := ctx.ShouldBindJSON(&postParam) diff --git a/handler/content/api/init.go b/handler/content/api/init.go index d0f4ec6..1890ecc 100644 --- a/handler/content/api/init.go +++ b/handler/content/api/init.go @@ -11,5 +11,6 @@ func init() { NewPostHandler, NewSheetHandler, NewOptionHandler, + NewPhotoHandler, ) } diff --git a/handler/content/api/photo.go b/handler/content/api/photo.go new file mode 100644 index 0000000..cd4cf70 --- /dev/null +++ b/handler/content/api/photo.go @@ -0,0 +1,26 @@ +package api + +import ( + "github.com/gin-gonic/gin" + + "github.com/go-sonic/sonic/service" + "github.com/go-sonic/sonic/util" +) + +type PhotoHandler struct { + PhotoService service.PhotoService +} + +func NewPhotoHandler(photoService service.PhotoService) *PhotoHandler { + return &PhotoHandler{ + PhotoService: photoService, + } +} + +func (p *PhotoHandler) Like(ctx *gin.Context) (interface{}, error) { + id, err := util.ParamInt32(ctx, "photoID") + if err != nil { + return nil, err + } + return nil, p.PhotoService.IncreaseLike(ctx, id) +} diff --git a/handler/content/api/post.go b/handler/content/api/post.go index 824ec17..e14abbc 100644 --- a/handler/content/api/post.go +++ b/handler/content/api/post.go @@ -182,9 +182,5 @@ func (p *PostHandler) Like(ctx *gin.Context) (interface{}, error) { if err != nil { return nil, err } - err = p.PostService.IncreaseLike(ctx, postID) - if err != nil { - return nil, err - } - return nil, err + return nil, p.PostService.IncreaseLike(ctx, postID) } diff --git a/handler/router.go b/handler/router.go index c555a39..808c6a2 100644 --- a/handler/router.go +++ b/handler/router.go @@ -103,7 +103,6 @@ func (s *Server) RegisterRouters() { postRouter.GET("/latest", s.wrapHandler(s.PostHandler.ListLatestPosts)) postRouter.GET("/status/:status", s.wrapHandler(s.PostHandler.ListPostsByStatus)) postRouter.GET("/:postID", s.wrapHandler(s.PostHandler.GetByPostID)) - postRouter.PUT("/:postID/likes", s.wrapHandler(s.PostHandler.LikePost)) postRouter.POST("", s.wrapHandler(s.PostHandler.CreatePost)) postRouter.PUT("/:postID", s.wrapHandler(s.PostHandler.UpdatePost)) postRouter.PUT("/:postID/status/:status", s.wrapHandler(s.PostHandler.UpdatePostStatus)) @@ -230,7 +229,6 @@ func (s *Server) RegisterRouters() { photoRouter.DELETE("/:id", s.wrapHandler(s.PhotoHandler.DeletePhoto)) photoRouter.POST("", s.wrapHandler(s.PhotoHandler.CreatePhoto)) photoRouter.PUT("/:id", s.wrapHandler(s.PhotoHandler.UpdatePhoto)) - photoRouter.POST("/:id/likes", s.wrapHandler(s.PhotoHandler.IncreasePhotoLike)) photoRouter.GET("/teams", s.wrapHandler(s.PhotoHandler.ListPhotoTeams)) } { @@ -327,11 +325,14 @@ func (s *Server) RegisterRouters() { contentAPIRouter.GET("/journals/:journalID/comments/list_view", s.wrapHandler(s.ContentAPIJournalHandler.ListComment)) contentAPIRouter.POST("/journals/comments", s.wrapHandler(s.ContentAPIJournalHandler.CreateComment)) + contentAPIRouter.POST("/photos/:photoID/likes", s.wrapHandler(s.ContentAPIPhotoHandler.Like)) + contentAPIRouter.GET("/posts/:postID/comments/top_view", s.wrapHandler(s.ContentAPIPostHandler.ListTopComment)) contentAPIRouter.GET("/posts/:postID/comments/:parentID/children", s.wrapHandler(s.ContentAPIPostHandler.ListChildren)) contentAPIRouter.GET("/posts/:postID/comments/tree_view", s.wrapHandler(s.ContentAPIPostHandler.ListCommentTree)) contentAPIRouter.GET("/posts/:postID/comments/list_view", s.wrapHandler(s.ContentAPIPostHandler.ListComment)) contentAPIRouter.POST("/posts/comments", s.wrapHandler(s.ContentAPIPostHandler.CreateComment)) + contentAPIRouter.POST("/posts/:postID/likes", s.wrapHandler(s.ContentAPIPostHandler.Like)) contentAPIRouter.GET("/sheets/:sheetID/comments/top_view", s.wrapHandler(s.ContentAPISheetHandler.ListTopComment)) contentAPIRouter.GET("/sheets/:sheetID/comments/:parentID/children", s.wrapHandler(s.ContentAPISheetHandler.ListChildren)) diff --git a/handler/server.go b/handler/server.go index b1c4de1..24e2dfc 100644 --- a/handler/server.go +++ b/handler/server.go @@ -76,6 +76,7 @@ type Server struct { ContentAPIPostHandler *api.PostHandler ContentAPISheetHandler *api.SheetHandler ContentAPIOptionHandler *api.OptionHandler + ContentAPIPhotoHandler *api.PhotoHandler } type ServerParams struct { @@ -130,6 +131,7 @@ type ServerParams struct { ContentAPIPostHandler *api.PostHandler ContentAPISheetHandler *api.SheetHandler ContentAPIOptionHandler *api.OptionHandler + ContentAPIPhotoHandler *api.PhotoHandler } func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server { @@ -194,6 +196,7 @@ func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server { ContentAPISheetHandler: param.ContentAPISheetHandler, ContentAPIOptionHandler: param.ContentAPIOptionHandler, ContentSearchHandler: param.ContentSearchHandler, + ContentAPIPhotoHandler: param.ContentAPIPhotoHandler, } lifecycle.Append(fx.Hook{ OnStop: httpServer.Shutdown, diff --git a/service/impl/attachment.go b/service/impl/attachment.go index bc452e9..8faf863 100644 --- a/service/impl/attachment.go +++ b/service/impl/attachment.go @@ -147,7 +147,7 @@ func (a *attachmentServiceImpl) Upload(ctx context.Context, fileHeader *multipar WithStatus(xerr.StatusBadRequest). WithMsg("附件路径为 " + attachmentDTO.Path + " 已经存在") } - if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { + if !errors.Is(err, gorm.ErrRecordNotFound) { return nil, WrapDBErr(err) } attachmentEntity := &entity.Attachment{