65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"juwan-backend/app/community/rpc/internal/models/postlikes"
|
|
"juwan-backend/app/community/rpc/internal/models/posts"
|
|
"juwan-backend/app/community/rpc/internal/svc"
|
|
"juwan-backend/app/community/rpc/pb"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type DelPostLikesLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewDelPostLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelPostLikesLogic {
|
|
return &DelPostLikesLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *DelPostLikesLogic) DelPostLikes(in *pb.DelPostLikesReq) (*pb.DelPostLikesResp, error) {
|
|
if in.GetId() <= 0 {
|
|
return nil, errors.New("id is required")
|
|
}
|
|
|
|
if in.UserId != nil && in.GetUserId() > 0 {
|
|
n, err := l.svcCtx.CommunityModelRW.PostLikes.Delete().
|
|
Where(postlikes.PostIDEQ(in.GetId()), postlikes.UserIDEQ(in.GetUserId())).
|
|
Exec(l.ctx)
|
|
if err != nil {
|
|
logx.Errorf("delPostLikes err: %v", err)
|
|
}
|
|
if n > 0 {
|
|
l.svcCtx.CommunityModelRW.Posts.Update().
|
|
Where(posts.IDEQ(in.GetId()), posts.LikeCountGT(0)).
|
|
AddLikeCount(-1).
|
|
Exec(l.ctx)
|
|
}
|
|
return &pb.DelPostLikesResp{}, nil
|
|
}
|
|
|
|
n, err := l.svcCtx.CommunityModelRW.PostLikes.Delete().
|
|
Where(postlikes.PostIDEQ(in.GetId())).
|
|
Exec(l.ctx)
|
|
if err != nil {
|
|
logx.Errorf("delPostLikes err: %v", err)
|
|
}
|
|
if n > 0 {
|
|
l.svcCtx.CommunityModelRW.Posts.Update().
|
|
Where(posts.IDEQ(in.GetId()), posts.LikeCountGTE(n)).
|
|
AddLikeCount(-n).
|
|
Exec(l.ctx)
|
|
}
|
|
|
|
return &pb.DelPostLikesResp{}, nil
|
|
}
|