app-cloudep-tweeting-service/internal/logic/postservice/like_logic.go

69 lines
1.7 KiB
Go
Raw Normal View History

2024-08-28 09:09:01 +00:00
package postservicelogic
import (
2024-08-29 13:26:01 +00:00
"app-cloudep-tweeting-service/internal/domain"
model "app-cloudep-tweeting-service/internal/model/mongo"
ers "code.30cm.net/digimon/library-go/errs"
2024-08-28 09:09:01 +00:00
"context"
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 LikeLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeLogic {
return &LikeLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
2024-08-29 13:26:01 +00:00
type likeReq struct {
2024-08-29 14:49:42 +00:00
Target string `json:"target" validate:"required"`
UID string `json:"uid" validate:"required"`
2024-08-29 13:26:01 +00:00
}
2024-08-28 09:09:01 +00:00
// Like 點讚/取消讚 貼文
2024-08-29 14:49:42 +00:00
func (l *LikeLogic) Like(in *tweeting.LikeReq) (*tweeting.PostReactionActionResp, error) {
2024-08-29 13:26:01 +00:00
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&likeReq{
2024-08-29 14:49:42 +00:00
Target: in.TargetId,
UID: in.GetUid(),
2024-08-29 13:26:01 +00:00
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
}
2024-08-29 14:49:42 +00:00
likeResp, err := l.svcCtx.PostLikeModel.LikeDislike(l.ctx, &model.PostLikes{
2024-08-29 13:26:01 +00:00
TargetID: in.GetTargetId(),
UID: in.GetUid(),
Type: int8(in.GetLikeType()),
})
if err != nil {
e := domain.PostMongoErrorL(
logx.WithContext(l.ctx),
[]logx.LogField{
{Key: "req", Value: in},
{Key: "func", Value: "PostModel.LikeDislike"},
{Key: "err", Value: err},
},
"failed to like or dislike").Wrap(err)
return nil, e
}
2024-08-28 09:09:01 +00:00
2024-08-29 14:49:42 +00:00
// 將文章的數量增加或減少的功能是業務邏輯從外面再決定要不要丟MQ 後算,或是怎麼算
return &tweeting.PostReactionActionResp{
PostID: likeResp.PostID,
ReactionType: int64(likeResp.ReactionType),
IsIncrement: likeResp.IsIncrement,
}, nil
2024-08-28 09:09:01 +00:00
}