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

65 lines
1.6 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 {
Target string `json:"target" validate:"required"`
UID string `json:"uid" validate:"required"`
LikeType domain.LikeType `json:"like_type" validate:"required,oneof=1 2"`
}
2024-08-28 09:09:01 +00:00
// Like 點讚/取消讚 貼文
2024-08-28 14:24:47 +00:00
func (l *LikeLogic) Like(in *tweeting.LikeReq) (*tweeting.OKResp, error) {
2024-08-29 13:26:01 +00:00
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&likeReq{
Target: in.TargetId,
UID: in.GetUid(),
LikeType: domain.LikeType(in.GetLikeType()),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
}
err := l.svcCtx.PostLikeModel.LikeDislike(l.ctx, &model.PostLikes{
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-28 14:24:47 +00:00
return &tweeting.OKResp{}, nil
2024-08-28 09:09:01 +00:00
}