✨Feat: 根据 id 获取博客
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package cn.hezhaohui.thumb.controller;
|
||||
|
||||
import cn.hezhaohui.thumb.common.BaseResponse;
|
||||
import cn.hezhaohui.thumb.common.ResultUtils;
|
||||
import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||
import cn.hezhaohui.thumb.service.BlogService;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("blog")
|
||||
public class BlogController {
|
||||
@Resource
|
||||
private BlogService blogService;
|
||||
|
||||
@GetMapping("/get")
|
||||
public BaseResponse<BlogVO> get(long blogId, HttpServletRequest request) {
|
||||
BlogVO blogVO = blogService.getBlogVOById(blogId, request);
|
||||
return ResultUtils.sucess(blogVO);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package cn.hezhaohui.thumb.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class BlogVO {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String coverImg;
|
||||
|
||||
private String content;
|
||||
|
||||
private Integer thumbCount;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
private Boolean hasThumb;
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package cn.hezhaohui.thumb.service;
|
||||
|
||||
import cn.hezhaohui.thumb.model.entity.Blog;
|
||||
import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author 23117
|
||||
@@ -10,4 +12,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
*/
|
||||
public interface BlogService extends IService<Blog> {
|
||||
|
||||
|
||||
BlogVO getBlogVOById(long blogId, HttpServletRequest request);
|
||||
}
|
||||
|
||||
@@ -11,5 +11,5 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
*/
|
||||
public interface UserService extends IService<User> {
|
||||
|
||||
public User getLoginUser(HttpServletRequest request);
|
||||
User getLoginUser(HttpServletRequest request);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
package cn.hezhaohui.thumb.service.impl;
|
||||
|
||||
import cn.hezhaohui.thumb.model.entity.Thumb;
|
||||
import cn.hezhaohui.thumb.model.entity.User;
|
||||
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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import cn.hezhaohui.thumb.model.entity.Blog;
|
||||
import cn.hezhaohui.thumb.service.BlogService;
|
||||
import cn.hezhaohui.thumb.mapper.BlogMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@@ -15,6 +24,40 @@ import org.springframework.stereotype.Service;
|
||||
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
|
||||
implements BlogService{
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
@Lazy
|
||||
private ThumbService thumbService;
|
||||
|
||||
public BlogServiceImpl(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlogVO getBlogVOById(long blogId, HttpServletRequest request) {
|
||||
Blog blog = this.getById(blogId);
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
return this.getBlogVO(blog, loginUser);
|
||||
}
|
||||
|
||||
private BlogVO getBlogVO(Blog blog, User loginUser) {
|
||||
BlogVO blogVO = new BlogVO();
|
||||
BeanUtil.copyProperties(blog, blogVO);
|
||||
|
||||
if (loginUser == null) {
|
||||
return blogVO;
|
||||
}
|
||||
|
||||
Thumb thumb = thumbService.lambdaQuery()
|
||||
.eq(Thumb::getUserid, loginUser.getId())
|
||||
.eq(Thumb::getBlogId, blog.getId())
|
||||
.one();
|
||||
blogVO.setHasThumb(thumb != null);
|
||||
|
||||
return blogVO;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user