CommentController.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package nju.seec.helper.controller;
  2. import nju.seec.helper.api.dataanalysis.constant.EventAction;
  3. import nju.seec.helper.api.dataanalysis.constant.EventType;
  4. import nju.seec.helper.aspect.auth.Auth;
  5. import nju.seec.helper.aspect.auth.LoginUser;
  6. import nju.seec.helper.aspect.event.Event;
  7. import nju.seec.helper.controller.response.PageResponse;
  8. import nju.seec.helper.dto.comment.CommentDTO;
  9. import nju.seec.helper.enums.UserRole;
  10. import nju.seec.helper.service.CommentService;
  11. import nju.seec.helper.util.JsonUtils;
  12. import nju.seec.helper.vo.CommentVO;
  13. import org.springframework.data.domain.Pageable;
  14. import org.springframework.data.web.PageableDefault;
  15. import org.springframework.validation.annotation.Validated;
  16. import org.springframework.web.bind.annotation.*;
  17. import javax.validation.constraints.NotNull;
  18. /**
  19. * 评论
  20. *
  21. * @author cst
  22. */
  23. @Validated
  24. @RestController
  25. @RequestMapping("/api/comment")
  26. public class CommentController {
  27. private final CommentService commentService;
  28. public CommentController(CommentService commentService) {
  29. this.commentService = commentService;
  30. }
  31. /**
  32. * 发表评论
  33. */
  34. @Event(type = EventType.POST, action = EventAction.INITIATE)
  35. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "发表评论")
  36. @PostMapping
  37. public CommentVO createComment(LoginUser user,
  38. @Validated @RequestBody CommentDTO commentDTO) {
  39. CommentVO commentVO = commentService.createComment(user, commentDTO);
  40. CommentWebsocket.sendAllMessageToOneSlide(commentDTO.getSlideId(), JsonUtils.toJson(commentVO));
  41. return commentVO;
  42. }
  43. /**
  44. * 删除评论
  45. */
  46. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "删除评论")
  47. @DeleteMapping("/{commentId}")
  48. public void deleteComment(LoginUser user,
  49. @PathVariable Long commentId) {
  50. commentService.deleteComment(user, commentId);
  51. }
  52. /**
  53. * 教师置顶评论
  54. */
  55. @Auth(roles = UserRole.TEACHER, message = "置顶评论")
  56. @PostMapping("/{commentId}/top")
  57. public void topComment(LoginUser user,
  58. @PathVariable Long commentId) {
  59. commentService.topComment(user, commentId);
  60. }
  61. /**
  62. * 教师取消置顶评论
  63. */
  64. @Auth(roles = UserRole.TEACHER, message = "取消置顶评论")
  65. @PostMapping("/{commentId}/unTop")
  66. public void unTopComment(LoginUser user,
  67. @PathVariable Long commentId) {
  68. commentService.unTopComment(user, commentId);
  69. }
  70. /**
  71. * 取得某一页的评论信息
  72. */
  73. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获取评论")
  74. @GetMapping("/slide/{slideId}")
  75. public PageResponse<CommentVO> getComments(@PathVariable Long slideId,
  76. @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
  77. @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
  78. return PageResponse.of(commentService.getCommentsBySlideAndPageNumber(slideId, pageNumber, pageable));
  79. }
  80. /**
  81. * 取得某一评论
  82. */
  83. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获取评论")
  84. @GetMapping("/{commentId}")
  85. public CommentVO getOneComment(LoginUser user, @PathVariable Long commentId) {
  86. return commentService.getOneComment(user, commentId);
  87. }
  88. /**
  89. * 取得自己某一页的评论
  90. */
  91. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获取自己的评论")
  92. @GetMapping("/slide/{slideId}/self")
  93. public PageResponse<CommentVO> getSelfComments(LoginUser user,
  94. @PathVariable Long slideId,
  95. @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
  96. @RequestParam(required = false, defaultValue = "true") Boolean show,
  97. @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
  98. return PageResponse.of(commentService.getSelfCommentsBySlideAndPageNumber(user, slideId, pageNumber, show, pageable));
  99. }
  100. /**
  101. * 修改评论的展示状态
  102. */
  103. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "修改评论展示状态")
  104. @PutMapping("/{commentId}/show")
  105. public void modifyCommentShow(LoginUser user,
  106. @PathVariable Long commentId,
  107. @NotNull(message = "缺少展示状态") Boolean show) {
  108. commentService.modifyCommentShow(user, commentId, show);
  109. }
  110. }