CommentController.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package nju.seec.helper.controller;
  2. import nju.seec.helper.aspect.auth.Auth;
  3. import nju.seec.helper.controller.response.PageResponse;
  4. import nju.seec.helper.dto.CommentDTO;
  5. import nju.seec.helper.dto.LoginUser;
  6. import nju.seec.helper.service.CommentService;
  7. import nju.seec.helper.util.JsonUtils;
  8. import nju.seec.helper.util.enums.UserType;
  9. import nju.seec.helper.vo.CommentVO;
  10. import org.springframework.data.domain.Pageable;
  11. import org.springframework.data.web.PageableDefault;
  12. import org.springframework.web.bind.annotation.*;
  13. import javax.validation.Valid;
  14. /**
  15. * 评论
  16. *
  17. * @author cst
  18. */
  19. @RestController
  20. @RequestMapping("/api/comment")
  21. public class CommentController {
  22. private final CommentService commentService;
  23. public CommentController(CommentService commentService) {
  24. this.commentService = commentService;
  25. }
  26. /**
  27. * 发表评论
  28. */
  29. @Auth(roles = {UserType.TEACHER, UserType.STUDENT}, message = "发表评论")
  30. @PostMapping
  31. public CommentVO createComment(LoginUser user,
  32. @Valid @RequestBody CommentDTO commentDTO) {
  33. CommentVO commentVO = commentService.createComment(user, commentDTO);
  34. CommentWebsocket.sendAllMessage(commentDTO.getSlideId(), JsonUtils.toJson(commentVO));
  35. return commentVO;
  36. }
  37. /**
  38. * 删除评论
  39. */
  40. @Auth(roles = {UserType.TEACHER, UserType.STUDENT}, message = "删除评论")
  41. @DeleteMapping("/{commentId}")
  42. public void removeComment(LoginUser user,
  43. @PathVariable Integer commentId) {
  44. commentService.removeComment(user, commentId);
  45. }
  46. /**
  47. * 教师置顶评论
  48. */
  49. @Auth(roles = UserType.TEACHER, message = "置顶评论")
  50. @PostMapping("/{commentId}/top")
  51. public void topComment(LoginUser user,
  52. @PathVariable Integer commentId) {
  53. commentService.topComment(user, commentId);
  54. }
  55. /**
  56. * 教师取消置顶评论
  57. */
  58. @Auth(roles = UserType.TEACHER, message = "取消置顶评论")
  59. @PostMapping("/{commentId}/unTop")
  60. public void unTopComment(LoginUser user,
  61. @PathVariable Integer commentId) {
  62. commentService.unTopComment(user, commentId);
  63. }
  64. /**
  65. * 取得某一页的评论信息
  66. */
  67. @GetMapping("/slide/{slideId}")
  68. public PageResponse<CommentVO> getComments(@PathVariable Integer slideId,
  69. @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
  70. @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
  71. return PageResponse.of(commentService.getCommentsBySlideIdAndPageNumber(slideId, pageNumber, pageable));
  72. }
  73. }