✨Feat: 获取含登录用户点赞信息的博客列表
This commit is contained in:
@@ -2,6 +2,7 @@ package cn.hezhaohui.thumb.controller;
|
||||
|
||||
import cn.hezhaohui.thumb.common.BaseResponse;
|
||||
import cn.hezhaohui.thumb.common.ResultUtils;
|
||||
import cn.hezhaohui.thumb.model.entity.Blog;
|
||||
import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||
import cn.hezhaohui.thumb.service.BlogService;
|
||||
import jakarta.annotation.Resource;
|
||||
@@ -10,6 +11,8 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("blog")
|
||||
public class BlogController {
|
||||
@@ -21,4 +24,11 @@ public class BlogController {
|
||||
BlogVO blogVO = blogService.getBlogVOById(blogId, request);
|
||||
return ResultUtils.sucess(blogVO);
|
||||
}
|
||||
|
||||
@GetMapping("list")
|
||||
public BaseResponse<List<BlogVO>> list(HttpServletRequest request) {
|
||||
List<Blog> blogList = blogService.list();
|
||||
List<BlogVO> blogVOList = blogService.getBlogVOList(blogList, request);
|
||||
return ResultUtils.sucess(blogVOList);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 23117
|
||||
* @description 针对表【blog】的数据库操作Service
|
||||
@@ -14,4 +16,6 @@ public interface BlogService extends IService<Blog> {
|
||||
|
||||
|
||||
BlogVO getBlogVOById(long blogId, HttpServletRequest request);
|
||||
|
||||
List<BlogVO> getBlogVOList(List<Blog> blogList, HttpServletRequest request);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||
import cn.hezhaohui.thumb.service.ThumbService;
|
||||
import cn.hezhaohui.thumb.service.UserService;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import cn.hezhaohui.thumb.model.entity.Blog;
|
||||
import cn.hezhaohui.thumb.service.BlogService;
|
||||
@@ -15,6 +16,12 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author 23117
|
||||
* @description 针对表【blog】的数据库操作Service实现
|
||||
@@ -42,6 +49,29 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
|
||||
return this.getBlogVO(blog, loginUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BlogVO> getBlogVOList(List<Blog> blogList, HttpServletRequest request) {
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
Map<Long, Boolean> blogIdHasThumbMap = new HashMap<>();
|
||||
if (ObjUtil.isNotEmpty(loginUser)) {
|
||||
// Blog Id
|
||||
Set<Long> blogIdSet = blogList.stream().map(Blog::getId).collect(Collectors.toSet());
|
||||
// Get Thumb-up Data
|
||||
List<Thumb> thumbList = thumbService.lambdaQuery()
|
||||
.eq(Thumb::getUserid, loginUser.getId())
|
||||
.in(Thumb::getBlogId, blogIdSet)
|
||||
.list();
|
||||
thumbList.forEach(blogThumb -> blogIdHasThumbMap.put(blogThumb.getBlogId(), true));
|
||||
}
|
||||
return blogList.stream()
|
||||
.map(blog -> {
|
||||
BlogVO blogVO = BeanUtil.copyProperties(blog, BlogVO.class);
|
||||
blogVO.setHasThumb(blogIdHasThumbMap.get(blog.getId()));
|
||||
return blogVO;
|
||||
})
|
||||
.toList();
|
||||
}
|
||||
|
||||
private BlogVO getBlogVO(Blog blog, User loginUser) {
|
||||
BlogVO blogVO = new BlogVO();
|
||||
BeanUtil.copyProperties(blog, blogVO);
|
||||
|
||||
Reference in New Issue
Block a user