add post,add redis in docker compose,add torrentutil test remove dockerfile, modify torrentutil

Change-Id: I8014d4994d0a09c2f28cfcf0f8d2a430372aaab5
diff --git a/src/main/java/com/example/g8backend/controller/AuthController.java b/src/main/java/com/example/g8backend/controller/AuthController.java
index 2f36500..e824b63 100644
--- a/src/main/java/com/example/g8backend/controller/AuthController.java
+++ b/src/main/java/com/example/g8backend/controller/AuthController.java
@@ -66,7 +66,7 @@
 
         // passkey 用于在客户端发送announce请求时获取用户信息
         user.setPasskey(UUID.randomUUID().toString().replace("-", ""));
-        userService.registerUser(user);
+        userService.save(user);
 
         return ResponseEntity.ok("注册成功");
     }
diff --git a/src/main/java/com/example/g8backend/controller/PostController.java b/src/main/java/com/example/g8backend/controller/PostController.java
new file mode 100644
index 0000000..7b7b7c7
--- /dev/null
+++ b/src/main/java/com/example/g8backend/controller/PostController.java
@@ -0,0 +1,57 @@
+package com.example.g8backend.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.web.bind.annotation.*;
+import com.example.g8backend.entity.Post;
+import com.example.g8backend.service.IPostService;
+
+import java.util.List;
+
+@RestController
+@RequestMapping("/post")
+public class PostController {
+    @Autowired
+    private IPostService postService;
+
+    @PostMapping("")
+    public ResponseEntity<?> createPost(@RequestBody Post post) {
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        long userId = (long) authentication.getPrincipal();
+        post.setUserId(userId);
+        postService.save(post);
+        return ResponseEntity.ok().build();
+    }
+
+    @GetMapping("/{postId}")
+    public Post getPost(@PathVariable("postId") Long postId) {
+        return postService.getById(postId);
+    }
+
+    @DeleteMapping("/{postId}")
+    public ResponseEntity<?> deletePost(@PathVariable("postId") Long postId) {
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        long userId = (long) authentication.getPrincipal();
+        Post post = postService.getById(postId);
+        if (post == null) {
+            return ResponseEntity.status(500).body("Post not found.");
+        }
+        if (post.getUserId()!= userId) {
+            return ResponseEntity.status(403).body("You are not authorized to delete this post.");
+        }
+        postService.removeById(postId);
+        return ResponseEntity.ok().body("Post deleted successfully.");
+    }
+
+    @GetMapping("/getAll")
+    public List<Post> getAllPosts() {
+        return postService.list();
+    }
+
+    @GetMapping("/getByUserId/{userId}")
+    public List<Post> getPostsByUserId(@PathVariable("userId") Long userId) {
+        return postService.getPostsByUserId(userId);
+    }
+}
diff --git a/src/main/java/com/example/g8backend/controller/TorrentController.java b/src/main/java/com/example/g8backend/controller/TorrentController.java
index 5a8b473..0c016f4 100644
--- a/src/main/java/com/example/g8backend/controller/TorrentController.java
+++ b/src/main/java/com/example/g8backend/controller/TorrentController.java
@@ -37,7 +37,11 @@
         File tempFile = File.createTempFile("upload-", ".torrent");
         multipartFile.transferTo(tempFile);
 
-        torrentService.handleTorrentUpload(tempFile, userId, passkey);
+        try {
+            torrentService.handleTorrentUpload(tempFile, userId, passkey);
+        } catch (IllegalArgumentException e) {
+            return ResponseEntity.badRequest().body(e.getMessage());
+        }
 
         // 删除临时文件
         if(!tempFile.delete()){
diff --git a/src/main/java/com/example/g8backend/entity/Post.java b/src/main/java/com/example/g8backend/entity/Post.java
new file mode 100644
index 0000000..ac7ff20
--- /dev/null
+++ b/src/main/java/com/example/g8backend/entity/Post.java
@@ -0,0 +1,33 @@
+package com.example.g8backend.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+
+import java.sql.Timestamp;
+import lombok.Data;
+
+@Data
+@TableName("posts")
+public class Post {
+    @TableId(type = IdType.AUTO)
+    private Long postId;
+
+    private Long userId;
+    private String postTitle;
+    private String postContent;
+    private Timestamp createdAt;
+    private String postType;
+
+    @Override
+    public String toString() {
+        return "Post{" +
+                "postId=" + postId +
+                ", userId=" + userId +
+                ", postTitle='" + postTitle + '\'' +
+                ", postContent='" + postContent + '\'' +
+                ", createdAt=" + createdAt +
+                ", postType='" + postType + '\'' +
+                '}';
+    }
+}
diff --git a/src/main/java/com/example/g8backend/mapper/PostMapper.java b/src/main/java/com/example/g8backend/mapper/PostMapper.java
new file mode 100644
index 0000000..6db4e67
--- /dev/null
+++ b/src/main/java/com/example/g8backend/mapper/PostMapper.java
@@ -0,0 +1,13 @@
+package com.example.g8backend.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.g8backend.entity.Post;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+@Mapper
+public interface PostMapper extends BaseMapper<Post> {
+    List<Post> getPostsByUserId(@Param("userId") Long userId);
+}
diff --git a/src/main/java/com/example/g8backend/service/IPostService.java b/src/main/java/com/example/g8backend/service/IPostService.java
new file mode 100644
index 0000000..58c13dc
--- /dev/null
+++ b/src/main/java/com/example/g8backend/service/IPostService.java
@@ -0,0 +1,10 @@
+package com.example.g8backend.service;
+
+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);
+}
diff --git a/src/main/java/com/example/g8backend/service/IUserService.java b/src/main/java/com/example/g8backend/service/IUserService.java
index f407c37..af94d27 100644
--- a/src/main/java/com/example/g8backend/service/IUserService.java
+++ b/src/main/java/com/example/g8backend/service/IUserService.java
@@ -8,5 +8,4 @@
     User getUserByName(@Param("name") String name);
     User getUserByEmail(@Param("email") String email);
     User getUserByPasskey(@Param("passkey") String passkey);
