package logic import ( "context" "errors" "juwan-backend/app/community/rpc/internal/svc" "juwan-backend/app/community/rpc/pb" "github.com/zeromicro/go-zero/core/logx" ) type AddPostLikesLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewAddPostLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddPostLikesLogic { return &AddPostLikesLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } // -----------------------postLikes----------------------- func (l *AddPostLikesLogic) AddPostLikes(in *pb.AddPostLikesReq) (*pb.AddPostLikesResp, error) { // todo: add your logic here and delete this line if in.GetPostId() <= 0 || in.GetUserId() <= 0 { return nil, errors.New("postId and userId are required") } store := l.svcCtx.Store store.Mu.Lock() defer store.Mu.Unlock() post, ok := store.Posts[in.GetPostId()] if !ok || post.GetDeletedAt() > 0 { return nil, errors.New("post not found") } key := postLikeKey(in.GetPostId(), in.GetUserId()) if _, exists := store.PostLikes[key]; exists { return &pb.AddPostLikesResp{}, nil } store.PostLikes[key] = &pb.PostLikes{ PostId: in.GetPostId(), UserId: in.GetUserId(), CreatedAt: nowUnix(in.GetCreatedAt()), } post.LikeCount++ return &pb.AddPostLikesResp{}, nil }