BookCommentController.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.sf.controller;
  2. import com.sf.dto.RestResp;
  3. import com.sf.dto.req.UserCommentReqDto;
  4. import com.sf.dto.req.UserCommentUpdateReqDto;
  5. import com.sf.dto.resp.BookCommentRespDto;
  6. import com.sf.service.IBookCommentService;
  7. import com.sf.util.UserHolder;
  8. import io.swagger.v3.oas.annotations.Operation;
  9. import io.swagger.v3.oas.annotations.Parameter;
  10. import io.swagger.v3.oas.annotations.tags.Tag;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.*;
  13. import org.springframework.stereotype.Controller;
  14. /**
  15. * <p>
  16. * 小说评论 前端控制器
  17. * </p>
  18. *
  19. * @author baomidou
  20. * @since 2024-06-12
  21. */
  22. @Tag(name = "BookCommentController", description = "书籍评论模块")
  23. @RestController
  24. //@RequestMapping("/bookComment")
  25. public class BookCommentController {
  26. @Autowired
  27. private IBookCommentService bookCommentService;
  28. // http://127.0.0.1:8888/api/front/book/comment/newest_list?bookId=1431630596354977795
  29. @Operation(summary = "评论列表接口")
  30. @GetMapping("/api/front/book/comment/newest_list")
  31. public RestResp<BookCommentRespDto> commentNewestList(@RequestParam("bookId") Long bookId) {
  32. BookCommentRespDto bookCommentRespDto = bookCommentService.commentNewestList(bookId);
  33. return RestResp.ok(bookCommentRespDto);
  34. }
  35. // http://127.0.0.1:8888/api/front/user/comment
  36. @Operation(summary = "添加评论接口")
  37. @PostMapping("/api/front/user/comment")
  38. public RestResp<Void> addComment(@RequestBody UserCommentReqDto userCommentReqDto) {
  39. Long userId = UserHolder.getUserId();
  40. System.out.println("BookCommentController addComment: " + userId);
  41. if (userId == null) {
  42. return RestResp.fail("00005", "用户登录失效了,请重新登录");
  43. }
  44. userCommentReqDto.setUserId(userId);
  45. bookCommentService.saveComment(userCommentReqDto);
  46. // 如果一个请求来了,但是又开了一个新线程,Threadlocal还能取到你说的全局id吗? 不能
  47. // 因为ThreadLocal是对当前线程的备份 如果开启一个新的线程 获取不到之前线程的备份的
  48. // new Thread(()->{
  49. // Long threadUserId = UserHolder.getUserId();
  50. // System.out.println("BookCommentController threadUserId: " + threadUserId);
  51. // }).start();
  52. return RestResp.ok(null);
  53. }
  54. // http://127.0.0.1:8888/api/front/user/comment/{id}
  55. @Operation(summary = "删除评论接口")
  56. @DeleteMapping("/api/front/user/comment/{id}")
  57. public RestResp<Void> deleteComment(@Parameter(description = "评论id") @PathVariable("id") Long id) {
  58. bookCommentService.removeById(id);
  59. return RestResp.ok(null);
  60. }
  61. // http://127.0.0.1:8888/api/front/user/comment/{id}
  62. @Operation(summary = "修改评论接口")
  63. @PutMapping("/api/front/user/comment/{id}")
  64. public RestResp<Void> updateComment(@PathVariable("id") Long id, String content) {
  65. // public RestResp<Void> updateComment(@PathVariable("id") Long id, UserCommentUpdateReqDto userCommentUpdateReqDto) {
  66. // System.out.println("BookCommentController updateComment: " + userCommentUpdateReqDto);
  67. bookCommentService.updateComment(id, content);
  68. return RestResp.ok(null);
  69. }
  70. }