package com.sf.controller;
import com.sf.dto.RestResp;
import com.sf.dto.req.UserCommentReqDto;
import com.sf.dto.req.UserCommentUpdateReqDto;
import com.sf.dto.resp.BookCommentRespDto;
import com.sf.service.IBookCommentService;
import com.sf.util.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
/**
*
* 小说评论 前端控制器
*
*
* @author baomidou
* @since 2024-06-12
*/
@Tag(name = "BookCommentController", description = "书籍评论模块")
@RestController
//@RequestMapping("/bookComment")
public class BookCommentController {
@Autowired
private IBookCommentService bookCommentService;
// http://127.0.0.1:8888/api/front/book/comment/newest_list?bookId=1431630596354977795
@Operation(summary = "评论列表接口")
@GetMapping("/api/front/book/comment/newest_list")
public RestResp commentNewestList(@RequestParam("bookId") Long bookId) {
BookCommentRespDto bookCommentRespDto = bookCommentService.commentNewestList(bookId);
return RestResp.ok(bookCommentRespDto);
}
// http://127.0.0.1:8888/api/front/user/comment
@Operation(summary = "添加评论接口")
@PostMapping("/api/front/user/comment")
public RestResp addComment(@RequestBody UserCommentReqDto userCommentReqDto) {
Long userId = UserHolder.getUserId();
System.out.println("BookCommentController addComment: " + userId);
if (userId == null) {
return RestResp.fail("00005", "用户登录失效了,请重新登录");
}
userCommentReqDto.setUserId(userId);
bookCommentService.saveComment(userCommentReqDto);
// 如果一个请求来了,但是又开了一个新线程,Threadlocal还能取到你说的全局id吗? 不能
// 因为ThreadLocal是对当前线程的备份 如果开启一个新的线程 获取不到之前线程的备份的
// new Thread(()->{
// Long threadUserId = UserHolder.getUserId();
// System.out.println("BookCommentController threadUserId: " + threadUserId);
// }).start();
return RestResp.ok(null);
}
// http://127.0.0.1:8888/api/front/user/comment/{id}
@Operation(summary = "删除评论接口")
@DeleteMapping("/api/front/user/comment/{id}")
public RestResp deleteComment(@Parameter(description = "评论id") @PathVariable("id") Long id) {
bookCommentService.removeById(id);
return RestResp.ok(null);
}
// http://127.0.0.1:8888/api/front/user/comment/{id}
@Operation(summary = "修改评论接口")
@PutMapping("/api/front/user/comment/{id}")
public RestResp updateComment(@PathVariable("id") Long id, String content) {
// public RestResp updateComment(@PathVariable("id") Long id, UserCommentUpdateReqDto userCommentUpdateReqDto) {
// System.out.println("BookCommentController updateComment: " + userCommentUpdateReqDto);
bookCommentService.updateComment(id, content);
return RestResp.ok(null);
}
}