mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
3.9 KiB
147 lines
3.9 KiB
2 years ago
|
// Copyright 2022 ROC. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
3 years ago
|
package api
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
|
"github.com/rocboss/paopao-ce/internal/servants/web/broker"
|
||
3 years ago
|
"github.com/rocboss/paopao-ce/pkg/app"
|
||
|
"github.com/rocboss/paopao-ce/pkg/convert"
|
||
|
"github.com/rocboss/paopao-ce/pkg/errcode"
|
||
2 years ago
|
"github.com/sirupsen/logrus"
|
||
3 years ago
|
)
|
||
|
|
||
|
func GetPostComments(c *gin.Context) {
|
||
|
postID := convert.StrTo(c.Query("id")).MustInt64()
|
||
|
response := app.NewResponse(c)
|
||
|
|
||
2 years ago
|
contents, totalRows, err := broker.GetPostComments(postID, "id ASC", 0, 0)
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
2 years ago
|
logrus.Errorf("service.GetPostComments err: %v\n", err)
|
||
3 years ago
|
response.ToErrorResponse(errcode.GetCommentsFailed)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.ToResponseList(contents, totalRows)
|
||
|
}
|
||
|
|
||
|
func CreatePostComment(c *gin.Context) {
|
||
2 years ago
|
param := broker.CommentCreationReq{}
|
||
3 years ago
|
response := app.NewResponse(c)
|
||
|
valid, errs := app.BindAndValid(c, ¶m)
|
||
|
if !valid {
|
||
2 years ago
|
logrus.Errorf("app.BindAndValid errs: %v", errs)
|
||
3 years ago
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
userID, _ := c.Get("UID")
|
||
2 years ago
|
comment, err := broker.CreatePostComment(c, userID.(int64), param)
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
if err == errcode.MaxCommentCount {
|
||
|
response.ToErrorResponse(errcode.MaxCommentCount)
|
||
|
} else {
|
||
2 years ago
|
logrus.Errorf("service.CreatePostComment err: %v\n", err)
|
||
3 years ago
|
response.ToErrorResponse(errcode.CreateCommentFailed)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.ToResponse(comment)
|
||
|
}
|
||
|
|
||
|
func DeletePostComment(c *gin.Context) {
|
||
2 years ago
|
param := broker.CommentDelReq{}
|
||
3 years ago
|
response := app.NewResponse(c)
|
||
|
valid, errs := app.BindAndValid(c, ¶m)
|
||
|
if !valid {
|
||
2 years ago
|
logrus.Errorf("app.BindAndValid errs: %v", errs)
|
||
3 years ago
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
|
||
|
return
|
||
|
}
|
||
|
user, _ := c.Get("USER")
|
||
|
|
||
2 years ago
|
comment, err := broker.GetPostComment(param.ID)
|
||
3 years ago
|
if err != nil {
|
||
2 years ago
|
logrus.Errorf("service.GetPostComment err: %v\n", err)
|
||
3 years ago
|
response.ToErrorResponse(errcode.GetCommentFailed)
|
||
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
if user.(*core.User).ID != comment.UserID && !user.(*core.User).IsAdmin {
|
||
3 years ago
|
response.ToErrorResponse(errcode.NoPermission)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 执行删除
|
||
2 years ago
|
err = broker.DeletePostComment(comment)
|
||
3 years ago
|
if err != nil {
|
||
2 years ago
|
logrus.Errorf("service.DeletePostComment err: %v\n", err)
|
||
3 years ago
|
response.ToErrorResponse(errcode.DeleteCommentFailed)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.ToResponse(nil)
|
||
|
}
|
||
|
|
||
|
func CreatePostCommentReply(c *gin.Context) {
|
||
2 years ago
|
param := broker.CommentReplyCreationReq{}
|
||
3 years ago
|
response := app.NewResponse(c)
|
||
|
valid, errs := app.BindAndValid(c, ¶m)
|
||
|
if !valid {
|
||
2 years ago
|
logrus.Errorf("app.BindAndValid errs: %v", errs)
|
||
3 years ago
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
|
||
|
return
|
||
|
}
|
||
|
user, _ := c.Get("USER")
|
||
|
|
||
2 years ago
|
comment, err := broker.CreatePostCommentReply(c, param.CommentID, param.Content, user.(*core.User).ID, param.AtUserID)
|
||
3 years ago
|
if err != nil {
|
||
2 years ago
|
logrus.Errorf("service.CreatePostCommentReply err: %v\n", err)
|
||
3 years ago
|
response.ToErrorResponse(errcode.CreateReplyFailed)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.ToResponse(comment)
|
||
|
}
|
||
|
|
||
|
func DeletePostCommentReply(c *gin.Context) {
|
||
2 years ago
|
param := broker.ReplyDelReq{}
|
||
3 years ago
|
response := app.NewResponse(c)
|
||
|
valid, errs := app.BindAndValid(c, ¶m)
|
||
|
if !valid {
|
||
2 years ago
|
logrus.Errorf("app.BindAndValid errs: %v", errs)
|
||
3 years ago
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
user, _ := c.Get("USER")
|
||
|
|
||
2 years ago
|
reply, err := broker.GetPostCommentReply(param.ID)
|
||
3 years ago
|
if err != nil {
|
||
2 years ago
|
logrus.Errorf("service.GetPostCommentReply err: %v\n", err)
|
||
3 years ago
|
response.ToErrorResponse(errcode.GetReplyFailed)
|
||
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
if user.(*core.User).ID != reply.UserID && !user.(*core.User).IsAdmin {
|
||
3 years ago
|
response.ToErrorResponse(errcode.NoPermission)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 执行删除
|
||
2 years ago
|
err = broker.DeletePostCommentReply(reply)
|
||
3 years ago
|
if err != nil {
|
||
2 years ago
|
logrus.Errorf("service.DeletePostCommentReply err: %v\n", err)
|
||
3 years ago
|
response.ToErrorResponse(errcode.DeleteCommentFailed)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.ToResponse(nil)
|
||
|
}
|