complete_postservice_test_code

Change-Id: If176a0ac12d71e7695635761e7199c03926e4c85
diff --git a/src/main/java/com/example/g8backend/controller/PostController.java b/src/main/java/com/example/g8backend/controller/PostController.java
index 7b7b7c7..3557af2 100644
--- a/src/main/java/com/example/g8backend/controller/PostController.java
+++ b/src/main/java/com/example/g8backend/controller/PostController.java
@@ -54,4 +54,51 @@
     public List<Post> getPostsByUserId(@PathVariable("userId") Long userId) {
         return postService.getPostsByUserId(userId);
     }
+
+    @PutMapping("/{postId}")
+    public ResponseEntity<?> updatePost(@PathVariable("postId") Long postId, @RequestBody Post post) {
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        long userId = (long) authentication.getPrincipal();
+        Post existingPost = postService.getById(postId);
+        
+        if (existingPost == null) {
+            return ResponseEntity.status(500).body("Post not found.");
+        }
+        if (existingPost.getUserId() != userId) {
+            return ResponseEntity.status(403).body("You are not authorized to update this post.");
+        }
+        
+        post.setPostId(postId);
+        post.setUserId(userId);
+        postService.updateById(post);
+        return ResponseEntity.ok().body("Post updated successfully.");
+    }
+
+    @GetMapping("/type/{postType}")
+    public ResponseEntity<?> getPostsByType(@PathVariable String postType) {
+        List<Post> posts = postService.getPostsByType(postType);
+        return ResponseEntity.ok().body(posts);
+    }
+
+    @PostMapping("/{postId}/like")
+    public ResponseEntity<?> likePost(@PathVariable Long postId) {
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        long userId = (long) authentication.getPrincipal();
+        postService.likePost(userId, postId);
+        return ResponseEntity.ok().body("Post liked successfully.");
+    }
+
+    @DeleteMapping("/{postId}/like")
+    public ResponseEntity<?> unlikePost(@PathVariable Long postId) {
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        long userId = (long) authentication.getPrincipal();
+        postService.unlikePost(userId, postId);
+        return ResponseEntity.ok().body("Post unliked successfully.");
+    }
+
+    @GetMapping("/{postId}/likes")
+    public ResponseEntity<?> getPostLikeCount(@PathVariable Long postId) {
+        Long likeCount = postService.getPostLikeCount(postId);
+        return ResponseEntity.ok().body(likeCount);
+    }
 }
diff --git a/src/main/java/com/example/g8backend/service/IPostService.java b/src/main/java/com/example/g8backend/service/IPostService.java
index 58c13dc..f81053e 100644
--- a/src/main/java/com/example/g8backend/service/IPostService.java
+++ b/src/main/java/com/example/g8backend/service/IPostService.java
@@ -2,9 +2,14 @@
 
 import com.example.g8backend.entity.Post;
 import com.baomidou.mybatisplus.extension.service.IService;
-
 import java.util.List;
 
 public interface IPostService extends IService<Post> {
     List<Post> getPostsByUserId(Long userId);
+    Post createPost(Post post);
+    Post updatePost(Post post);
+    List<Post> getPostsByType(String postType);
+    Long getPostLikeCount(Long postId);
+    void likePost(Long userId, Long postId);
+    void unlikePost(Long userId, Long postId);
 }
diff --git a/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java b/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
index 90d353a..09a471e 100644
--- a/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
+++ b/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
@@ -1,21 +1,63 @@
 package com.example.g8backend.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.example.g8backend.entity.Post;
 import com.example.g8backend.mapper.PostMapper;
 import com.example.g8backend.service.IPostService;
 import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
-
+import java.sql.Timestamp;
 import java.util.List;
 
 @Service
 public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {
-    @Resource
-    private PostMapper postMapper;
+    
+    private final PostMapper postMapper;
+
+    public PostServiceImpl(PostMapper postMapper) {
+        this.postMapper = postMapper;
+        this.baseMapper = postMapper; // 重要:设置 baseMapper
+    }
 
     @Override
     public List<Post> getPostsByUserId(Long userId) {
         return postMapper.getPostsByUserId(userId);
     }
+
+    @Override
+    public Post createPost(Post post) {
+        post.setCreatedAt(new Timestamp(System.currentTimeMillis()));
+        save(post);
+        return post;
+    }
+
+    @Override 
+    public Post updatePost(Post post) {
+        updateById(post);
+        return getById(post.getPostId());
+    }
+
+    @Override
+    public List<Post> getPostsByType(String postType) {
+        QueryWrapper<Post> wrapper = new QueryWrapper<>();
+        wrapper.eq("post_type", postType);
+        return list(wrapper);
+    }
+
+    @Override
+    public Long getPostLikeCount(Long postId) {
+        // TODO: 需要实现post_likes表的查询
+        return 0L;
+    }
+
+    @Override
+    public void likePost(Long userId, Long postId) {
+        // TODO: 需要实现post_likes表的插入
+    }
+
+    @Override
+    public void unlikePost(Long userId, Long postId) {
+        // TODO: 需要实现post_likes表的删除
+    }
 }