当我们进入到笔记详情页面时,会发送一个请求,判断当前登录用户是否关注了笔记博主
请求网址: http://localhost:8080/api/follow/or/not/2
请求方法: GET
当我们点击关注按钮时,会发送一个请求,实现关注/取关
请求网址: http://localhost:8080/api/follow/2/true
请求方法: PUT
关注是User之间的关系,是博主与粉丝的关系,数据库中有一张tb_follow表来标示
Field | Type | Collation | Null | Key | Default | Extra | Comment |
---|---|---|---|---|---|---|---|
id | bigint | (NULL) | NO | PRI | (NULL) | auto_increment | 主键 |
user_id | bigint unsigned | (NULL) | NO | (NULL) | 用户id | ||
follow_user_id | bigint unsigned | (NULL) | NO | (NULL) | 关联的用户id | ||
create_time | timestamp | (NULL) | NO | CURRENT_TIMESTAMP | DEFAULT_GENERATED | 创建时间 |
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tb_follow")
public class Follow implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 用户id
*/
private Long userId;
/**
* 关联的用户id
*/
private Long followUserId;
/**
* 创建时间
*/
private LocalDateTime createTime;
}
那我们现在来Controller层中编写对应的两个方法
@RestController
@RequestMapping("/follow")
public class FollowController {
@Resource
private IFollowService followService;
//判断当前用户是否关注了该博主
@GetMapping("/or/not/{id}")
public Result isFollow(@PathVariable("id") Long followUserId) {
return followService.isFollow(followUserId);
}
//实现取关/关注
@PutMapping("/{id}/{isFollow}")
public Result follow(@PathVariable("id") Long followUserId, @PathVariable("isFollow") Boolean isFellow) {
return followService.follow(followUserId,isFellow);
}
}
具体的业务逻辑我们还是放在FellowServiceImpl中来编写
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
@Override
public Result isFollow(Long followUserId) {
//获取当前登录的userId
Long userId = UserHolder.getUser().getId();
LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<>();
//查询当前用户是否关注了该笔记的博主
queryWrapper.eq(Follow::getUserId, userId).eq(Follow::getFollowUserId, followUserId);
//只查询一个count就行了
int count = this.count(queryWrapper);
return Result.ok(count > 0);
}
@Override
public Result follow(Long followUserId, Boolean isFellow) {
//获取当前用户id
Long userId = UserHolder.getUser().getId();
//判断是否关注
if (isFellow) {
//关注,则将信息保存到数据库
Follow follow = new Follow();
follow.setUserId(userId);
follow.setFollowUserId(followUserId);
save(follow);
} else {
//取关,则将数据从数据库中移除
LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Follow::getUserId, userId).eq(Follow::getFollowUserId, followUserId);
remove(queryWrapper);
}
return Result.ok();
}
}
点击用户头像,进入到用户详情页,可以查看用户发布的笔记,和共同关注列表
但现在我们还没写具体的业务逻辑,所以现在暂时看不到数据
检测NetWork选项卡,查看发送的请求
查询用户信息
请求网址: http://localhost:8080/api/user/2
请求方法: GET
查看共同关注
请求网址: http://localhost:8080/api/follow/common/undefined
请求方法: GET
编写查询用户信息
方法
@GetMapping("/{id}")
public Result queryById(@PathVariable("id") Long userId) {
// 查询详情
User user = userService.getById(userId);
if (user == null) {
// 没有详情,应该是第一次查看详情
return Result.ok();
}
UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
// 返回
return Result.ok(userDTO);
}
重启服务器,现在可以看到用户信息,但是不能看到用户发布的笔记信息,查看NetWork检测的请求,我们还需要完成这个需求
请求网址: http://localhost:8080/api/blog/of/user?&id=2¤t=1
请求方法: GET
编写查询用户笔记
方法