package nju.seec.helper.controller; import nju.seec.helper.sys.dataanalysis.constant.EventAction; import nju.seec.helper.sys.dataanalysis.constant.EventType; import nju.seec.helper.aspect.auth.Auth; import nju.seec.helper.aspect.auth.LoginUser; import nju.seec.helper.aspect.event.Event; import nju.seec.helper.controller.response.PageResponse; import nju.seec.helper.dto.comment.CommentDTO; import nju.seec.helper.enums.UserRole; import nju.seec.helper.service.CommentService; import nju.seec.helper.util.JsonUtils; import nju.seec.helper.vo.CommentVO; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.constraints.NotNull; /** * 评论 * * @author cst */ @Validated @RestController @RequestMapping("/api/comment") public class CommentController { private final CommentService commentService; public CommentController(CommentService commentService) { this.commentService = commentService; } /** * 发表评论 */ @Event(type = EventType.POST, action = EventAction.INITIATE) @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "发表评论") @PostMapping public CommentVO createComment(LoginUser user, @Validated @RequestBody CommentDTO commentDTO) { CommentVO commentVO = commentService.createComment(user, commentDTO); CommentWebsocket.sendAllMessageToOneSlide(commentDTO.getSlideId(), JsonUtils.toJson(commentVO)); return commentVO; } /** * 删除评论 */ @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "删除评论") @DeleteMapping("/{commentId}") public void deleteComment(LoginUser user, @PathVariable Long commentId) { commentService.deleteComment(user, commentId); } /** * 教师置顶评论 */ @Auth(roles = UserRole.TEACHER, message = "置顶评论") @PostMapping("/{commentId}/top") public void topComment(LoginUser user, @PathVariable Long commentId) { commentService.topComment(user, commentId); } /** * 教师取消置顶评论 */ @Auth(roles = UserRole.TEACHER, message = "取消置顶评论") @PostMapping("/{commentId}/unTop") public void unTopComment(LoginUser user, @PathVariable Long commentId) { commentService.unTopComment(user, commentId); } /** * 取得某一页的评论信息 */ @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获取评论") @GetMapping("/slide/{slideId}") public PageResponse getComments(@PathVariable Long slideId, @RequestParam(required = false, defaultValue = "1") Integer pageNumber, @PageableDefault(Integer.MAX_VALUE) Pageable pageable) { return PageResponse.of(commentService.getCommentsBySlideAndPageNumber(slideId, pageNumber, pageable)); } /** * 取得某一评论 */ @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获取评论") @GetMapping("/{commentId}") public CommentVO getOneComment(LoginUser user, @PathVariable Long commentId) { return commentService.getOneComment(user, commentId); } /** * 取得自己某一页的评论 */ @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获取自己的评论") @GetMapping("/slide/{slideId}/self") public PageResponse getSelfComments(LoginUser user, @PathVariable Long slideId, @RequestParam(required = false, defaultValue = "1") Integer pageNumber, @RequestParam(required = false, defaultValue = "true") Boolean show, @PageableDefault(Integer.MAX_VALUE) Pageable pageable) { return PageResponse.of(commentService.getSelfCommentsBySlideAndPageNumber(user, slideId, pageNumber, show, pageable)); } /** * 修改评论的展示状态 */ @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "修改评论展示状态") @PutMapping("/{commentId}/show") public void modifyCommentShow(LoginUser user, @PathVariable Long commentId, @NotNull(message = "缺少展示状态") Boolean show) { commentService.modifyCommentShow(user, commentId, show); } @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获取自己的评论") @GetMapping("/self") public PageResponse getSelfComments(LoginUser user, @RequestParam(required = false, defaultValue = "") String key, @PageableDefault(Integer.MAX_VALUE) Pageable pageable) { return PageResponse.of(commentService.getSelfComments(user, key, pageable)); } }