基于Java得黑马头条项目------day03

文章详情前后端成形记

这个是zookeeper进行分布式主键的序列化自增,其实就算自动调用对应的api文档进行构建,个人觉得还是比较简单的,需要虚拟机,如果没有虚拟机直接用windows版本的zookeeper也可以,其实不影响的,本机运行zookeeper还是比较方便的

public enum ZkSequenceEnum {
AP_LIKES,AP_READ_BEHAVIOR,AP_COLLECTION,AP_USER_FOLLOW,AP_USER_FAN
}

使用zookeeper生成自增主键,手动维护,进行封装,上面的就是创建的表的枚举的类型,直接复制笔记里面的内容就可以了,生成分布式的原子性的自增主键类,每500秒,重试自增,封装zookeeper客户端

判断是否生成的主键序列

创建表和主键绑定的序列

设置zookeeper的初始化过程方法

封装客户得zookeeper主键,注入到Spring容器中

分布式主键生成策略,测试用例

成功通过,分布式自增的主键值

编写文章信息接口,接口是根据articleId来查询文章详细信息

   public ResponseResult getArticleInfo(Integer articleId) {
        if (articleId==null || articleId<1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Map<String,Object> data=new HashMap<>();
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectByArticleId(articleId);
        if (apArticleConfig==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }else if (!apArticleConfig.getIsDelete()){
            ApArticleContent apArticleContent = apArticleContentMapper.selectByArticleId(articleId);
            String content = ZipUtils.gunzip(apArticleContent.getContent());
            apArticleContent.setContent(content);
            data.put("data",content);
        }
        data.put("config",apArticleConfig);
        return ResponseResult.okResult(data);
    }

测试文章接口信息

3.2 文章关系接口

接口定义

 

下面是接口的实现类及其方法以及对应的测试结果信息如下

关于文章信息接口的实现

public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        //用户与设备不能同时为空
        if(user==null && dto.getEquipmentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //1,通过equipmentId或用户id查看行为实体  --->entryId
        Long userId = null;
        if(user!=null){
            userId = user.getId();
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.selectByUserIdOrEquipemntId(userId, dto.getEquipmentId());

        boolean isUnlike = false,isLike = false,isCollection = false,isFollow=false;
        String burst = BurstUtils.groudOne(apBehaviorEntry.getId());

        //2,通过entryId和articleId去查看收藏表,看是否有数据,有数据就说明已经收藏,否则就是没有收藏
        ApCollection apCollection = apCollectionMapper.selectForEntryId(burst, apBehaviorEntry.getId(), dto.getArticleId(), ApCollection.Type.ARTICLE.getCode());
        if(apCollection!=null){
            isCollection=true;
        }
        // 3,通过entryId和articleId去查看点赞表,看是否有数据,有数据就说明已经点赞,否则就是没有点赞
        ApLikesBehavior apLikesBehavior = apLikesBehaviorMapper.selectLastLike(burst, apBehaviorEntry.getId(), dto.getArticleId(), ApCollection.Type.ARTICLE.getCode());
        if(apLikesBehavior!=null && apLikesBehavior.getOperation() == ApLikesBehavior.Operation.LIKE.getCode()){
            isLike = true;
        }
        //4,通过entryId和articleId去查看不喜欢表,看是否有数据,有数据就说明不喜欢,否则就是喜欢
        ApUnlikesBehavior apUnlikesBehavior = apUnlikesBehaviorMapper.selectLastUnLike(apBehaviorEntry.getId(), dto.getArticleId());
        if(apUnlikesBehavior!=null && apUnlikesBehavior.getType()==ApUnlikesBehavior.Type.UNLIKE.getCode()){
            isUnlike = true;
        }

        //5,通过authorId去app的id  userId(app账号信息id)
        ApAuthor apAuthor = apAuthorMapper.selectById(dto.getAuthorId());
        //查看关注表,根据当前登录人的userId与作者的app账号id去查询,如果有数据,就说明已经关注,没有则说明没有关注
        if(user!=null && apAuthor!=null && apAuthor.getUserId()!=null){
            ApUserFollow apUserFollow = apUserFollowMapper.selectByFollowId(BurstUtils.groudOne(user.getId()), user.getId(), apAuthor.getUserId().intValue());
            if(apUserFollow!=null){
                isFollow=true;
            }
        }
        Map<String,Object> data = new HashMap<>();
        data.put("isfollow",isFollow);
        data.put("islike",isLike);
        data.put("isunlike",isUnlike);
        data.put("iscollection",isCollection);

        return ResponseResult.okResult(data);
    }

测试成功

接下来的接口是关注接口相关的测试,需要新搭建一个user工程

 

保存行为用户关注作者的的接口信息

    @Override
    @Async
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto) {
        int a = 1 / 0;
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null && dto.getEquipmentId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if (user != null) {
            userId = user.getId();
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.selectByUserIdOrEquipemntId(userId, dto.getEquipmentId());
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //保存行为
        ApFollowBehavior alb = new ApFollowBehavior();
        alb.setEntryId(apBehaviorEntry.getId());
        alb.setArticleId(dto.getArticleId());
        alb.setFollowId(dto.getFollowId());
        alb.setCreatedTime(new Date());
        int insert = apFollowBehaviorMapper.insert(alb);
        return ResponseResult.okResult(insert);
    }

用户关注和取消关注的接口信息

 public ResponseResult follow(UserRelationDto dto) {
        if(dto.getOperation()==null || dto.getOperation() < 0 || dto.getOperation()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"无效的operation参数");
        }

        //获取到followId
        Integer followId = dto.getUserId();
        if(followId==null && dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"followId或authorId不能同时为空");
        }else if (followId ==null){
            ApAuthor apAuthor = apAuthorMapper.selectById(dto.getAuthorId());
            if(apAuthor!=null){
                followId =  apAuthor.getUserId().intValue();
            }
        }
        if(followId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"关注人不存在");
        }else{
            //判断当前用户是否已经关注
            ApUser user = AppThreadLocalUtils.getUser();
            if(user!=null){
                if(dto.getOperation()==0){
                    //关注操作
                    //保存ap_user_follow  ap_user_fan  保存关注的行为
                    return followByUserId(user,followId,dto.getArticleId());
                }else{
                    //取消关注
                    //删除ap_user_follow  ap_user_fan
                    return followCancelByUserId(user,followId);

                }
            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }

        }


    }

接口信息测试通过

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值