用户资料接口添加
Change-Id: Id55e7fef307ec1663bb7a05cfbf7f81e097ac767
diff --git a/src/main/java/api/ApiController.java b/src/main/java/api/ApiController.java
index d8528db..41d9052 100644
--- a/src/main/java/api/ApiController.java
+++ b/src/main/java/api/ApiController.java
@@ -14,14 +14,19 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.CrossOrigin;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.PostMapping;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import database.Database1;
-import entity.Seed;
import tracker.Tracker;
+import entity.Seed;
+import entity.User;
+
+import java.util.UUID;
@RestController
public class ApiController implements ApiInterface {
@@ -29,6 +34,8 @@
private static Database1 db1;
private static Tracker tracker;
private static ObjectMapper mapper;
+ private static HttpHeaders headers;
+ private static HttpHeaders errorHeaders;
@PostConstruct
public void init() {
@@ -36,6 +43,14 @@
tracker = new Tracker();
mapper = new ObjectMapper();
mapper.configure(com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+ headers = new HttpHeaders();
+ headers.add("Access-Control-Allow-Origin", "*");
+ headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
+ headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
+ errorHeaders = new HttpHeaders();
+ errorHeaders.add("Access-Control-Allow-Origin", "*");
+ errorHeaders.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
+ errorHeaders.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
}
@Override
@@ -47,42 +62,24 @@
) {
try {
Seed seed = new Seed();
- seed.seedid = "exampleSeedId"; // 示例种子ID
- seed.seeduserid = userid; // 示例用户ID
- seed.title = title; // 示例标题
- seed.seedsize = "1GB"; // 示例种子大小
- seed.seedtag = tag; // 示例标签
+ seed.seedid = UUID.randomUUID().toString(); // 生成唯一的种子ID
+ seed.seeduserid = userid;
+ seed.title = title;
+ seed.seedsize = "1GB";
+ seed.seedtag = tag;
seed.url = "http://example.com/torrent"; // 示例URL
-
+ System.out.println("seed is null? " + (seed == null));
int ret = db1.RegisterSeed(seed);
- // System.out.println("RegisterSeed ret: " + ret);
- // System.out.println(seed.seedid + " " + seed.seeduserid + " " + seed.title + " " + seed.seedsize + " " + seed.seedtag + " " + seed.url);
-
- // Convert MultipartFile to File
+ if (ret != 0) {
+ // 如果注册种子失败,返回错误状态
+ return new ResponseEntity<>(ret, headers, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
File tempFile = File.createTempFile(seed.seedid, file.getOriginalFilename());
file.transferTo(tempFile);
-
tracker.SaveTorrent(seed.seedid, tempFile);
-
- // Optionally, delete the temp file after saving if not needed
- // tempFile.delete();
-
- // 创建带有 CORS 响应头的 ResponseEntity
- HttpHeaders headers = new HttpHeaders();
- headers.add("Access-Control-Allow-Origin", "*"); // 允许所有域名访问,可以根据需要调整为特定域名
- headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // 允许的 HTTP 方法
- headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization"); // 允许的请求头
-
return new ResponseEntity<>(0, headers, HttpStatus.OK); // 返回 0 表示成功
} catch (Exception e) {
e.printStackTrace();
-
- // 创建带有 CORS 响应头的错误 ResponseEntity
- HttpHeaders errorHeaders = new HttpHeaders();
- errorHeaders.add("Access-Control-Allow-Origin", "*");
- errorHeaders.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
- errorHeaders.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
-
return new ResponseEntity<>(1, errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示失败
}
}
@@ -90,8 +87,8 @@
@Override
@CrossOrigin(origins = "*", allowedHeaders = "*") // 允许所有来源和头部
public ResponseEntity<Resource> getTorrent(
- @RequestParam("torrentId") String seedid,
- @RequestParam("userId") String userid
+ @RequestParam("torrentId") String seedid,
+ @RequestParam("userId") String userid
) {
File file = tracker.GetTTorent(seedid, userid);
if (file != null) {
@@ -106,31 +103,187 @@
}
}
- //无按标签查询的函数
@Override
public ResponseEntity<String> getSeedListByTag(
@RequestParam("tag") String tag
) {
try {
Seed[] seeds = db1.GetSeedListByTag(tag);
+ if (seeds == null || seeds.length == 0) {
+ return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子
+ }
String json = mapper.writeValueAsString(seeds);
- // System.out.println("getSeedListByTag: " + json);
- HttpHeaders headers = new HttpHeaders();
- headers.add("Access-Control-Allow-Origin", "*");
- headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
- headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
return new ResponseEntity<>(json, headers, HttpStatus.OK);
} catch (JsonProcessingException e) {
e.printStackTrace();
- HttpHeaders errorHeaders = new HttpHeaders();
- errorHeaders.add("Access-Control-Allow-Origin", "*");
- errorHeaders.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
- errorHeaders.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Override
+ public ResponseEntity<String> getUserProfile(
+ @RequestParam("userid") String userid
+ ) {
+ try {
+ User user = db1.GetInformation(userid);
+ if (user == null) {
+ return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ String json = mapper.writeValueAsString(user);
+ return new ResponseEntity<>(json, headers, HttpStatus.OK);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ @Override
+ @CrossOrigin(origins = "*", allowedHeaders = "*")
+ public ResponseEntity<Integer> changeProfile(
+ @RequestBody String requestBody
+ ) {
+ try {
+ // 解析 JSON 数据
+ com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
+
+ // 安全地获取 userid 字段
+ com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
+ if (useridNode == null) {
+ return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
+ }
+ String userid = useridNode.asText();
+
+ // 添加参数验证
+ if (userid == null || userid.trim().isEmpty()) {
+ return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
+ }
+
+ // 手动映射前端字段到 User 对象,处理类型转换
+ User user = new User();
+ user.userid = userid;
+
+ // 安全地获取其他字段并进行类型转换
+ if (jsonNode.has("username") && !jsonNode.get("username").isNull()) {
+ user.username = jsonNode.get("username").asText();
+ }
+ if (jsonNode.has("school") && !jsonNode.get("school").isNull()) {
+ user.school = jsonNode.get("school").asText();
+ }
+ if (jsonNode.has("gender") && !jsonNode.get("gender").isNull()) {
+ user.sex = jsonNode.get("gender").asText();
+ }
+ if (jsonNode.has("avatar_url") && !jsonNode.get("avatar_url").isNull()) {
+ user.pictureurl = jsonNode.get("avatar_url").asText();
+ }
+
+ // 处理 account_status 的类型转换(字符串/数字 -> 布尔值)
+ if (jsonNode.has("account_status") && !jsonNode.get("account_status").isNull()) {
+ com.fasterxml.jackson.databind.JsonNode statusNode = jsonNode.get("account_status");
+ if (statusNode.isTextual()) {
+ String statusStr = statusNode.asText();
+ user.accountstate = "1".equals(statusStr) || "封禁".equals(statusStr);
+ } else if (statusNode.isNumber()) {
+ user.accountstate = statusNode.asInt() == 1;
+ } else if (statusNode.isBoolean()) {
+ user.accountstate = statusNode.asBoolean();
+ }
+ }
+
+ // 处理 invite_left 的类型转换(字符串 -> 整数)
+ if (jsonNode.has("invite_left") && !jsonNode.get("invite_left").isNull()) {
+ com.fasterxml.jackson.databind.JsonNode inviteNode = jsonNode.get("invite_left");
+ if (inviteNode.isTextual()) {
+ try {
+ user.invitetimes = Integer.parseInt(inviteNode.asText());
+ } catch (NumberFormatException e) {
+ user.invitetimes = 0; // 默认值
+ }
+ } else if (inviteNode.isNumber()) {
+ user.invitetimes = inviteNode.asInt();
+ }
+ }
+
+ int ret = db1.UpdateInformation(user);
+ if (ret == 0) {
+ return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
+ } else {
+ return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
+ }
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
+ } catch (Exception e) {
+ e.printStackTrace();
+ return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ @Override
+ public ResponseEntity<String> getUserSeeds(
+ @RequestParam("userid") String userid
+ ) {
+ try {
+ Seed[] seeds = db1.GetSeedListByUser(userid);
+ if (seeds == null || seeds.length == 0) {
+ return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子
+ }
+ String json = mapper.writeValueAsString(seeds);
+ return new ResponseEntity<>(json, headers, HttpStatus.OK);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ @Override
+ @CrossOrigin(origins = "*", allowedHeaders = "*")
+ public ResponseEntity<Integer> deleteSeed(
+ @RequestBody String requestBody
+ ) {
+ try {
+ // 解析 JSON 数据
+ com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
+ com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
+ if (seedidNode == null) {
+ return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
+ }
+ String seedid = seedidNode.asText();
+
+ // 添加参数验证
+ if (seedid == null || seedid.trim().isEmpty()) {
+ return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
+ }
+
+ int ret = db1.DeleteSeed(seedid);
+ if (ret == 0) {
+ return new ResponseEntity<>(0, HttpStatus.OK);
+ } else {
+ return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ @Override
+ public ResponseEntity<String> getUserStat(
+ @RequestParam("userid") String userid
+ ) {
+ try {
+ User user = db1.GetInformation(userid);
+ if (user == null) {
+ return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ String json = mapper.writeValueAsString(user);
+ return new ResponseEntity<>(json, headers, HttpStatus.OK);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ @Override
public ResponseEntity<String> getTorrentDetail(
@RequestParam("id") String seedid
) {
diff --git a/src/main/java/api/ApiInterface.java b/src/main/java/api/ApiInterface.java
index 6859aa6..2b4a3a3 100644
--- a/src/main/java/api/ApiInterface.java
+++ b/src/main/java/api/ApiInterface.java
@@ -8,6 +8,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.bind.annotation.RequestBody;
@RestController
@RequestMapping("/api")
@@ -36,4 +37,29 @@
ResponseEntity<String> getTorrentDetail(
@RequestParam("id") String seedid
);
+
+ @GetMapping("user-profile")
+ ResponseEntity<String> getUserProfile(
+ @RequestParam("id") String userid
+ );
+
+ @PostMapping("/change-profile")
+ ResponseEntity<Integer> changeProfile(
+ @RequestBody String requestBody
+ );
+
+ @GetMapping("/user-seeds")
+ ResponseEntity<String> getUserSeeds(
+ @RequestParam("userid") String userid
+ );
+
+ @PostMapping("/delete-seed")
+ ResponseEntity<Integer> deleteSeed(
+ @RequestBody String requestBody
+ );
+
+ @GetMapping("/user-stat")
+ ResponseEntity<String> getUserStat(
+ @RequestParam("userid") String userid
+ );
}
\ No newline at end of file