84 lines
2.4 KiB
Java
84 lines
2.4 KiB
Java
package com.hmdp.controller;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.hmdp.dto.Result;
|
|
import com.hmdp.dto.UserDTO;
|
|
import com.hmdp.entity.Blog;
|
|
import com.hmdp.entity.User;
|
|
import com.hmdp.service.IBlogService;
|
|
import com.hmdp.service.IUserService;
|
|
import com.hmdp.utils.SystemConstants;
|
|
import com.hmdp.utils.UserHolder;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* <p>
|
|
* 前端控制器
|
|
* </p>
|
|
*
|
|
* @author 虎哥
|
|
* @since 2021-12-22
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/blog")
|
|
public class BlogController {
|
|
|
|
@Resource
|
|
private IBlogService blogService;
|
|
@Resource
|
|
private IUserService userService;
|
|
|
|
@PostMapping
|
|
public Result saveBlog(@RequestBody Blog blog) {
|
|
// 获取登录用户
|
|
UserDTO user = UserHolder.getUser();
|
|
blog.setUserId(user.getId());
|
|
// 保存探店博文
|
|
blogService.save(blog);
|
|
// 返回id
|
|
return Result.ok(blog.getId());
|
|
}
|
|
|
|
@PutMapping("/like/{id}")
|
|
public Result likeBlog(@PathVariable("id") Long id) {
|
|
// 修改点赞数量
|
|
blogService.update()
|
|
.setSql("liked = liked + 1").eq("id", id).update();
|
|
return Result.ok();
|
|
}
|
|
|
|
@GetMapping("/of/me")
|
|
public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
|
|
// 获取登录用户
|
|
UserDTO user = UserHolder.getUser();
|
|
// 根据用户查询
|
|
Page<Blog> page = blogService.query()
|
|
.eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
|
|
// 获取当前页数据
|
|
List<Blog> records = page.getRecords();
|
|
return Result.ok(records);
|
|
}
|
|
|
|
@GetMapping("/hot")
|
|
public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
|
|
// 根据用户查询
|
|
Page<Blog> page = blogService.query()
|
|
.orderByDesc("liked")
|
|
.page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
|
|
// 获取当前页数据
|
|
List<Blog> records = page.getRecords();
|
|
// 查询用户
|
|
records.forEach(blog ->{
|
|
Long userId = blog.getUserId();
|
|
User user = userService.getById(userId);
|
|
blog.setName(user.getNickName());
|
|
blog.setIcon(user.getIcon());
|
|
});
|
|
return Result.ok(records);
|
|
}
|
|
}
|