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.BBSPostCommentService;
import top.picacho.bbs.service.BBSPostService;
import top.picacho.bbs.service.BBSUserService;
import top.picacho.bbs.util.PageResult;
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;
@Resource
private BBSPostCommentService bbsPostCommentService;
/**
* 跳转至发帖页面
* @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 评论数据
PageResult commentsPage = bbsPostCommentService.getCommentsByPostId(postId, commentPage);
request.setAttribute("commentsPage", commentsPage);
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("po
评论0