初步实现资源与下载的框架,后续在TODO处完善即可。

Change-Id: Ib12b43a9f873f7b2ab65f3790c0e1015f18407ce
diff --git a/src/main/java/com/pt/Item/ResourceInfo.java b/src/main/java/com/pt/Item/ResourceInfo.java
new file mode 100644
index 0000000..96e5989
--- /dev/null
+++ b/src/main/java/com/pt/Item/ResourceInfo.java
@@ -0,0 +1,96 @@
+package com.pt.Item;
+
+import java.time.LocalDateTime;
+
+public class ResourceInfo {
+
+    private int resourceId;
+
+    private String name;
+    private double size;
+
+    private LocalDateTime publishTime;
+    private String author;
+
+    private String description;
+
+    private int seedCount;
+    private int downloadCount;
+
+    public ResourceInfo() {
+    }
+
+    public ResourceInfo(int resourceId, String name, double size, LocalDateTime publishTime, String author) {
+        this.resourceId = resourceId;
+        this.name = name;
+        this.size = size;
+        this.publishTime = publishTime;
+        this.author = author;
+    }
+
+    public int getResourceId() {
+        return resourceId;
+    }
+    public void setResourceId(int resourceId) {
+        this.resourceId = resourceId;
+    }
+    public String getName() {
+        return name;
+    }
+    public void setName(String name) {
+        this.name = name;
+    }
+    public double getSize() {
+        return size;
+    }
+    public void setSize(double size) {
+        this.size = size;
+    }
+    public LocalDateTime getPublishTime() {
+        return publishTime;
+    }
+    public void setPublishTime(LocalDateTime publishTime) {
+        this.publishTime = publishTime;
+    }
+    public String getAuthor() {
+        return author;
+    }
+    public void setAuthor(String author) {
+        this.author = author;
+    }
+    public String getDescription() {
+        return description;
+    }
+    public void setDescription(String description) {
+        this.description = description;
+    }
+    public int getSeedCount() {
+        return seedCount;
+    }
+    public void setSeedCount(int seedCount) {
+        this.seedCount = seedCount;
+    }
+    public int getDownloadCount() {
+        return downloadCount;
+    }
+    public void setDownloadCount(int downloadCount) {
+        this.downloadCount = downloadCount;
+    }
+
+    /*
+        * 重写toString方法,将资源信息以JSON字符串形式返回
+     */
+    @Override
+    public String toString() {
+        return "{" +
+                "\"resourceId\":" + resourceId +
+                ", \"name\":\"" + name + "\"" +
+                ", \"size\":" + size +
+                ", \"publishTime\":\"" + publishTime + "\"" +
+                ", \"author\":\"" + author + "\"" +
+                ", \"description\":\"" + description + "\"" +
+                ", \"seedCount\":" + seedCount +
+                ", \"downloadCount\":" + downloadCount +
+                '}';
+    }
+}
diff --git a/src/main/java/com/pt/controller/ResourceController.java b/src/main/java/com/pt/controller/ResourceController.java
index 5ed01f8..caf2050 100644
--- a/src/main/java/com/pt/controller/ResourceController.java
+++ b/src/main/java/com/pt/controller/ResourceController.java
@@ -1,8 +1,10 @@
 package com.pt.controller;
 
+import com.pt.Item.ResourceInfo;
 import com.pt.constant.Constants;
 import com.pt.entity.Resource;
 import com.pt.entity.User;
+import com.pt.service.DownloadService;
 import com.pt.service.ResourceService;
 import com.pt.service.UserService;
 import com.pt.utils.JWTUtils;
@@ -10,6 +12,7 @@
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -22,6 +25,7 @@
     @Autowired
     private ResourceService resourceService;
     private UserService userService;