-    void registerUser(User user);;
 }
diff --git a/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java b/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
new file mode 100644
index 0000000..90d353a
--- /dev/null
+++ b/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
@@ -0,0 +1,21 @@
+package com.example.g8backend.service.impl;
+
+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.util.List;
+
+@Service
+public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {
+    @Resource
+    private PostMapper postMapper;
+
+    @Override
+    public List<Post> getPostsByUserId(Long userId) {
+        return postMapper.getPostsByUserId(userId);
+    }
+}
diff --git a/src/main/java/com/example/g8backend/service/impl/TorrentServiceImpl.java b/src/main/java/com/example/g8backend/service/impl/TorrentServiceImpl.java
index 8d9ae74..bedb94a 100644
--- a/src/main/java/com/example/g8backend/service/impl/TorrentServiceImpl.java
+++ b/src/main/java/com/example/g8backend/service/impl/TorrentServiceImpl.java
@@ -20,7 +20,7 @@
     String tracker = "http://127.0.0.1:8080/announce/";
 
     @Override
-    public Torrent handleTorrentUpload(File file, Long userId, String passkey) throws IOException{
+    public Torrent handleTorrentUpload(File file, Long userId, String passkey) throws IOException, IllegalArgumentException {
         // 修改 announce 字段
         byte[] modifiedBytes = TorrentUtil.injectTracker(file, tracker + passkey);
 
diff --git a/src/main/java/com/example/g8backend/service/impl/UserServiceImpl.java b/src/main/java/com/example/g8backend/service/impl/UserServiceImpl.java
index 3f3357c..e8bcf20 100644
--- a/src/main/java/com/example/g8backend/service/impl/UserServiceImpl.java
+++ b/src/main/java/com/example/g8backend/service/impl/UserServiceImpl.java
@@ -22,6 +22,4 @@
     @Override
     public User getUserByPasskey(String passkey) { return userMapper.getUserByPasskey(passkey);}
 
-    @Override
-    public void registerUser(User user) {userMapper.insert(user);}
 }
diff --git a/src/main/java/com/example/g8backend/util/TorrentUtil.java b/src/main/java/com/example/g8backend/util/TorrentUtil.java
index f644fff..e6c1f3b 100644
--- a/src/main/java/com/example/g8backend/util/TorrentUtil.java
+++ b/src/main/java/com/example/g8backend/util/TorrentUtil.java
@@ -22,10 +22,14 @@
         return bencode.encode(torrentMap);
     }
 
-    public static String getInfoHash(File torrentFile) throws IOException {
+    public static String getInfoHash(File torrentFile) throws IOException, IllegalArgumentException {
         byte[] fileBytes = readBytes(torrentFile);
         Map<String, Object> torrentMap = bencode.decode(fileBytes, Type.DICTIONARY);
 
+        if (!torrentMap.containsKey("info")) {
+            throw new IllegalArgumentException("Invalid torrent file: missing 'info' dictionary.");
+        }
+
         @SuppressWarnings("unchecked")
         Map<String, Object> info = (Map<String, Object>) torrentMap.get("info");
 
@@ -56,7 +60,7 @@
         }
     }
 
-    private static String bytesToHex(byte[] hash) {
+    public static String bytesToHex(byte[] hash) {
         StringBuilder hex = new StringBuilder();
         for (byte b : hash) {
             hex.append(String.format("%02x", b));