bug_fix+historyview

Change-Id: I6f446c1b660a4322865cfcf5502c88cb772ca0a1
diff --git a/src/main/java/com/example/g8backend/controller/PostController.java b/src/main/java/com/example/g8backend/controller/PostController.java
index a47734a..51aa519 100644
--- a/src/main/java/com/example/g8backend/controller/PostController.java
+++ b/src/main/java/com/example/g8backend/controller/PostController.java
@@ -1,6 +1,9 @@
 package com.example.g8backend.controller;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.example.g8backend.dto.PostCreateDTO;
+import com.example.g8backend.entity.PostView;
+import com.example.g8backend.mapper.PostViewMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.security.core.Authentication;
@@ -16,6 +19,8 @@
 public class PostController {
     @Autowired
     private IPostService postService;
+    @Autowired  // ✅ 新增注入
+    private PostViewMapper postViewMapper;
 
     @PostMapping("")
     public ResponseEntity<?> createPost(@RequestBody PostCreateDTO postCreateDTO) {
@@ -34,7 +39,15 @@
     }
 
     @GetMapping("/{postId}")
-    public Post getPost(@PathVariable("postId") Long postId) {
+    public Post getPost(@PathVariable Long postId) {
+        // 获取当前用户ID
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        long userId = (long) authentication.getPrincipal();
+
+        // 记录浏览行为
+        postService.recordViewHistory(userId, postId);
+
+        // 返回帖子详情
         return postService.getById(postId);
     }
 
@@ -119,4 +132,20 @@
 
         return postService.searchPosts(keyword, tags, author);
     }
+
+    @GetMapping("/history")
+    public ResponseEntity<List<PostView>> getViewHistory() {
+        // 获取当前用户ID
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        long userId = (long) authentication.getPrincipal();
+
+        // 查询历史记录(按时间倒序)
+        List<PostView> history = postViewMapper.selectList(
+                new QueryWrapper<PostView>()
+                        .eq("user_id", userId)
+                        .orderByDesc("view_time")
+        );
+
+        return ResponseEntity.ok(history);
+    }
 }