java+mysql简单实现点赞评论转发帖子

mysql表设计:
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

java代码

接口

  /**
     * 更新帖子点赞数 收藏数 同时更新中间表
     * @param topic
     * @param userId
     * @param type
     */
    void updateByTopic(Topic topic, Long userId, String type);

实现类:

 @Override
    public void updateByTopic(Topic topic, Long userId, String type) {
        TopicMiddleModel model = new TopicMiddleModel();
        model.setTopicId(topic.getId());
        model.setUserId(userId);
        List<TopicMiddle> topicMiddles = topicMiddleSV.selectByModel(model);
        TopicMiddle topicMiddle = new TopicMiddle();
        if(topicMiddles != null && !topicMiddles.isEmpty()){
            topicMiddle = topicMiddles.get(0);
        }

    if("01".equals(type)){
        if(FlagEnum.IS.stringValue().equals(topicMiddle.getGoodFlag())){
            throw new BusinessException("该文章已经点过赞了");
        }
        topic.setGoodNum(topic.getGoodNum()+1);
        topicMiddle.setGoodFlag(FlagEnum.IS.stringValue());
    }else if("02".equals(type)){
        topic.setForwardNum(topic.getForwardNum()+1);
    }else if("03".equals(type)){
        if(FlagEnum.IS.stringValue().equals(topicMiddle.getCollect())){
            throw new BusinessException("该文章已经被收藏过了");
        }
        topic.setColNum(topic.getColNum()+1);
        topicMiddle.setCollect(FlagEnum.IS.stringValue());
    }else if ("04".equals(type)){
        if(FlagEnum.NO.stringValue().equals(topicMiddle.getGoodFlag())){
            throw new BusinessException("该文章还没有点过赞");
        }
        topic.setGoodNum(topic.getGoodNum()-1);
        topicMiddle.setGoodFlag(FlagEnum.NO.stringValue());
    }else if ("05".equals(type)){
        if((FlagEnum.NO.stringValue().equals(topicMiddle.getCollect()))){
            throw new BusinessException("该文章还没有被收藏过");
        }
        topic.setColNum(topic.getColNum()-1);
        topicMiddle.setCollect(FlagEnum.NO.stringValue());
    }else {
        throw new BusinessException("请传入01-06 有效值");
    }
    updateByIdSelective(topic);
    if(!"02".equals(type)){
        if(topicMiddles.isEmpty()){
            topicMiddle.setUserId(userId);
            topicMiddle.setTopicId(topic.getId());
            topicMiddleSV.insertSelective(topicMiddle);
        }else {
            topicMiddleSV.updateByIdSelective(topicMiddle);
        }
    }
}
  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值