查看探店笔记:
private void queryBlogUser(Blog blog) {
Long userId = blog.getUserId();
User user = userService.getById(userId);
blog.setName(user.getNickName());
blog.setIcon(user.getIcon());
}
@Override
public Result queryBlogById(Long id) {
//1.查询blog
Blog blog =getById(id);
if (blog == null) {
return Result.fail("笔记不存在!");
}
//2.查询blog有关的用户
queryBlogUser(blog);
return Result.ok(blog);
}
//设置blog的属性
private void queryBlogUser(Blog blog) {
Long userId = blog.getUserId();
User user = userService.getById(userId);
blog.setName(user.getNickName());
blog.setIcon(user.getIcon());
}
点赞功能:
需求:
实现步骤:
给Blog类中添加一个isLike字段,标示是否被当前用户点赞
/**
* 是否点赞过了
*/
@TableField(exist = false)
private Boolean isLike;
修改点赞功能,利用Redis的set集合判断是否点赞过,未点赞过则点赞数+1,已点赞过则点赞数-1
@Override
public Result likeBlog(Long id) {
//1.获取登录用户
Long userId = UserHolder.getUser().getId();
//2.判断当前登录用户是否已经点赞
Boolean isMember = stringRedisTemplate.opsForSet().isMember(BLOG_LIKED_KEY + id, userId.toString());
//3.如果未点赞,可以点赞
if (BooleanUtil.isFalse(isMember)){
//3.1.数据库点赞数 + 1
update().setSql("liked = liked + 1").eq("id", id).update();
//3.2.保存用户到Redis的set集合
stringRedisTemplate.opsForSet().add(BLOG_LIKED_KEY + id,userId.toString());
return Result.ok();
}
//4.如果已点赞,取消点赞
//4.1数据库点赞数 -1
update().setSql("liked = liked - 1").eq("id", id).update();
//4.2把用户从Redis的Set集合移除
stringRedisTemplate.opsForSet().remove(BLOG_LIKED_KEY + id,userId.toString());
return Result.ok();
}
修改根据id查询Blog的业务,判断当前登录用户是否点赞过,赋值isLike字段
@Override
public Result queryBlogById(Long id) {
//1.查询blog
Blog blog =getById(id);
if (blog == null) {
return Result.fail("笔记不存在!");
}
//2.查询blog有关的用户
queryBlogUser(blog);
//3.查询blog是否被点赞
isBlogLiked(blog);
return Result.ok(blog);
}
修改分页查询Blog业务,判断当前登录用户是否点赞过,赋值给isLike字段
@Override
public Result queryHotBlog(Integer current) {
// 根据用户查询
Page<Blog> page = query()
.orderByDesc("liked")
.page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
// 获取当前页数据
List<Blog> records = page.getRecords();
// 查询用户
records.forEach(blog -> {
//查询一页中blog所属用户
queryBlogUser(blog);
//查是否被当前用户点赞
isBlogLiked(blog);
});
return Result.ok(records);
}
判断该blog用户是否点赞
private void isBlogLiked(Blog blog) {
//1.获取登录用户
Long userId = UserHolder.getUser().getId();
//2.判断当前登录用户是否已经点赞
Boolean isMember = stringRedisTemplate.opsForSet().isMember(BLOG_LIKED_KEY + blog.getId(), userId.toString());
blog.setIsLike(BooleanUtil.isTrue(isMember));
}
点赞排行榜
需求1:把笔记点赞的人显示出来,比如最早点赞的TOP5,形成点赞排行榜
不同集合比较:
由于set集合不具有排序效果,所以采用SortedSet进行替换:
判断当前用户是否点赞过该笔记:
private void isBlogLiked(Blog blog) {
//1.获取登录用户
Long userId = UserHolder.getUser().getId();
//2.判断当前登录用户是否已经点赞
// Boolean isMember = stringRedisTemplate.opsForSet().isMember(BLOG_LIKED_KEY + blog.getId(), userId.toString());
Double score = stringRedisTemplate.opsForZSet().score(BLOG_LIKED_KEY + blog.getId(), userId.toString());
blog.setIsLike(score != null);
}
将该笔记点赞总数信息写入数据库与redis,score值为当前时间戳:
判断是否点赞,如果未点赞则可以点赞数据库点赞量+1,并且在redis增加点赞信息。如果已点赞,则取消点赞,并且在redis移除点赞信息。
@Override
public Result likeBlog(Long id) {
//1.获取登录用户
Long userId = UserHolder.getUser().getId();
//2.判断当前登录用户是否已经点赞
// Boolean isMember = stringRedisTemplate.opsForSet().isMember(BLOG_LIKED_KEY + id, userId.toString());
Double score = stringRedisTemplate.opsForZSet().score(BLOG_LIKED_KEY + id, userId.toString());
//3.如果未点赞,可以点赞
// if (BooleanUtil.isFalse(isMember)){
if (score == null) {
//3.1.数据库点赞数 + 1
update().setSql("liked = liked + 1").eq("id", id).update();
//3.2.保存用户到Redis的set集合
// stringRedisTemplate.opsForSet().add(BLOG_LIKED_KEY + id,userId.toString());
stringRedisTemplate.opsForZSet().add(BLOG_LIKED_KEY + id, userId.toString(), System.currentTimeMillis());
return Result.ok();
}
// }
//4.如果已点赞,取消点赞
//4.1数据库点赞数 -1
update().setSql("liked = liked - 1").eq("id", id).update();
//4.2把用户从Redis的Set集合移除
// stringRedisTemplate.opsForSet().remove(BLOG_LIKED_KEY + id,userId.toString());
stringRedisTemplate.opsForZSet().remove(BLOG_LIKED_KEY + id,userId.toString());
return Result.ok();
}
显示点赞最早的前5用户:
@Override
public Result queryBlogLikes(Long id) {
//1.查询top5的点赞用户zrange key 0 4
Set<String> top5 = stringRedisTemplate.opsForZSet().range(BLOG_LIKED_KEY + id, 0, 4);
if (top5==null || top5.isEmpty()){
return Result.ok(Collections.emptyList());
}
//2.解析出其中的用户id
List<Long> userIdList = top5.stream().map(Long::valueOf).collect(Collectors.toList());
//3.根据用户id查询用户
// List userDTOList = userService.listByIds(userIdList)
// 由于where id in (5,1) order by FIELD(id,5,1)
String ids = StrUtil.join(",", userIdList);
List<UserDTO> userDTOList = userService.query()
.in("id",userIdList).last("order by FIELD(id,"+ids+")").list()
.stream()
.map(user -> BeanUtil.copyProperties(user, UserDTO.class))
.collect(Collectors.toList());
//4.返回
return Result.ok(userDTOList);
}