+    private DownloadService downloadService;
 
     @GetMapping("/list/all")
     public ResponseEntity<?> getAllResources(@RequestHeader("token") String token,
@@ -37,7 +41,27 @@
         if (resources.isEmpty()) {
             return ResponseEntity.noContent().build();
         }
-        return ResponseEntity.ok(resources);
+
+        List<ResourceInfo> resourceInfos = new ArrayList<>();
+        for (Resource resource : resources) {
+            ResourceInfo resourceInfo = new ResourceInfo();
+            resourceInfo.setResourceId(resource.getResourceId());
+            resourceInfo.setName(resource.getName());
+            resourceInfo.setSize(resource.getSize());
+            resourceInfo.setDescription(resource.getDescription());
+            resourceInfo.setAuthor(resource.getAuthor());
+            resourceInfo.setPublishTime(resource.getPublishTime());
+
+            int downloadCount = downloadService.searchByResourceId(resource.getResourceId()).size();
+            int seedCount = 0; // TODO: 需要根据tracker信息计算该资源的种子树
+            resourceInfo.setDownloadCount(downloadCount);
+            resourceInfo.setSeedCount(seedCount);
+            resourceInfos.add(resourceInfo);
+        }
+
+        ans.put("message", "Resources retrieved successfully");
+        ans.put("resources", resourceInfos);
+        return ResponseEntity.ok(ans);
     }
 
     @GetMapping("/list/user")
@@ -76,12 +100,14 @@
             return ResponseEntity.status(403).body(ans);
         }
 
-        resourceService.publishResource(name, description, username, size);
-
         /*
             * TODO: 在这里实现资源发布的逻辑
+            *  需要将资源同步到Tracker服务器
+            *  种子文件需要保存
          */
 
+        resourceService.publishResource(name, description, username, size);
+
         ans.put("result", "Resource published successfully");
         return ResponseEntity.ok(ans);
     }
@@ -117,6 +143,7 @@
 
         /*
             * TODO: 在这里实现下载资源的方法
+            *       从本地的种子文件目录获取种子文件
          */
 
         // Here you would typically return the file or a download link
@@ -154,7 +181,8 @@
         }
 
         /*
-            TODO: 在这里实现删除资源的方法
+            * TODO: 在这里实现删除资源的方法
+            *  需要同步到Tracker服务器
          */
 
 
diff --git a/src/main/java/com/pt/entity/Download.java b/src/main/java/com/pt/entity/Download.java
index 38b955a..1ae9354 100644
--- a/src/main/java/com/pt/entity/Download.java
+++ b/src/main/java/com/pt/entity/Download.java
@@ -14,14 +14,14 @@
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private int downloadId;
 
-    private String resourceId;
+    private int resourceId;
     private String downloader;
     private LocalDateTime downloadTime;
 
     public Download() {
     }
 
-    public Download(int downloadId, String resourceId, String downloader, LocalDateTime downloadTime) {
+    public Download(int downloadId, int resourceId, String downloader, LocalDateTime downloadTime) {
         this.downloadId = downloadId;
         this.resourceId = resourceId;
         this.downloader = downloader;
@@ -34,10 +34,10 @@
     public void setDownloadId(int downloadId) {
         this.downloadId = downloadId;
     }
-    public String getResourceId() {
+    public int getResourceId() {
         return resourceId;
     }
-    public void setResourceId(String resourceId) {
+    public void setResourceId(int resourceId) {
         this.resourceId = resourceId;
     }
     public String getDownloader() {
diff --git a/src/main/java/com/pt/repository/DownloadRepository.java b/src/main/java/com/pt/repository/DownloadRepository.java
index 741f142..360a1c0 100644
--- a/src/main/java/com/pt/repository/DownloadRepository.java
+++ b/src/main/java/com/pt/repository/DownloadRepository.java
@@ -12,7 +12,7 @@
      * @param resourceId the ID of the resource
      * @return a list of downloads associated with the specified resource
      */
-    List<Download> findByResourceId(String resourceId);
+    List<Download> findByResourceId(int resourceId);
 
     /**
      * Finds downloads by the downloader's username.
diff --git a/src/main/java/com/pt/service/DownloadService.java b/src/main/java/com/pt/service/DownloadService.java
index 284be5a..c02aea1 100644
--- a/src/main/java/com/pt/service/DownloadService.java
+++ b/src/main/java/com/pt/service/DownloadService.java
@@ -1,9 +1,12 @@
 package com.pt.service;
 
+import com.pt.entity.Download;
 import com.pt.repository.DownloadRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+
 @Service
 public class DownloadService {
 
@@ -14,6 +17,9 @@
         this.downloadRepository = downloadRepository;
     }
 
+    public List<Download> searchByResourceId(int resourceId) {
+        return downloadRepository.findByResourceId(resourceId);
+    }
     /*
         TODO: 添加下载需要的服务;
      */