app-cloudep-tweeting-service/internal/logic/commentservice/update_comment_logic.go

84 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-08-28 09:09:01 +00:00
package commentservicelogic
import (
2024-08-28 14:29:42 +00:00
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/domain"
model "app-cloudep-tweeting-service/internal/model/mongo"
2024-08-28 14:29:42 +00:00
"app-cloudep-tweeting-service/internal/svc"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"go.mongodb.org/mongo-driver/bson/primitive"
2024-08-28 09:09:01 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateCommentLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCommentLogic {
return &UpdateCommentLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
2024-09-03 09:47:34 +00:00
type checkCommentID struct {
CommentID string `validate:"required"`
Content string `json:"content,omitempty" validate:"lte=500"`
}
2024-08-28 09:09:01 +00:00
// UpdateComment 更新評論
2024-08-28 14:24:47 +00:00
func (l *UpdateCommentLogic) UpdateComment(in *tweeting.UpdateCommentReq) (*tweeting.OKResp, error) {
// 驗證資料
2024-09-03 09:47:34 +00:00
if err := l.svcCtx.Validate.ValidateAll(&checkCommentID{
CommentID: in.GetCommentId(),
Content: in.GetContent(),
}); err != nil {
// 錯誤代碼 05-011-00
return nil, ers.InvalidFormat(err.Error())
}
// 沒有就沒有,有就走全覆蓋
update := model.Comment{}
oid, err := primitive.ObjectIDFromHex(in.GetCommentId())
if err != nil {
// 錯誤代碼 05-011-00
return nil, ers.InvalidFormat("failed to get correct comment id")
}
update.ID = oid
update.Content = in.GetContent()
// 因為 0 也有意義,所以如果是真的沒帶進來,用 -1 帶進去表示不作動
if in.LikeCount == nil {
update.LikeCount = -1
} else {
update.LikeCount = in.GetLikeCount()
}
if in.DislikeCount == nil {
update.DisLikeCount = -1
} else {
update.DisLikeCount = in.GetDislikeCount()
}
_, err = l.svcCtx.CommentModel.UpdateOptional(l.ctx, &update)
if err != nil {
// 錯誤代碼 05-021-13
e := domain.CommentErrorL(
domain.CommentUpdateErrorCode,
logx.WithContext(l.ctx),
[]logx.LogField{
{Key: "req", Value: in},
{Key: "func", Value: "CommentModel.UpdateOptional"},
{Key: "err", Value: err},
},
"failed to update comment:", in.CommentId).Wrap(err)
2024-09-03 09:47:34 +00:00
return nil, e
}
2024-08-28 09:09:01 +00:00
2024-08-28 14:24:47 +00:00
return &tweeting.OKResp{}, nil
2024-08-28 09:09:01 +00:00
}