fix some api
Change-Id: I7179a115c9ed3e7d3473c1761df1bb4f2bf3711e
diff --git a/src/main/java/com/example/g8backend/controller/TorrentController.java b/src/main/java/com/example/g8backend/controller/TorrentController.java
index ea7fdf2..e9216f8 100644
--- a/src/main/java/com/example/g8backend/controller/TorrentController.java
+++ b/src/main/java/com/example/g8backend/controller/TorrentController.java
@@ -16,6 +16,8 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
@RestController
@RequestMapping("/torrent")
@@ -29,7 +31,7 @@
// 处理种子文件上传
@PostMapping("/upload")
- public ApiResponse<String> handleTorrentUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
+ public ApiResponse<Map> handleTorrentUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
long userId = (long) authentication.getPrincipal();
@@ -50,9 +52,9 @@
File tempFile = File.createTempFile("upload-", ".torrent");
multipartFile.transferTo(tempFile);
-
+ Torrent newTorrent;
try {
- torrentService.handleTorrentUpload(tempFile, fileName, userId, passkey);
+ newTorrent = torrentService.handleTorrentUpload(tempFile, fileName, userId, passkey);
} catch (IllegalArgumentException e) {
return ApiResponse.error(400, e.getMessage());
}
@@ -61,7 +63,9 @@
if (!tempFile.delete()) {
throw new IOException("Failed to delete temporary file: " + tempFile.getAbsolutePath());
}
- return ApiResponse.success("种子上传成功");
+ HashMap<String, Long> response = new HashMap<>();
+ response.put("torrentId", newTorrent.getTorrentId());
+ return ApiResponse.success(response);
}
// 下载种子文件
diff --git a/src/main/java/com/example/g8backend/controller/UserController.java b/src/main/java/com/example/g8backend/controller/UserController.java
index f9d0de4..a41e4d7 100644
--- a/src/main/java/com/example/g8backend/controller/UserController.java
+++ b/src/main/java/com/example/g8backend/controller/UserController.java
@@ -9,6 +9,7 @@
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import com.example.g8backend.service.ISigningService;
@@ -23,14 +24,42 @@
@Autowired
private IUserService userService;
- // 获取已登录的用户信息
@GetMapping
- public ApiResponse<User> getUserInfo(){
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
- long userId = (long) authentication.getPrincipal();
+ public ApiResponse<Map<String, Object>> getCurrentUserInfo() {
+ Long userId = getCurrentUserId();
+ return getUserInfo(userId);
+ }
+
+ @GetMapping("/{userId}")
+ public ApiResponse<Map<String, Object>> getOtherUserInfo(@PathVariable(required = false) Long userId){
+ return getUserInfo(userId);
+ }
+
+ @GetMapping("/{userId}/follow-status")
+ public ApiResponse<Map<String, Boolean>> getFollowStatus(@PathVariable Long userId) {
+ Long currentUserId = getCurrentUserId();
+ boolean isFollowing = userService.isFollowing(currentUserId, userId);
+ return ApiResponse.success(Map.of("isFollowing", isFollowing));
+ }
+
+ private ApiResponse<Map<String, Object>> getUserInfo(Long userId) {
User user = userService.getById(userId);
- user.setPassword(null); // 不返回密码
- return ApiResponse.success(user);
+ if (user == null) {
+ return ApiResponse.error(404, "用户不存在");
+ }
+ user.setPassword(null);
+ user.setPasskey(null);
+
+ int followingCount = userService.getFollowingsCount(userId);
+ int followersCount = userService.getFollowersCount(userId);
+
+ return ApiResponse.success(Map.of(
+ "userInfo", user,
+ "statistics", Map.of(
+ "followingCount", followingCount,
+ "followersCount", followersCount
+ )
+ ));
}
// ==================== 关注功能 ====================
diff --git a/src/main/java/com/example/g8backend/mapper/UserMapper.java b/src/main/java/com/example/g8backend/mapper/UserMapper.java
index 98e6c67..f913b0f 100644
--- a/src/main/java/com/example/g8backend/mapper/UserMapper.java
+++ b/src/main/java/com/example/g8backend/mapper/UserMapper.java
@@ -18,4 +18,17 @@
@Update("UPDATE users SET user_level = #{userLevel} WHERE user_id = #{userId}")
int updateUserLevel(@Param("userId") Long userId, @Param("userLevel") String userLevel);
+
+ @Select("SELECT COUNT(*) FROM user_follows WHERE followed_id = #{userId}")
+ int countFollowings(Long userId);
+
+ @Select("SELECT COUNT(*) FROM user_follows WHERE follower_id = #{userId}")
+ int countFollowers(Long userId);
+
+ @Select("SELECT COUNT(*) FROM user_follows " +
+ "WHERE follower_id = #{currentUserId} AND followed_id = #{targetUserId}")
+ int existsFollowRelationship(
+ @Param("currentUserId") Long currentUserId,
+ @Param("targetUserId") Long targetUserId
+ );
}
diff --git a/src/main/java/com/example/g8backend/scheduler/ColdTorrentRefreshScheduler.java b/src/main/java/com/example/g8backend/scheduler/ColdTorrentRefreshScheduler.java
index bf88d07..68ed6fb 100644
--- a/src/main/java/com/example/g8backend/scheduler/ColdTorrentRefreshScheduler.java
+++ b/src/main/java/com/example/g8backend/scheduler/ColdTorrentRefreshScheduler.java
@@ -18,14 +18,14 @@
this.recommendationService = recommendationService;
}
- @Scheduled(fixedRate = 5000)
+ @Scheduled(fixedRate = 50000)
public void refreshColdTorrents() {
// 自动刷新数据库中的israre字段,保持前10个冷门标记
recommendationService.refreshIsRareField(10);
// 获取刷新后的冷门数据返回(可选打印日志)
List<TorrentRecommendationDTO> coldTorrents = recommendationService.getColdTorrentRecommendations(10);
- System.out.println("【定时任务】刷新冷门种子列表,条数:" + coldTorrents.size());
+// System.out.println("【定时任务】刷新冷门种子列表,条数:" + coldTorrents.size());
coldTorrents.forEach(System.out::println);
}
}
diff --git a/src/main/java/com/example/g8backend/service/IUserService.java b/src/main/java/com/example/g8backend/service/IUserService.java
index 1f178f8..d980543 100644
--- a/src/main/java/com/example/g8backend/service/IUserService.java
+++ b/src/main/java/com/example/g8backend/service/IUserService.java
@@ -17,6 +17,9 @@
boolean unfollowUser(Long followerId, Long followedId);
List<User> getFollowings(Long userId);
List<User> getFollowers(Long userId);
+ int getFollowingsCount(Long userId);
+ int getFollowersCount(Long userId);
+ boolean isFollowing(Long currentUserId, Long targetUserId);
// 私信功能
Long sendMessage(Long senderId, Long receiverId, String content);
diff --git a/src/main/java/com/example/g8backend/service/impl/TorrentRecommendationServiceImpl.java b/src/main/java/com/example/g8backend/service/impl/TorrentRecommendationServiceImpl.java
index 7adc4c0..263b895 100644
--- a/src/main/java/com/example/g8backend/service/impl/TorrentRecommendationServiceImpl.java
+++ b/src/main/java/com/example/g8backend/service/impl/TorrentRecommendationServiceImpl.java
@@ -103,7 +103,7 @@
// 直接调用mapper更新对应torrent的israre字段
int rows = torrentMapper.updateIsRareByInfoHash(dto.getInfoHash(), israre);
- System.out.println("更新infoHash=" + dto.getInfoHash() + " 的is_rare=" + israre + ",受影响行数=" + rows);
+// System.out.println("更新infoHash=" + dto.getInfoHash() + " 的is_rare=" + israre + ",受影响行数=" + rows);
}
}
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 1d1b6dc..a5f2f5b 100644
--- a/src/main/java/com/example/g8backend/service/impl/UserServiceImpl.java
+++ b/src/main/java/com/example/g8backend/service/impl/UserServiceImpl.java
@@ -38,6 +38,29 @@
private MessageMapper messageMapper;
@Override
+ public int getFollowingsCount(Long userId) {
+ // 实现获取关注数的逻辑
+ return userMapper.countFollowings(userId);
+ }
+
+ @Override
+ public int getFollowersCount(Long userId) {
+ // 实现获取粉丝数的逻辑
+ return userMapper.countFollowers(userId);
+ }
+
+ @Override
+ public boolean isFollowing(Long currentUserId, Long targetUserId) {
+ // 避免自己关注自己的情况
+ if (currentUserId.equals(targetUserId)) {
+ return false;
+ }
+
+ // 查询关注关系是否存在
+ return userMapper.existsFollowRelationship(currentUserId, targetUserId) > 0;
+ }
+
+ @Override
public boolean followUser(Long followerId, Long followedId) {
if (followerId.equals(followedId)) return false;
Follow follow = new Follow();