删除不必要的代码
This commit is contained in:
parent
01b6dd65b9
commit
f0f0b6e3c4
@ -1,250 +0,0 @@
|
||||
package top.peng.answerbi.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.google.gson.Gson;
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import top.peng.answerbi.annotation.AuthCheck;
|
||||
import top.peng.answerbi.common.CommonResponse;
|
||||
import top.peng.answerbi.common.DeleteRequest;
|
||||
import top.peng.answerbi.common.ErrorCode;
|
||||
import top.peng.answerbi.common.ResultUtils;
|
||||
import top.peng.answerbi.constant.UserConstant;
|
||||
import top.peng.answerbi.exception.BusinessException;
|
||||
import top.peng.answerbi.exception.ThrowUtils;
|
||||
import top.peng.answerbi.model.dto.post.PostAddRequest;
|
||||
import top.peng.answerbi.model.dto.post.PostEditRequest;
|
||||
import top.peng.answerbi.model.dto.post.PostQueryRequest;
|
||||
import top.peng.answerbi.model.dto.post.PostUpdateRequest;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
import top.peng.answerbi.model.vo.PostVO;
|
||||
import top.peng.answerbi.service.PostService;
|
||||
import top.peng.answerbi.service.UserService;
|
||||
|
||||
/**
|
||||
* 帖子接口
|
||||
*
|
||||
* @author <a href="https://github.com/liyupi">程序员鱼皮</a>
|
||||
* @from <a href="https://yupi.icu">编程导航知识星球</a>
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/post")
|
||||
@Slf4j
|
||||
public class PostController {
|
||||
|
||||
@Resource
|
||||
private PostService postService;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
private final static Gson GSON = new Gson();
|
||||
|
||||
// region 增删改查
|
||||
|
||||
/**
|
||||
* 创建
|
||||
*
|
||||
* @param postAddRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/add")
|
||||
public CommonResponse<Long> addPost(@RequestBody PostAddRequest postAddRequest, HttpServletRequest request) {
|
||||
if (postAddRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
Post post = new Post();
|
||||
BeanUtils.copyProperties(postAddRequest, post);
|
||||
List<String> tags = postAddRequest.getTags();
|
||||
if (tags != null) {
|
||||
post.setTags(GSON.toJson(tags));
|
||||
}
|
||||
postService.validPost(post, true);
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
post.setUserId(loginUser.getId());
|
||||
post.setFavourNum(0);
|
||||
post.setThumbNum(0);
|
||||
boolean result = postService.save(post);
|
||||
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
|
||||
long newPostId = post.getId();
|
||||
return ResultUtils.success(newPostId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param deleteRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/delete")
|
||||
public CommonResponse<Boolean> deletePost(@RequestBody
|
||||
DeleteRequest deleteRequest, HttpServletRequest request) {
|
||||
if (deleteRequest == null || deleteRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User user = userService.getLoginUser(request);
|
||||
long id = deleteRequest.getId();
|
||||
// 判断是否存在
|
||||
Post oldPost = postService.getById(id);
|
||||
ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
|
||||
// 仅本人或管理员可删除
|
||||
if (!oldPost.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
boolean b = postService.removeById(id);
|
||||
return ResultUtils.success(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新(仅管理员)
|
||||
*
|
||||
* @param postUpdateRequest
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/update")
|
||||
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public CommonResponse<Boolean> updatePost(@RequestBody PostUpdateRequest postUpdateRequest) {
|
||||
if (postUpdateRequest == null || postUpdateRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
Post post = new Post();
|
||||
BeanUtils.copyProperties(postUpdateRequest, post);
|
||||
List<String> tags = postUpdateRequest.getTags();
|
||||
if (tags != null) {
|
||||
post.setTags(GSON.toJson(tags));
|
||||
}
|
||||
// 参数校验
|
||||
postService.validPost(post, false);
|
||||
long id = postUpdateRequest.getId();
|
||||
// 判断是否存在
|
||||
Post oldPost = postService.getById(id);
|
||||
ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
|
||||
boolean result = postService.updateById(post);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 id 获取
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/get/vo")
|
||||
public CommonResponse<PostVO> getPostVOById(long id, HttpServletRequest request) {
|
||||
if (id <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
Post post = postService.getById(id);
|
||||
if (post == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
|
||||
}
|
||||
return ResultUtils.success(postService.getPostVO(post, request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取列表(封装类)
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/list/page/vo")
|
||||
public CommonResponse<Page<PostVO>> listPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
long current = postQueryRequest.getCurrent();
|
||||
long size = postQueryRequest.getPageSize();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postService.page(new Page<>(current, size),
|
||||
postService.getQueryWrapper(postQueryRequest));
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取当前用户创建的资源列表
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/my/list/page/vo")
|
||||
public CommonResponse<Page<PostVO>> listMyPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postQueryRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
postQueryRequest.setUserId(loginUser.getId());
|
||||
long current = postQueryRequest.getCurrent();
|
||||
long size = postQueryRequest.getPageSize();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postService.page(new Page<>(current, size),
|
||||
postService.getQueryWrapper(postQueryRequest));
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 分页搜索(从 ES 查询,封装类)
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/search/page/vo")
|
||||
public CommonResponse<Page<PostVO>> searchPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
long size = postQueryRequest.getPageSize();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postService.searchFromEs(postQueryRequest);
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑(用户)
|
||||
*
|
||||
* @param postEditRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/edit")
|
||||
public CommonResponse<Boolean> editPost(@RequestBody PostEditRequest postEditRequest, HttpServletRequest request) {
|
||||
if (postEditRequest == null || postEditRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
Post post = new Post();
|
||||
BeanUtils.copyProperties(postEditRequest, post);
|
||||
List<String> tags = postEditRequest.getTags();
|
||||
if (tags != null) {
|
||||
post.setTags(GSON.toJson(tags));
|
||||
}
|
||||
// 参数校验
|
||||
postService.validPost(post, false);
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
long id = postEditRequest.getId();
|
||||
// 判断是否存在
|
||||
Post oldPost = postService.getById(id);
|
||||
ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
|
||||
// 仅本人或管理员可编辑
|
||||
if (!oldPost.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
boolean result = postService.updateById(post);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,109 +0,0 @@
|
||||
package top.peng.answerbi.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import top.peng.answerbi.common.CommonResponse;
|
||||
import top.peng.answerbi.common.ErrorCode;
|
||||
import top.peng.answerbi.common.ResultUtils;
|
||||
import top.peng.answerbi.exception.BusinessException;
|
||||
import top.peng.answerbi.exception.ThrowUtils;
|
||||
import top.peng.answerbi.model.dto.post.PostQueryRequest;
|
||||
import top.peng.answerbi.model.dto.postfavour.PostFavourAddRequest;
|
||||
import top.peng.answerbi.model.dto.postfavour.PostFavourQueryRequest;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
import top.peng.answerbi.model.vo.PostVO;
|
||||
import top.peng.answerbi.service.PostFavourService;
|
||||
import top.peng.answerbi.service.PostService;
|
||||
import top.peng.answerbi.service.UserService;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 帖子收藏接口
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/post_favour")
|
||||
@Slf4j
|
||||
public class PostFavourController {
|
||||
|
||||
@Resource
|
||||
private PostFavourService postFavourService;
|
||||
|
||||
@Resource
|
||||
private PostService postService;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 收藏 / 取消收藏
|
||||
*
|
||||
* @param postFavourAddRequest
|
||||
* @param request
|
||||
* @return resultNum 收藏变化数
|
||||
*/
|
||||
@PostMapping("/")
|
||||
public CommonResponse<Integer> doPostFavour(@RequestBody PostFavourAddRequest postFavourAddRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postFavourAddRequest == null || postFavourAddRequest.getPostId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
// 登录才能操作
|
||||
final User loginUser = userService.getLoginUser(request);
|
||||
long postId = postFavourAddRequest.getPostId();
|
||||
int result = postFavourService.doPostFavour(postId, loginUser);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取我收藏的帖子列表
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @param request
|
||||
*/
|
||||
@PostMapping("/my/list/page")
|
||||
public CommonResponse<Page<PostVO>> listMyFavourPostByPage(@RequestBody PostQueryRequest postQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postQueryRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
long current = postQueryRequest.getCurrent();
|
||||
long size = postQueryRequest.getPageSize();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postFavourService.listFavourPostByPage(new Page<>(current, size),
|
||||
postService.getQueryWrapper(postQueryRequest), loginUser.getId());
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户收藏的帖子列表
|
||||
*
|
||||
* @param postFavourQueryRequest
|
||||
* @param request
|
||||
*/
|
||||
@PostMapping("/list/page")
|
||||
public CommonResponse<Page<PostVO>> listFavourPostByPage(@RequestBody PostFavourQueryRequest postFavourQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postFavourQueryRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
long current = postFavourQueryRequest.getCurrent();
|
||||
long size = postFavourQueryRequest.getPageSize();
|
||||
Long userId = postFavourQueryRequest.getUserId();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20 || userId == null, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postFavourService.listFavourPostByPage(new Page<>(current, size),
|
||||
postService.getQueryWrapper(postFavourQueryRequest.getPostQueryRequest()), userId);
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
}
|
||||
@ -1,56 +0,0 @@
|
||||
package top.peng.answerbi.controller;
|
||||
|
||||
import top.peng.answerbi.common.CommonResponse;
|
||||
import top.peng.answerbi.common.ErrorCode;
|
||||
import top.peng.answerbi.common.ResultUtils;
|
||||
import top.peng.answerbi.exception.BusinessException;
|
||||
import top.peng.answerbi.model.dto.postthumb.PostThumbAddRequest;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
import top.peng.answerbi.service.PostThumbService;
|
||||
import top.peng.answerbi.service.UserService;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 帖子点赞接口
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/post_thumb")
|
||||
@Slf4j
|
||||
public class PostThumbController {
|
||||
|
||||
@Resource
|
||||
private PostThumbService postThumbService;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 点赞 / 取消点赞
|
||||
*
|
||||
* @param postThumbAddRequest
|
||||
* @param request
|
||||
* @return resultNum 本次点赞变化数
|
||||
*/
|
||||
@PostMapping("/")
|
||||
public CommonResponse<Integer> doThumb(@RequestBody PostThumbAddRequest postThumbAddRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postThumbAddRequest == null || postThumbAddRequest.getPostId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
// 登录才能点赞
|
||||
final User loginUser = userService.getLoginUser(request);
|
||||
long postId = postThumbAddRequest.getPostId();
|
||||
int result = postThumbService.doPostThumb(postId, loginUser);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
package top.peng.answerbi.esdao;
|
||||
|
||||
import top.peng.answerbi.model.dto.post.PostEsDTO;
|
||||
import java.util.List;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
|
||||
/**
|
||||
* 帖子 ES 操作
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
public interface PostEsDao extends ElasticsearchRepository<PostEsDTO, Long> {
|
||||
|
||||
List<PostEsDTO> findByUserId(Long userId);
|
||||
}
|
||||
@ -1,57 +0,0 @@
|
||||
package top.peng.answerbi.job.cycle;
|
||||
|
||||
import top.peng.answerbi.esdao.PostEsDao;
|
||||
import top.peng.answerbi.mapper.PostMapper;
|
||||
import top.peng.answerbi.model.dto.post.PostEsDTO;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
/**
|
||||
* 增量同步帖子到 es
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
// todo 取消注释开启任务
|
||||
//@Component
|
||||
@Slf4j
|
||||
public class IncSyncPostToEs {
|
||||
|
||||
@Resource
|
||||
private PostMapper postMapper;
|
||||
|
||||
@Resource
|
||||
private PostEsDao postEsDao;
|
||||
|
||||
/**
|
||||
* 每分钟执行一次
|
||||
*/
|
||||
@Scheduled(fixedRate = 60 * 1000)
|
||||
public void run() {
|
||||
// 查询近 5 分钟内的数据
|
||||
Date fiveMinutesAgoDate = new Date(new Date().getTime() - 5 * 60 * 1000L);
|
||||
List<Post> postList = postMapper.listPostWithDelete(fiveMinutesAgoDate);
|
||||
if (CollectionUtils.isEmpty(postList)) {
|
||||
log.info("no inc post");
|
||||
return;
|
||||
}
|
||||
List<PostEsDTO> postEsDTOList = postList.stream()
|
||||
.map(PostEsDTO::objToDto)
|
||||
.collect(Collectors.toList());
|
||||
final int pageSize = 500;
|
||||
int total = postEsDTOList.size();
|
||||
log.info("IncSyncPostToEs start, total {}", total);
|
||||
for (int i = 0; i < total; i += pageSize) {
|
||||
int end = Math.min(i + pageSize, total);
|
||||
log.info("sync from {} to {}", i, end);
|
||||
postEsDao.saveAll(postEsDTOList.subList(i, end));
|
||||
}
|
||||
log.info("IncSyncPostToEs end, total {}", total);
|
||||
}
|
||||
}
|
||||
@ -1,48 +0,0 @@
|
||||
package top.peng.answerbi.job.once;
|
||||
|
||||
import top.peng.answerbi.esdao.PostEsDao;
|
||||
import top.peng.answerbi.model.dto.post.PostEsDTO;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.service.PostService;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
|
||||
/**
|
||||
* 全量同步帖子到 es
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
// todo 取消注释开启任务
|
||||
//@Component
|
||||
@Slf4j
|
||||
public class FullSyncPostToEs implements CommandLineRunner {
|
||||
|
||||
@Resource
|
||||
private PostService postService;
|
||||
|
||||
@Resource
|
||||
private PostEsDao postEsDao;
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
List<Post> postList = postService.list();
|
||||
if (CollectionUtils.isEmpty(postList)) {
|
||||
return;
|
||||
}
|
||||
List<PostEsDTO> postEsDTOList = postList.stream().map(PostEsDTO::objToDto).collect(Collectors.toList());
|
||||
final int pageSize = 500;
|
||||
int total = postEsDTOList.size();
|
||||
log.info("FullSyncPostToEs start, total {}", total);
|
||||
for (int i = 0; i < total; i += pageSize) {
|
||||
int end = Math.min(i + pageSize, total);
|
||||
log.info("sync from {} to {}", i, end);
|
||||
postEsDao.saveAll(postEsDTOList.subList(i, end));
|
||||
}
|
||||
log.info("FullSyncPostToEs end, total {}", total);
|
||||
}
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
package top.peng.answerbi.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.model.entity.PostFavour;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* 帖子收藏数据库操作
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
public interface PostFavourMapper extends BaseMapper<PostFavour> {
|
||||
|
||||
/**
|
||||
* 分页查询收藏帖子列表
|
||||
*
|
||||
* @param page
|
||||
* @param queryWrapper
|
||||
* @param favourUserId
|
||||
* @return
|
||||
*/
|
||||
Page<Post> listFavourPostByPage(IPage<Post> page, @Param(Constants.WRAPPER) Wrapper<Post> queryWrapper,
|
||||
long favourUserId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
package top.peng.answerbi.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 帖子数据库操作
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
public interface PostMapper extends BaseMapper<Post> {
|
||||
|
||||
/**
|
||||
* 查询帖子列表(包括已被删除的数据)
|
||||
*/
|
||||
List<Post> listPostWithDelete(Date minUpdateTime);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
package top.peng.answerbi.mapper;
|
||||
|
||||
import top.peng.answerbi.model.entity.PostThumb;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* 帖子点赞数据库操作
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
public interface PostThumbMapper extends BaseMapper<PostThumb> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,32 +0,0 @@
|
||||
package top.peng.answerbi.model.dto.post;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 创建请求
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Data
|
||||
public class PostAddRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 标签列表
|
||||
*/
|
||||
private List<String> tags;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
package top.peng.answerbi.model.dto.post;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 编辑请求
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Data
|
||||
public class PostEditRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 标签列表
|
||||
*/
|
||||
private List<String> tags;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,125 +0,0 @@
|
||||
package top.peng.answerbi.model.dto.post;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.Gson;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
/**
|
||||
* 帖子 ES 包装类
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
**/
|
||||
// todo 取消注释开启 ES(须先配置 ES)
|
||||
//@Document(indexName = "post")
|
||||
@Data
|
||||
public class PostEsDTO implements Serializable {
|
||||
|
||||
private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 标签列表
|
||||
*/
|
||||
private List<String> tags;
|
||||
|
||||
/**
|
||||
* 点赞数
|
||||
*/
|
||||
private Integer thumbNum;
|
||||
|
||||
/**
|
||||
* 收藏数
|
||||
*/
|
||||
private Integer favourNum;
|
||||
|
||||
/**
|
||||
* 创建用户 id
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Field(index = false, store = true, type = FieldType.Date, format = {}, pattern = DATE_TIME_PATTERN)
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@Field(index = false, store = true, type = FieldType.Date, format = {}, pattern = DATE_TIME_PATTERN)
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 是否删除
|
||||
*/
|
||||
private Integer isDelete;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
/**
|
||||
* 对象转包装类
|
||||
*
|
||||
* @param post
|
||||
* @return
|
||||
*/
|
||||
public static PostEsDTO objToDto(Post post) {
|
||||
if (post == null) {
|
||||
return null;
|
||||
}
|
||||
PostEsDTO postEsDTO = new PostEsDTO();
|
||||
BeanUtils.copyProperties(post, postEsDTO);
|
||||
String tagsStr = post.getTags();
|
||||
if (StringUtils.isNotBlank(tagsStr)) {
|
||||
postEsDTO.setTags(GSON.fromJson(tagsStr, new TypeToken<List<String>>() {
|
||||
}.getType()));
|
||||
}
|
||||
return postEsDTO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 包装类转对象
|
||||
*
|
||||
* @param postEsDTO
|
||||
* @return
|
||||
*/
|
||||
public static Post dtoToObj(PostEsDTO postEsDTO) {
|
||||
if (postEsDTO == null) {
|
||||
return null;
|
||||
}
|
||||
Post post = new Post();
|
||||
BeanUtils.copyProperties(postEsDTO, post);
|
||||
List<String> tagList = postEsDTO.getTags();
|
||||
if (CollectionUtils.isNotEmpty(tagList)) {
|
||||
post.setTags(GSON.toJson(tagList));
|
||||
}
|
||||
return post;
|
||||
}
|
||||
}
|
||||
@ -1,65 +0,0 @@
|
||||
package top.peng.answerbi.model.dto.post;
|
||||
|
||||
import top.peng.answerbi.common.PageRequest;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 查询请求
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class PostQueryRequest extends PageRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long notId;
|
||||
|
||||
/**
|
||||
* 搜索词
|
||||
*/
|
||||
private String searchText;
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 标签列表
|
||||
*/
|
||||
private List<String> tags;
|
||||
|
||||
/**
|
||||
* 至少有一个标签
|
||||
*/
|
||||
private List<String> orTags;
|
||||
|
||||
/**
|
||||
* 创建用户 id
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 收藏用户 id
|
||||
*/
|
||||
private Long favourUserId;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
package top.peng.answerbi.model.dto.post;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 更新请求
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Data
|
||||
public class PostUpdateRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 标签列表
|
||||
*/
|
||||
private List<String> tags;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package top.peng.answerbi.model.dto.postfavour;
|
||||
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 帖子收藏 / 取消收藏请求
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Data
|
||||
public class PostFavourAddRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 帖子 id
|
||||
*/
|
||||
private Long postId;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
package top.peng.answerbi.model.dto.postfavour;
|
||||
|
||||
import top.peng.answerbi.common.PageRequest;
|
||||
import top.peng.answerbi.model.dto.post.PostQueryRequest;
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 帖子收藏查询请求
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class PostFavourQueryRequest extends PageRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 帖子查询请求
|
||||
*/
|
||||
private PostQueryRequest postQueryRequest;
|
||||
|
||||
/**
|
||||
* 用户 id
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package top.peng.answerbi.model.dto.postthumb;
|
||||
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 帖子点赞请求
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Data
|
||||
public class PostThumbAddRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 帖子 id
|
||||
*/
|
||||
private Long postId;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,76 +0,0 @@
|
||||
package top.peng.answerbi.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 帖子
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@TableName(value = "post")
|
||||
@Data
|
||||
public class Post implements Serializable {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 标签列表 json
|
||||
*/
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 点赞数
|
||||
*/
|
||||
private Integer thumbNum;
|
||||
|
||||
/**
|
||||
* 收藏数
|
||||
*/
|
||||
private Integer favourNum;
|
||||
|
||||
/**
|
||||
* 创建用户 id
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 是否删除
|
||||
*/
|
||||
@TableLogic
|
||||
private Integer isDelete;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,49 +0,0 @@
|
||||
package top.peng.answerbi.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 帖子收藏
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
**/
|
||||
@TableName(value = "post_favour")
|
||||
@Data
|
||||
public class PostFavour implements Serializable {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 帖子 id
|
||||
*/
|
||||
private Long postId;
|
||||
|
||||
/**
|
||||
* 创建用户 id
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,49 +0,0 @@
|
||||
package top.peng.answerbi.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 帖子点赞
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@TableName(value = "post_thumb")
|
||||
@Data
|
||||
public class PostThumb implements Serializable {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 帖子 id
|
||||
*/
|
||||
private Long postId;
|
||||
|
||||
/**
|
||||
* 创建用户 id
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -1,118 +0,0 @@
|
||||
package top.peng.answerbi.model.vo;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
/**
|
||||
* 帖子视图
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Data
|
||||
public class PostVO implements Serializable {
|
||||
|
||||
private final static Gson GSON = new Gson();
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 点赞数
|
||||
*/
|
||||
private Integer thumbNum;
|
||||
|
||||
/**
|
||||
* 收藏数
|
||||
*/
|
||||
private Integer favourNum;
|
||||
|
||||
/**
|
||||
* 创建用户 id
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 标签列表
|
||||
*/
|
||||
private List<String> tagList;
|
||||
|
||||
/**
|
||||
* 创建人信息
|
||||
*/
|
||||
private UserVO user;
|
||||
|
||||
/**
|
||||
* 是否已点赞
|
||||
*/
|
||||
private Boolean hasThumb;
|
||||
|
||||
/**
|
||||
* 是否已收藏
|
||||
*/
|
||||
private Boolean hasFavour;
|
||||
|
||||
/**
|
||||
* 包装类转对象
|
||||
*
|
||||
* @param postVO
|
||||
* @return
|
||||
*/
|
||||
public static Post voToObj(PostVO postVO) {
|
||||
if (postVO == null) {
|
||||
return null;
|
||||
}
|
||||
Post post = new Post();
|
||||
BeanUtils.copyProperties(postVO, post);
|
||||
List<String> tagList = postVO.getTagList();
|
||||
if (tagList != null) {
|
||||
post.setTags(GSON.toJson(tagList));
|
||||
}
|
||||
return post;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象转包装类
|
||||
*
|
||||
* @param post
|
||||
* @return
|
||||
*/
|
||||
public static PostVO objToVo(Post post) {
|
||||
if (post == null) {
|
||||
return null;
|
||||
}
|
||||
PostVO postVO = new PostVO();
|
||||
BeanUtils.copyProperties(post, postVO);
|
||||
postVO.setTagList(GSON.fromJson(post.getTags(), new TypeToken<List<String>>() {
|
||||
}.getType()));
|
||||
return postVO;
|
||||
}
|
||||
}
|
||||
@ -1,47 +0,0 @@
|
||||
package top.peng.answerbi.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.model.entity.PostFavour;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
|
||||
/**
|
||||
* 帖子收藏服务
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
public interface PostFavourService extends IService<PostFavour> {
|
||||
|
||||
/**
|
||||
* 帖子收藏
|
||||
*
|
||||
* @param postId
|
||||
* @param loginUser
|
||||
* @return
|
||||
*/
|
||||
int doPostFavour(long postId, User loginUser);
|
||||
|
||||
/**
|
||||
* 分页获取用户收藏的帖子列表
|
||||
*
|
||||
* @param page
|
||||
* @param queryWrapper
|
||||
* @param favourUserId
|
||||
* @return
|
||||
*/
|
||||
Page<Post> listFavourPostByPage(IPage<Post> page, Wrapper<Post> queryWrapper,
|
||||
long favourUserId);
|
||||
|
||||
/**
|
||||
* 帖子收藏(内部服务)
|
||||
*
|
||||
* @param userId
|
||||
* @param postId
|
||||
* @return
|
||||
*/
|
||||
int doPostFavourInner(long userId, long postId);
|
||||
}
|
||||
@ -1,60 +0,0 @@
|
||||
package top.peng.answerbi.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import top.peng.answerbi.model.dto.post.PostQueryRequest;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.model.vo.PostVO;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 帖子服务
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
public interface PostService extends IService<Post> {
|
||||
|
||||
/**
|
||||
* 校验
|
||||
*
|
||||
* @param post
|
||||
* @param add
|
||||
*/
|
||||
void validPost(Post post, boolean add);
|
||||
|
||||
/**
|
||||
* 获取查询条件
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @return
|
||||
*/
|
||||
QueryWrapper<Post> getQueryWrapper(PostQueryRequest postQueryRequest);
|
||||
|
||||
/**
|
||||
* 从 ES 查询
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @return
|
||||
*/
|
||||
Page<Post> searchFromEs(PostQueryRequest postQueryRequest);
|
||||
|
||||
/**
|
||||
* 获取帖子封装
|
||||
*
|
||||
* @param post
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
PostVO getPostVO(Post post, HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 分页获取帖子封装
|
||||
*
|
||||
* @param postPage
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
Page<PostVO> getPostVOPage(Page<Post> postPage, HttpServletRequest request);
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
package top.peng.answerbi.service;
|
||||
|
||||
import top.peng.answerbi.model.entity.PostThumb;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
|
||||
/**
|
||||
* 帖子点赞服务
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
public interface PostThumbService extends IService<PostThumb> {
|
||||
|
||||
/**
|
||||
* 点赞
|
||||
*
|
||||
* @param postId
|
||||
* @param loginUser
|
||||
* @return
|
||||
*/
|
||||
int doPostThumb(long postId, User loginUser);
|
||||
|
||||
/**
|
||||
* 帖子点赞(内部服务)
|
||||
*
|
||||
* @param userId
|
||||
* @param postId
|
||||
* @return
|
||||
*/
|
||||
int doPostThumbInner(long userId, long postId);
|
||||
}
|
||||
@ -1,116 +0,0 @@
|
||||
package top.peng.answerbi.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import top.peng.answerbi.common.ErrorCode;
|
||||
import top.peng.answerbi.exception.BusinessException;
|
||||
import top.peng.answerbi.mapper.PostFavourMapper;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.model.entity.PostFavour;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
import top.peng.answerbi.service.PostFavourService;
|
||||
import top.peng.answerbi.service.PostService;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.aop.framework.AopContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 帖子收藏服务实现
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Service
|
||||
public class PostFavourServiceImpl extends ServiceImpl<PostFavourMapper, PostFavour>
|
||||
implements PostFavourService {
|
||||
|
||||
@Resource
|
||||
private PostService postService;
|
||||
|
||||
/**
|
||||
* 帖子收藏
|
||||
*
|
||||
* @param postId
|
||||
* @param loginUser
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int doPostFavour(long postId, User loginUser) {
|
||||
// 判断是否存在
|
||||
Post post = postService.getById(postId);
|
||||
if (post == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
|
||||
}
|
||||
// 是否已帖子收藏
|
||||
long userId = loginUser.getId();
|
||||
// 每个用户串行帖子收藏
|
||||
// 锁必须要包裹住事务方法
|
||||
PostFavourService postFavourService = (PostFavourService) AopContext.currentProxy();
|
||||
synchronized (String.valueOf(userId).intern()) {
|
||||
return postFavourService.doPostFavourInner(userId, postId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Post> listFavourPostByPage(IPage<Post> page, Wrapper<Post> queryWrapper, long favourUserId) {
|
||||
if (favourUserId <= 0) {
|
||||
return new Page<>();
|
||||
}
|
||||
return baseMapper.listFavourPostByPage(page, queryWrapper, favourUserId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装了事务的方法
|
||||
*
|
||||
* @param userId
|
||||
* @param postId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int doPostFavourInner(long userId, long postId) {
|
||||
PostFavour postFavour = new PostFavour();
|
||||
postFavour.setUserId(userId);
|
||||
postFavour.setPostId(postId);
|
||||
QueryWrapper<PostFavour> postFavourQueryWrapper = new QueryWrapper<>(postFavour);
|
||||
PostFavour oldPostFavour = this.getOne(postFavourQueryWrapper);
|
||||
boolean result;
|
||||
// 已收藏
|
||||
if (oldPostFavour != null) {
|
||||
result = this.remove(postFavourQueryWrapper);
|
||||
if (result) {
|
||||
// 帖子收藏数 - 1
|
||||
result = postService.update()
|
||||
.eq("id", postId)
|
||||
.gt("favourNum", 0)
|
||||
.setSql("favourNum = favourNum - 1")
|
||||
.update();
|
||||
return result ? -1 : 0;
|
||||
} else {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
|
||||
}
|
||||
} else {
|
||||
// 未帖子收藏
|
||||
result = this.save(postFavour);
|
||||
if (result) {
|
||||
// 帖子收藏数 + 1
|
||||
result = postService.update()
|
||||
.eq("id", postId)
|
||||
.setSql("favourNum = favourNum + 1")
|
||||
.update();
|
||||
return result ? 1 : 0;
|
||||
} else {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,316 +0,0 @@
|
||||
package top.peng.answerbi.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.gson.Gson;
|
||||
import top.peng.answerbi.common.ErrorCode;
|
||||
import top.peng.answerbi.constant.CommonConstant;
|
||||
import top.peng.answerbi.exception.BusinessException;
|
||||
import top.peng.answerbi.exception.ThrowUtils;
|
||||
import top.peng.answerbi.mapper.PostFavourMapper;
|
||||
import top.peng.answerbi.mapper.PostMapper;
|
||||
import top.peng.answerbi.mapper.PostThumbMapper;
|
||||
import top.peng.answerbi.model.dto.post.PostEsDTO;
|
||||
import top.peng.answerbi.model.dto.post.PostQueryRequest;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.model.entity.PostFavour;
|
||||
import top.peng.answerbi.model.entity.PostThumb;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
import top.peng.answerbi.model.vo.PostVO;
|
||||
import top.peng.answerbi.model.vo.UserVO;
|
||||
import top.peng.answerbi.service.PostService;
|
||||
import top.peng.answerbi.service.UserService;
|
||||
import top.peng.answerbi.utils.SqlUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.search.sort.SortBuilder;
|
||||
import org.elasticsearch.search.sort.SortBuilders;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.core.SearchHit;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 帖子服务实现
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
|
||||
|
||||
private final static Gson GSON = new Gson();
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private PostThumbMapper postThumbMapper;
|
||||
|
||||
@Resource
|
||||
private PostFavourMapper postFavourMapper;
|
||||
|
||||
@Resource
|
||||
private ElasticsearchRestTemplate elasticsearchRestTemplate;
|
||||
|
||||
@Override
|
||||
public void validPost(Post post, boolean add) {
|
||||
if (post == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
String title = post.getTitle();
|
||||
String content = post.getContent();
|
||||
String tags = post.getTags();
|
||||
// 创建时,参数不能为空
|
||||
if (add) {
|
||||
ThrowUtils.throwIf(StringUtils.isAnyBlank(title, content, tags), ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
// 有参数则校验
|
||||
if (StringUtils.isNotBlank(title) && title.length() > 80) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "标题过长");
|
||||
}
|
||||
if (StringUtils.isNotBlank(content) && content.length() > 8192) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容过长");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询包装类
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public QueryWrapper<Post> getQueryWrapper(PostQueryRequest postQueryRequest) {
|
||||
QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
|
||||
if (postQueryRequest == null) {
|
||||
return queryWrapper;
|
||||
}
|
||||
String searchText = postQueryRequest.getSearchText();
|
||||
String sortField = postQueryRequest.getSortField();
|
||||
String sortOrder = postQueryRequest.getSortOrder();
|
||||
Long id = postQueryRequest.getId();
|
||||
String title = postQueryRequest.getTitle();
|
||||
String content = postQueryRequest.getContent();
|
||||
List<String> tagList = postQueryRequest.getTags();
|
||||
Long userId = postQueryRequest.getUserId();
|
||||
Long notId = postQueryRequest.getNotId();
|
||||
// 拼接查询条件
|
||||
if (StringUtils.isNotBlank(searchText)) {
|
||||
queryWrapper.like("title", searchText).or().like("content", searchText);
|
||||
}
|
||||
queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
|
||||
queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
|
||||
if (CollectionUtils.isNotEmpty(tagList)) {
|
||||
for (String tag : tagList) {
|
||||
queryWrapper.like("tags", "\"" + tag + "\"");
|
||||
}
|
||||
}
|
||||
queryWrapper.ne(ObjectUtils.isNotEmpty(notId), "id", notId);
|
||||
queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
|
||||
queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
|
||||
queryWrapper.eq("isDelete", false);
|
||||
queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
|
||||
sortField);
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Post> searchFromEs(PostQueryRequest postQueryRequest) {
|
||||
Long id = postQueryRequest.getId();
|
||||
Long notId = postQueryRequest.getNotId();
|
||||
String searchText = postQueryRequest.getSearchText();
|
||||
String title = postQueryRequest.getTitle();
|
||||
String content = postQueryRequest.getContent();
|
||||
List<String> tagList = postQueryRequest.getTags();
|
||||
List<String> orTagList = postQueryRequest.getOrTags();
|
||||
Long userId = postQueryRequest.getUserId();
|
||||
// es 起始页为 0
|
||||
long current = postQueryRequest.getCurrent() - 1;
|
||||
long pageSize = postQueryRequest.getPageSize();
|
||||
String sortField = postQueryRequest.getSortField();
|
||||
String sortOrder = postQueryRequest.getSortOrder();
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
// 过滤
|
||||
boolQueryBuilder.filter(QueryBuilders.termQuery("isDelete", 0));
|
||||
if (id != null) {
|
||||
boolQueryBuilder.filter(QueryBuilders.termQuery("id", id));
|
||||
}
|
||||
if (notId != null) {
|
||||
boolQueryBuilder.mustNot(QueryBuilders.termQuery("id", notId));
|
||||
}
|
||||
if (userId != null) {
|
||||
boolQueryBuilder.filter(QueryBuilders.termQuery("userId", userId));
|
||||
}
|
||||
// 必须包含所有标签
|
||||
if (CollectionUtils.isNotEmpty(tagList)) {
|
||||
for (String tag : tagList) {
|
||||
boolQueryBuilder.filter(QueryBuilders.termQuery("tags", tag));
|
||||
}
|
||||
}
|
||||
// 包含任何一个标签即可
|
||||
if (CollectionUtils.isNotEmpty(orTagList)) {
|
||||
BoolQueryBuilder orTagBoolQueryBuilder = QueryBuilders.boolQuery();
|
||||
for (String tag : orTagList) {
|
||||
orTagBoolQueryBuilder.should(QueryBuilders.termQuery("tags", tag));
|
||||
}
|
||||
orTagBoolQueryBuilder.minimumShouldMatch(1);
|
||||
boolQueryBuilder.filter(orTagBoolQueryBuilder);
|
||||
}
|
||||
// 按关键词检索
|
||||
if (StringUtils.isNotBlank(searchText)) {
|
||||
boolQueryBuilder.should(QueryBuilders.matchQuery("title", searchText));
|
||||
boolQueryBuilder.should(QueryBuilders.matchQuery("description", searchText));
|
||||
boolQueryBuilder.should(QueryBuilders.matchQuery("content", searchText));
|
||||
boolQueryBuilder.minimumShouldMatch(1);
|
||||
}
|
||||
// 按标题检索
|
||||
if (StringUtils.isNotBlank(title)) {
|
||||
boolQueryBuilder.should(QueryBuilders.matchQuery("title", title));
|
||||
boolQueryBuilder.minimumShouldMatch(1);
|
||||
}
|
||||
// 按内容检索
|
||||
if (StringUtils.isNotBlank(content)) {
|
||||
boolQueryBuilder.should(QueryBuilders.matchQuery("content", content));
|
||||
boolQueryBuilder.minimumShouldMatch(1);
|
||||
}
|
||||
// 排序
|
||||
SortBuilder<?> sortBuilder = SortBuilders.scoreSort();
|
||||
if (StringUtils.isNotBlank(sortField)) {
|
||||
sortBuilder = SortBuilders.fieldSort(sortField);
|
||||
sortBuilder.order(CommonConstant.SORT_ORDER_ASC.equals(sortOrder) ? SortOrder.ASC : SortOrder.DESC);
|
||||
}
|
||||
// 分页
|
||||
PageRequest pageRequest = PageRequest.of((int) current, (int) pageSize);
|
||||
// 构造查询
|
||||
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder)
|
||||
.withPageable(pageRequest).withSorts(sortBuilder).build();
|
||||
SearchHits<PostEsDTO> searchHits = elasticsearchRestTemplate.search(searchQuery, PostEsDTO.class);
|
||||
Page<Post> page = new Page<>();
|
||||
page.setTotal(searchHits.getTotalHits());
|
||||
List<Post> resourceList = new ArrayList<>();
|
||||
// 查出结果后,从 db 获取最新动态数据(比如点赞数)
|
||||
if (searchHits.hasSearchHits()) {
|
||||
List<SearchHit<PostEsDTO>> searchHitList = searchHits.getSearchHits();
|
||||
List<Long> postIdList = searchHitList.stream().map(searchHit -> searchHit.getContent().getId())
|
||||
.collect(Collectors.toList());
|
||||
List<Post> postList = baseMapper.selectBatchIds(postIdList);
|
||||
if (postList != null) {
|
||||
Map<Long, List<Post>> idPostMap = postList.stream().collect(Collectors.groupingBy(Post::getId));
|
||||
postIdList.forEach(postId -> {
|
||||
if (idPostMap.containsKey(postId)) {
|
||||
resourceList.add(idPostMap.get(postId).get(0));
|
||||
} else {
|
||||
// 从 es 清空 db 已物理删除的数据
|
||||
String delete = elasticsearchRestTemplate.delete(String.valueOf(postId), PostEsDTO.class);
|
||||
log.info("delete post {}", delete);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
page.setRecords(resourceList);
|
||||
return page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostVO getPostVO(Post post, HttpServletRequest request) {
|
||||
PostVO postVO = PostVO.objToVo(post);
|
||||
long postId = post.getId();
|
||||
// 1. 关联查询用户信息
|
||||
Long userId = post.getUserId();
|
||||
User user = null;
|
||||
if (userId != null && userId > 0) {
|
||||
user = userService.getById(userId);
|
||||
}
|
||||
UserVO userVO = userService.getUserVO(user);
|
||||
postVO.setUser(userVO);
|
||||
// 2. 已登录,获取用户点赞、收藏状态
|
||||
User loginUser = userService.getLoginUserPermitNull(request);
|
||||
if (loginUser != null) {
|
||||
// 获取点赞
|
||||
QueryWrapper<PostThumb> postThumbQueryWrapper = new QueryWrapper<>();
|
||||
postThumbQueryWrapper.in("postId", postId);
|
||||
postThumbQueryWrapper.eq("userId", loginUser.getId());
|
||||
PostThumb postThumb = postThumbMapper.selectOne(postThumbQueryWrapper);
|
||||
postVO.setHasThumb(postThumb != null);
|
||||
// 获取收藏
|
||||
QueryWrapper<PostFavour> postFavourQueryWrapper = new QueryWrapper<>();
|
||||
postFavourQueryWrapper.in("postId", postId);
|
||||
postFavourQueryWrapper.eq("userId", loginUser.getId());
|
||||
PostFavour postFavour = postFavourMapper.selectOne(postFavourQueryWrapper);
|
||||
postVO.setHasFavour(postFavour != null);
|
||||
}
|
||||
return postVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<PostVO> getPostVOPage(Page<Post> postPage, HttpServletRequest request) {
|
||||
List<Post> postList = postPage.getRecords();
|
||||
Page<PostVO> postVOPage = new Page<>(postPage.getCurrent(), postPage.getSize(), postPage.getTotal());
|
||||
if (CollectionUtils.isEmpty(postList)) {
|
||||
return postVOPage;
|
||||
}
|
||||
// 1. 关联查询用户信息
|
||||
Set<Long> userIdSet = postList.stream().map(Post::getUserId).collect(Collectors.toSet());
|
||||
Map<Long, List<User>> userIdUserListMap = userService.listByIds(userIdSet).stream()
|
||||
.collect(Collectors.groupingBy(User::getId));
|
||||
// 2. 已登录,获取用户点赞、收藏状态
|
||||
Map<Long, Boolean> postIdHasThumbMap = new HashMap<>();
|
||||
Map<Long, Boolean> postIdHasFavourMap = new HashMap<>();
|
||||
User loginUser = userService.getLoginUserPermitNull(request);
|
||||
if (loginUser != null) {
|
||||
Set<Long> postIdSet = postList.stream().map(Post::getId).collect(Collectors.toSet());
|
||||
loginUser = userService.getLoginUser(request);
|
||||
// 获取点赞
|
||||
QueryWrapper<PostThumb> postThumbQueryWrapper = new QueryWrapper<>();
|
||||
postThumbQueryWrapper.in("postId", postIdSet);
|
||||
postThumbQueryWrapper.eq("userId", loginUser.getId());
|
||||
List<PostThumb> postPostThumbList = postThumbMapper.selectList(postThumbQueryWrapper);
|
||||
postPostThumbList.forEach(postPostThumb -> postIdHasThumbMap.put(postPostThumb.getPostId(), true));
|
||||
// 获取收藏
|
||||
QueryWrapper<PostFavour> postFavourQueryWrapper = new QueryWrapper<>();
|
||||
postFavourQueryWrapper.in("postId", postIdSet);
|
||||
postFavourQueryWrapper.eq("userId", loginUser.getId());
|
||||
List<PostFavour> postFavourList = postFavourMapper.selectList(postFavourQueryWrapper);
|
||||
postFavourList.forEach(postFavour -> postIdHasFavourMap.put(postFavour.getPostId(), true));
|
||||
}
|
||||
// 填充信息
|
||||
List<PostVO> postVOList = postList.stream().map(post -> {
|
||||
PostVO postVO = PostVO.objToVo(post);
|
||||
Long userId = post.getUserId();
|
||||
User user = null;
|
||||
if (userIdUserListMap.containsKey(userId)) {
|
||||
user = userIdUserListMap.get(userId).get(0);
|
||||
}
|
||||
postVO.setUser(userService.getUserVO(user));
|
||||
postVO.setHasThumb(postIdHasThumbMap.getOrDefault(post.getId(), false));
|
||||
postVO.setHasFavour(postIdHasFavourMap.getOrDefault(post.getId(), false));
|
||||
return postVO;
|
||||
}).collect(Collectors.toList());
|
||||
postVOPage.setRecords(postVOList);
|
||||
return postVOPage;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,105 +0,0 @@
|
||||
package top.peng.answerbi.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import top.peng.answerbi.common.ErrorCode;
|
||||
import top.peng.answerbi.exception.BusinessException;
|
||||
import top.peng.answerbi.mapper.PostThumbMapper;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.model.entity.PostThumb;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
import top.peng.answerbi.service.PostService;
|
||||
import top.peng.answerbi.service.PostThumbService;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.aop.framework.AopContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 帖子点赞服务实现
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Service
|
||||
public class PostThumbServiceImpl extends ServiceImpl<PostThumbMapper, PostThumb>
|
||||
implements PostThumbService {
|
||||
|
||||
@Resource
|
||||
private PostService postService;
|
||||
|
||||
/**
|
||||
* 点赞
|
||||
*
|
||||
* @param postId
|
||||
* @param loginUser
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int doPostThumb(long postId, User loginUser) {
|
||||
// 判断实体是否存在,根据类别获取实体
|
||||
Post post = postService.getById(postId);
|
||||
if (post == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
|
||||
}
|
||||
// 是否已点赞
|
||||
long userId = loginUser.getId();
|
||||
// 每个用户串行点赞
|
||||
// 锁必须要包裹住事务方法
|
||||
PostThumbService postThumbService = (PostThumbService) AopContext.currentProxy();
|
||||
synchronized (String.valueOf(userId).intern()) {
|
||||
return postThumbService.doPostThumbInner(userId, postId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装了事务的方法
|
||||
*
|
||||
* @param userId
|
||||
* @param postId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int doPostThumbInner(long userId, long postId) {
|
||||
PostThumb postThumb = new PostThumb();
|
||||
postThumb.setUserId(userId);
|
||||
postThumb.setPostId(postId);
|
||||
QueryWrapper<PostThumb> thumbQueryWrapper = new QueryWrapper<>(postThumb);
|
||||
PostThumb oldPostThumb = this.getOne(thumbQueryWrapper);
|
||||
boolean result;
|
||||
// 已点赞
|
||||
if (oldPostThumb != null) {
|
||||
result = this.remove(thumbQueryWrapper);
|
||||
if (result) {
|
||||
// 点赞数 - 1
|
||||
result = postService.update()
|
||||
.eq("id", postId)
|
||||
.gt("thumbNum", 0)
|
||||
.setSql("thumbNum = thumbNum - 1")
|
||||
.update();
|
||||
return result ? -1 : 0;
|
||||
} else {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
|
||||
}
|
||||
} else {
|
||||
// 未点赞
|
||||
result = this.save(postThumb);
|
||||
if (result) {
|
||||
// 点赞数 + 1
|
||||
result = postService.update()
|
||||
.eq("id", postId)
|
||||
.setSql("thumbNum = thumbNum + 1")
|
||||
.update();
|
||||
return result ? 1 : 0;
|
||||
} else {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="top.peng.answerbi.mapper.PostFavourMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="top.peng.answerbi.model.entity.PostFavour">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
<result property="postId" column="postId" jdbcType="BIGINT"/>
|
||||
<result property="userId" column="userId" jdbcType="BIGINT"/>
|
||||
<result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>
|
||||
<result property="updateTime" column="updateTime" jdbcType="TIMESTAMP"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,postId,userId,
|
||||
createTime,updateTime
|
||||
</sql>
|
||||
|
||||
<select id="listFavourPostByPage"
|
||||
resultType="top.peng.answerbi.model.entity.Post">
|
||||
select p.*
|
||||
from post p
|
||||
join (select postId from post_favour where userId = #{favourUserId}) pf
|
||||
on p.id = pf.postId ${ew.customSqlSegment}
|
||||
</select>
|
||||
</mapper>
|
||||
@ -1,31 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="top.peng.answerbi.mapper.PostMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="top.peng.answerbi.model.entity.Post">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
<result property="title" column="title" jdbcType="VARCHAR"/>
|
||||
<result property="content" column="content" jdbcType="VARCHAR"/>
|
||||
<result property="tags" column="tags" jdbcType="VARCHAR"/>
|
||||
<result property="thumbNum" column="thumbNum" jdbcType="BIGINT"/>
|
||||
<result property="favourNum" column="favourNum" jdbcType="BIGINT"/>
|
||||
<result property="userId" column="userId" jdbcType="BIGINT"/>
|
||||
<result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>
|
||||
<result property="updateTime" column="updateTime" jdbcType="TIMESTAMP"/>
|
||||
<result property="isDelete" column="isDelete" jdbcType="TINYINT"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,title,content,tags,
|
||||
thumbNum,favourNum,userId,
|
||||
createTime,updateTime,isDelete
|
||||
</sql>
|
||||
|
||||
<select id="listPostWithDelete" resultType="top.peng.answerbi.model.entity.Post">
|
||||
select *
|
||||
from post
|
||||
where updateTime >= #{minUpdateTime}
|
||||
</select>
|
||||
</mapper>
|
||||
@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="top.peng.answerbi.mapper.PostThumbMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="top.peng.answerbi.model.entity.PostThumb">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
<result property="postId" column="postId" jdbcType="BIGINT"/>
|
||||
<result property="userId" column="userId" jdbcType="BIGINT"/>
|
||||
<result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>
|
||||
<result property="updateTime" column="updateTime" jdbcType="TIMESTAMP"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,postId,
|
||||
userId,createTime,updateTime
|
||||
</sql>
|
||||
</mapper>
|
||||
Binary file not shown.
@ -1,83 +0,0 @@
|
||||
package top.peng.answerbi.esdao;
|
||||
|
||||
import top.peng.answerbi.model.dto.post.PostEsDTO;
|
||||
import top.peng.answerbi.model.dto.post.PostQueryRequest;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import top.peng.answerbi.service.PostService;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.annotation.Resource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
/**
|
||||
* 帖子 ES 操作测试
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@SpringBootTest
|
||||
public class PostEsDaoTest {
|
||||
|
||||
@Resource
|
||||
private PostEsDao postEsDao;
|
||||
|
||||
@Resource
|
||||
private PostService postService;
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
PostQueryRequest postQueryRequest = new PostQueryRequest();
|
||||
com.baomidou.mybatisplus.extension.plugins.pagination.Page<Post> page =
|
||||
postService.searchFromEs(postQueryRequest);
|
||||
System.out.println(page);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSelect() {
|
||||
System.out.println(postEsDao.count());
|
||||
Page<PostEsDTO> PostPage = postEsDao.findAll(
|
||||
PageRequest.of(0, 5, Sort.by("createTime")));
|
||||
List<PostEsDTO> postList = PostPage.getContent();
|
||||
System.out.println(postList);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAdd() {
|
||||
PostEsDTO postEsDTO = new PostEsDTO();
|
||||
postEsDTO.setId(1L);
|
||||
postEsDTO.setTitle("test");
|
||||
postEsDTO.setContent("test");
|
||||
postEsDTO.setTags(Arrays.asList("java", "python"));
|
||||
postEsDTO.setThumbNum(1);
|
||||
postEsDTO.setFavourNum(1);
|
||||
postEsDTO.setUserId(1L);
|
||||
postEsDTO.setCreateTime(new Date());
|
||||
postEsDTO.setUpdateTime(new Date());
|
||||
postEsDTO.setIsDelete(0);
|
||||
postEsDao.save(postEsDTO);
|
||||
System.out.println(postEsDTO.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindById() {
|
||||
Optional<PostEsDTO> postEsDTO = postEsDao.findById(1L);
|
||||
System.out.println(postEsDTO);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCount() {
|
||||
System.out.println(postEsDao.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindByCategory() {
|
||||
List<PostEsDTO> postEsDaoTestList = postEsDao.findByUserId(1L);
|
||||
System.out.println(postEsDaoTestList);
|
||||
}
|
||||
}
|
||||
@ -1,33 +0,0 @@
|
||||
package top.peng.answerbi.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import javax.annotation.Resource;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
/**
|
||||
* 帖子收藏数据库操作测试
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@SpringBootTest
|
||||
class PostFavourMapperTest {
|
||||
|
||||
@Resource
|
||||
private PostFavourMapper postFavourMapper;
|
||||
|
||||
@Test
|
||||
void listUserFavourPostByPage() {
|
||||
IPage<Post> page = new Page<>(2, 1);
|
||||
QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("id", 1);
|
||||
queryWrapper.like("content", "a");
|
||||
IPage<Post> result = postFavourMapper.listFavourPostByPage(page, queryWrapper, 1);
|
||||
Assertions.assertNotNull(result);
|
||||
}
|
||||
}
|
||||
@ -1,28 +0,0 @@
|
||||
package top.peng.answerbi.mapper;
|
||||
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
/**
|
||||
* 帖子数据库操作测试
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@SpringBootTest
|
||||
class PostMapperTest {
|
||||
|
||||
@Resource
|
||||
private PostMapper postMapper;
|
||||
|
||||
@Test
|
||||
void listPostWithDelete() {
|
||||
List<Post> postList = postMapper.listPostWithDelete(new Date());
|
||||
Assertions.assertNotNull(postList);
|
||||
}
|
||||
}
|
||||
@ -1,44 +0,0 @@
|
||||
package top.peng.answerbi.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import javax.annotation.Resource;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
|
||||
/**
|
||||
* 帖子收藏服务测试
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@SpringBootTest
|
||||
class PostFavourServiceTest {
|
||||
|
||||
@Resource
|
||||
private PostFavourService postFavourService;
|
||||
|
||||
private static final User loginUser = new User();
|
||||
|
||||
@BeforeAll
|
||||
static void setUp() {
|
||||
loginUser.setId(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void doPostFavour() {
|
||||
int i = postFavourService.doPostFavour(1L, loginUser);
|
||||
Assertions.assertTrue(i >= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listFavourPostByPage() {
|
||||
QueryWrapper<Post> postQueryWrapper = new QueryWrapper<>();
|
||||
postQueryWrapper.eq("id", 1L);
|
||||
postFavourService.listFavourPostByPage(Page.of(0, 1), postQueryWrapper, loginUser.getId());
|
||||
}
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
package top.peng.answerbi.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import top.peng.answerbi.model.dto.post.PostQueryRequest;
|
||||
import top.peng.answerbi.model.entity.Post;
|
||||
import javax.annotation.Resource;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
/**
|
||||
* 帖子服务测试
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@SpringBootTest
|
||||
class PostServiceTest {
|
||||
|
||||
@Resource
|
||||
private PostService postService;
|
||||
|
||||
@Test
|
||||
void searchFromEs() {
|
||||
PostQueryRequest postQueryRequest = new PostQueryRequest();
|
||||
postQueryRequest.setUserId(1L);
|
||||
Page<Post> postPage = postService.searchFromEs(postQueryRequest);
|
||||
Assertions.assertNotNull(postPage);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
package top.peng.answerbi.service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
|
||||
/**
|
||||
* 帖子点赞服务测试
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@SpringBootTest
|
||||
class PostThumbServiceTest {
|
||||
|
||||
@Resource
|
||||
private PostThumbService postThumbService;
|
||||
|
||||
private static final User loginUser = new User();
|
||||
|
||||
@BeforeAll
|
||||
static void setUp() {
|
||||
loginUser.setId(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void doPostThumb() {
|
||||
int i = postThumbService.doPostThumb(1L, loginUser);
|
||||
Assertions.assertTrue(i >= 0);
|
||||
}
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
package top.peng.answerbi.utils;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.support.ExcelTypeEnum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* EasyExcel 测试
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@SpringBootTest
|
||||
public class EasyExcelTest {
|
||||
|
||||
@Test
|
||||
public void doImport() throws FileNotFoundException {
|
||||
File file = ResourceUtils.getFile("classpath:test_excel.xlsx");
|
||||
List<Map<Integer, String>> list = EasyExcel.read(file)
|
||||
.excelType(ExcelTypeEnum.XLSX)
|
||||
.sheet()
|
||||
.headRowNumber(0)
|
||||
.doReadSync();
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user