2024-08-28 09:09:01 +00:00
|
|
|
package postservicelogic
|
|
|
|
|
|
|
|
import (
|
2024-08-30 07:08:43 +00:00
|
|
|
"app-cloudep-tweeting-service/internal/domain"
|
|
|
|
model "app-cloudep-tweeting-service/internal/model/mongo"
|
2024-08-28 09:09:01 +00:00
|
|
|
"context"
|
|
|
|
|
2024-08-30 07:08:43 +00:00
|
|
|
ers "code.30cm.net/digimon/library-go/errs"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
|
2024-08-28 14:29:42 +00:00
|
|
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
|
|
|
"app-cloudep-tweeting-service/internal/svc"
|
2024-08-28 09:09:01 +00:00
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UpdatePostLogic struct {
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
logx.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUpdatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePostLogic {
|
|
|
|
return &UpdatePostLogic{
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 09:47:34 +00:00
|
|
|
type checkPostID struct {
|
2024-08-30 07:08:43 +00:00
|
|
|
PostID string `validate:"required"`
|
|
|
|
Content string `json:"content,omitempty" validate:"lte=500"`
|
|
|
|
}
|
|
|
|
|
2024-08-28 09:09:01 +00:00
|
|
|
// UpdatePost 更新貼文
|
2024-08-28 14:24:47 +00:00
|
|
|
func (l *UpdatePostLogic) UpdatePost(in *tweeting.UpdatePostReq) (*tweeting.OKResp, error) {
|
2024-08-30 07:08:43 +00:00
|
|
|
// 驗證資料
|
2024-09-03 09:47:34 +00:00
|
|
|
if err := l.svcCtx.Validate.ValidateAll(&checkPostID{
|
2024-08-30 07:08:43 +00:00
|
|
|
PostID: in.GetPostId(),
|
|
|
|
Content: in.GetContent(),
|
|
|
|
}); err != nil {
|
|
|
|
// 錯誤代碼 05-011-00
|
|
|
|
return nil, ers.InvalidFormat(err.Error())
|
|
|
|
}
|
|
|
|
// 沒有就沒有,有就走全覆蓋
|
|
|
|
update := model.Post{}
|
|
|
|
oid, err := primitive.ObjectIDFromHex(in.GetPostId())
|
|
|
|
if err != nil {
|
|
|
|
// 錯誤代碼 05-011-00
|
|
|
|
return nil, ers.InvalidFormat("failed to get correct post id")
|
|
|
|
}
|
|
|
|
update.ID = oid
|
|
|
|
update.Tags = in.GetTags()
|
|
|
|
// 將 Media 存入
|
2024-09-03 09:47:34 +00:00
|
|
|
media := make([]model.Media, 0, len(in.GetMedia()))
|
2024-08-30 07:08:43 +00:00
|
|
|
for _, item := range in.GetMedia() {
|
|
|
|
media = append(media, model.Media{
|
|
|
|
Links: item.Url,
|
|
|
|
Type: item.Type,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
update.MediaURL = media
|
|
|
|
update.Content = in.GetContent()
|
|
|
|
|
|
|
|
// 因為 0 也有意義,所以如果是真的沒帶進來,用 -1 帶進去表示不作動
|
|
|
|
if in.LikeCount == nil {
|
|
|
|
update.Like = -1
|
|
|
|
} else {
|
|
|
|
update.Like = in.GetLikeCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
if in.DislikeCount == nil {
|
|
|
|
update.DisLike = -1
|
|
|
|
} else {
|
|
|
|
update.DisLike = in.GetDislikeCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = l.svcCtx.PostModel.UpdateOptional(l.ctx, &update)
|
|
|
|
if err != nil {
|
|
|
|
// 錯誤代碼 05-021-04
|
|
|
|
e := domain.CommentErrorL(
|
|
|
|
domain.UpdatePostError,
|
|
|
|
logx.WithContext(l.ctx),
|
|
|
|
[]logx.LogField{
|
|
|
|
{Key: "req", Value: in},
|
|
|
|
{Key: "func", Value: "PostModel.UpdateOptional"},
|
|
|
|
{Key: "err", Value: err},
|
|
|
|
},
|
|
|
|
"failed to update post", in.PostId).Wrap(err)
|
2024-09-03 09:47:34 +00:00
|
|
|
|
2024-08-30 07:08:43 +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
|
|
|
}
|