黑马头条第9天_18关注作者代码优化

课程 if else 嵌套太复杂优化,清爽开发代码如下:

    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //1.参数检查
        if (dto.getOperation() == null || dto.getOperation() < 0 || dto.getOperation() > 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getAuthorId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "作者信息不能为空");
        }

        //2.获取作者
        Integer followId = null;
        ApAuthor apAuthor = articleFeign.findById(dto.getAuthorId());
        if (apAuthor != null) {
            followId = apAuthor.getUserId();
        }
        if (followId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "关注人不存在");
        }

        ApUser apUser = AppThreadLocalUtils.getUser();

        if (apUser == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);

        if (dto.getOperation() == 0) {
            //3.如果当前操作是0 创建数据(app用户关注信息和app的用户粉丝信息)
            return followByUserId(apUser, followId, dto.getArticleId());
        }
        if (dto.getOperation() == 1) {
            //4.如果当前操作是1 删除数据(app用户关注信息和app的用户粉丝信息)
            return followCancelByUserId(apUser, followId);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

你可能感兴趣的:(黑马头条第9天_18关注作者代码优化)