package top.picacho.bbs.controller.rest;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.picacho.bbs.common.Constants;
import top.picacho.bbs.common.ServiceResultEnum;
import top.picacho.bbs.entity.BBSPost;
import top.picacho.bbs.entity.BBSPostCategory;
import top.picacho.bbs.entity.BBSUser;
import top.picacho.bbs.service.BBSPostCategoryService;
import top.picacho.bbs.service.BBSPostService;
import top.picacho.bbs.service.BBSUserService;
import top.picacho.bbs.util.Result;
import top.picacho.bbs.util.ResultGenerator;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Controller
public class BBSPostController {
@Resource
private BBSPostCategoryService bbsPostCategoryService;
@Resource
private BBSPostService bbsPostService;
@Resource
private BBSUserService bbsUserService;
/**
* 跳转至发帖页面
* @param request
* @return
*/
@GetMapping("/addPostPage")
public String addPostPage(HttpServletRequest request) {
List<BBSPostCategory> bbsPostCategories = bbsPostCategoryService.getBBSPostCategories();
if (CollectionUtils.isEmpty(bbsPostCategories)) {
return "error/error_404";
}
//将分类数据封装到request域中
request.setAttribute("bbsPostCategories", bbsPostCategories);
return "jie/add";
}
/**
* 添加一条帖子数据
*/
@PostMapping("/addPost")
@ResponseBody
public Result addPost(@RequestParam("postTitle") String postTitle,
@RequestParam("postCategoryId") Integer postCategoryId,
@RequestParam("postContent") String postContent,
@RequestParam("verifyCode") String verifyCode,
HttpSession httpSession) {
if (!StringUtils.hasLength(postTitle)) {
return ResultGenerator.genFailResult("postTitle参数错误");
}
if (null == postCategoryId || postCategoryId < 0) {
return ResultGenerator.genFailResult("postCategoryId参数错误");
}
BBSPostCategory bbsPostCategory = bbsPostCategoryService.selectById(postCategoryId);
if (null == bbsPostCategory) {
return ResultGenerator.genFailResult("postCategoryId参数错误");
}
if (!StringUtils.hasLength(postContent)) {
return ResultGenerator.genFailResult("postContent参数错误");
}
if (postTitle.trim().length() > 32) {
return ResultGenerator.genFailResult("标题过长");
}
if (postContent.trim().length() > 100000) {
return ResultGenerator.genFailResult("内容过长");
}
String kaptchaCode = httpSession.getAttribute(Constants.VERIFY_CODE_KEY) + "";
if (!StringUtils.hasLength(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_ERROR.getResult());
}
BBSUser bbsUser = (BBSUser) httpSession.getAttribute(Constants.USER_SESSION_KEY);
BBSPost bbsPost = new BBSPost();
bbsPost.setPublishUserId(bbsUser.getUserId());
bbsPost.setPostTitle(postTitle);
bbsPost.setPostContent(postContent);
bbsPost.setPostCategoryId(postCategoryId);
bbsPost.setPostCategoryName(bbsPostCategory.getCategoryName());
if (bbsPostService.savePost(bbsPost) > 0) {
httpSession.removeAttribute(Constants.VERIFY_CODE_KEY);//清空session中的验证码信息
return ResultGenerator.genSuccessResult();
} else {
return ResultGenerator.genFailResult("请求失败,请检查参数及账号是否有操作权限");
}
}
/**
* 跳转帖子详情页
* @param request
* @param postId
* @param commentPage
* @return
*/
@GetMapping("detail/{postId}")
public String postDetail(HttpServletRequest request, @PathVariable(value = "postId") Long postId,
@RequestParam(value = "commentPage", required = false, defaultValue = "1") Integer commentPage) {
List<BBSPostCategory> bbsPostCategories = bbsPostCategoryService.getBBSPostCategories();
if (CollectionUtils.isEmpty(bbsPostCategories)) {
return "error/error_404";
}
//将分类数据封装到request域中
request.setAttribute("bbsPostCategories", bbsPostCategories);
// 帖子内容
BBSPost bbsPost = bbsPostService.getBBSPostForDetail(postId);
if (bbsPost == null) {
return "error/error_404";
}
request.setAttribute("bbsPost", bbsPost);
// 发帖用户信息
BBSUser bbsUser = bbsUserService.getUserById(bbsPost.getPublishUserId());
if (bbsUser == null) {
return "error/error_404";
}
request.setAttribute("bbsUser", bbsUser);
// todo 是否收藏了本贴
// 本周热议的帖子
request.setAttribute("hotTopicBBSPostList", bbsPostService.getHotTopicBBSPostList());
// todo 评论数据
return "jie/detail";
}
/**
* 跳转至编辑页
* @param request
* @param postId
* @return
*/
@GetMapping("editPostPage/{postId}")
public String editPostPage(HttpServletRequest request, @PathVariable(value = "postId") Long postId) {
BBSUser bbsUser = (BBSUser) request.getSession().getAttribute(Constants.USER_SESSION_KEY);
List<BBSPostCategory> bbsPostCategories = bbsPostCategoryService.getBBSPostCategories();
if (CollectionUtils.isEmpty(bbsPostCategories)) {
return "error/error_404";
}
//将分类数据封装到request域中
request.setAttribute("bbsPostCategories", bbsPostCategories);
if (null == postId || postId < 0) {
return "error/error_404";
}
BBSPost bbsPost = bbsPostService.getBBSPostById(postId);
if (bbsPost == null) {
return "error/error_404";
}
if (!bbsUser.getUserId().equals(bbsPost.getPublishUserId())) {
request.setAttribute("message", "非本人发帖,无权限修改");
return "error/error";
}
request.setAttribute("bbsPost", bbsPost);
request.setAttribute("postId", postId);
return "jie/edit";
}
/**
* 修改帖子
* @param postId
* @param postTitle
* @param postCategoryId
* @param postContent
* @param verifyCode
* @param httpSession
* @return
*/
@PostMapping("/editPost")
@ResponseBody
public Result editPost(@RequestParam("postId") Long postId,
@RequestParam("postTitle") String postTitle,
@RequestParam("postCategoryId") Integer postCategoryId,
@RequestParam("postContent") String postContent,
@RequestParam("verifyCode") String verifyCode,
HttpSession httpSession) {
BBSUser bbsUser = (BBSUser) httpSession.getAttribute(Constants.USER_SESSION_KEY);
if (null == postId || postId < 0) {
return ResultGenerator.genFailResult("postId参数错误");
}
BBSPost temp = bbsPostService.getBBSPostById(postId);
if (temp == null) {
return ResultGenerator.genFailResult("postId参数错误");
}
if (!bbsUser.getUserId().equals(temp.getPublishUserId())) {
return ResultGenerator.genFailResult("非本人发帖,无权限修改");
}
if (!StringUtils.hasLength(postTitle)) {
return ResultGenerator.genFailResult("postTitle参数错误");
没有合适的资源?快使用搜索试试~ 我知道了~
Spring Boot项目学习12之帖子详情与修改模块
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共463个文件
gif:150个
xml:92个
js:40个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 0 4 下载量 90 浏览量
2022-08-11
23:20:53
上传
评论
收藏 3.21MB ZIP 举报
温馨提示
Spring Boot项目学习12之帖子详情与修改模块
资源详情
资源评论
资源推荐
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
共 463 条
- 1
- 2
- 3
- 4
- 5
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![avatar](https://profile-avatar.csdnimg.cn/487548582bf44fc9be612a6fff471457_pikcacho_pkq.jpg!1)
picacho_pkq
- 粉丝: 77
- 资源: 40
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- python-leetcode面试题解之第270题最接近二叉搜索树值.zip
- python-leetcode面试题解之第267题回文排列II.zip
- python-leetcode面试题解之第264题丑数II.zip
- python-leetcode面试题解之第263题丑数.zip
- python-leetcode面试题解之第258题各位相加.zip
- python-leetcode面试题解之第257题二叉树的所有路径.zip
- python-leetcode面试题解之第253题会议室II.zip
- python-leetcode面试题解之第252题会议室.zip
- python-leetcode面试题解之第249题移位字符串分组.zip
- 基于LUT查找表方法的正弦信号产生器FPGA实现,包含testbench,包括程序,注释,操作步骤
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论0