Revert "注册登录,用户等级,社交,动态,新手任务"
This reverts commit 1c359108ca33d46271920ee0cd57dddcb7012937.
Reason for revert: <冲突>
Change-Id: Ie586267169acac99130b1fadf4a5f433441c4b8c
diff --git a/src/main/java/com/example/myproject/MyProjectApplication.java b/src/main/java/com/example/myproject/MyProjectApplication.java
index 49cd5f0..eb58082 100644
--- a/src/main/java/com/example/myproject/MyProjectApplication.java
+++ b/src/main/java/com/example/myproject/MyProjectApplication.java
@@ -5,10 +5,14 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+
@SpringBootApplication
@MapperScan("com.example.myproject.mapper") // 扫描 Mapper 接口
+
public class MyProjectApplication {
public static void main(String[] args) {
SpringApplication.run(MyProjectApplication.class, args);
}
}
+
diff --git a/src/main/java/com/example/myproject/common/CommonResultStatus.java b/src/main/java/com/example/myproject/common/CommonResultStatus.java
new file mode 100644
index 0000000..76e63a6
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/CommonResultStatus.java
@@ -0,0 +1,37 @@
+package com.example.myproject.common;
+
+
+public enum CommonResultStatus implements ResultStatus {
+
+ OK(0, "成功"),
+
+ FAIL(500, "失败"),
+
+ PARAM_ERROR(400, "参数非法"),
+
+ RECORD_NOT_EXIST(404, "记录不存在"),
+
+ UNAUTHORIZED(401, "未授权"),
+
+ FORBIDDEN(403, "无权限"),
+
+ SERVER_ERROR(500, "服务器内部错误");
+
+ private final int code;
+ private final String message;
+
+ CommonResultStatus(int code, String message) {
+ this.code = code;
+ this.message = message;
+ }
+
+ @Override
+ public int getCode() {
+ return code;
+ }
+
+ @Override
+ public String getMessage() {
+ return message;
+ }
+}
diff --git a/src/main/java/com/example/myproject/common/Constants.java b/src/main/java/com/example/myproject/common/Constants.java
new file mode 100644
index 0000000..4e3d864
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/Constants.java
@@ -0,0 +1,54 @@
+package com.example.myproject.common;
+
+
+public interface Constants {
+
+ String TOKEN_HEADER_NAME = "Authorization";
+ String SESSION_CURRENT_USER = "currentUser";
+
+ /**
+ * 菜单根id
+ */
+ Integer RESOURCE_ROOT_ID = 0;
+
+ interface Order {
+ String DEFAULT_ORDER_TYPE = "desc";
+
+ String[] ORDER_TYPE = new String[]{"desc", "asc", "DESC", "ASC"};
+ }
+
+ interface FinishStatus {
+
+ /**
+ * 已完成并测试
+ */
+ String FINISHED = " (已完成并测试通过)";
+
+ /**
+ * 未完成未测试
+ */
+ String UNFINISHED = " (未完成未测试)";
+
+ /**
+ * 已完成但未测试
+ */
+ String FINISHED_NOT_TEST = " (已完成但未测试)";
+
+ }
+
+ interface Source {
+ String PREFIX = "[RKT] ";
+
+ String NAME = "rocket pt";
+ }
+
+ interface Announce {
+
+ String PROTOCOL = "http";
+
+ String HOSTNAME = "192.168.6.112";
+
+ Integer PORT = 9966;
+
+ }
+}
diff --git a/src/main/java/com/example/myproject/common/ResultStatus.java b/src/main/java/com/example/myproject/common/ResultStatus.java
new file mode 100644
index 0000000..f6c7afe
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/ResultStatus.java
@@ -0,0 +1,14 @@
+package com.example.myproject.common;
+
+
+public interface ResultStatus {
+ /**
+ * 错误码
+ */
+ int getCode();
+
+ /**
+ * 错误信息
+ */
+ String getMessage();
+}
diff --git a/src/main/java/com/example/myproject/common/base/I18nMessage.java b/src/main/java/com/example/myproject/common/base/I18nMessage.java
new file mode 100644
index 0000000..a725e9b
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/I18nMessage.java
@@ -0,0 +1,61 @@
+package com.example.myproject.common.base;
+
+import org.springframework.context.MessageSource;
+import org.springframework.context.i18n.LocaleContextHolder;
+import org.springframework.context.support.ReloadableResourceBundleMessageSource;
+
+import java.util.Objects;
+
+import lombok.experimental.UtilityClass;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@UtilityClass
+public class I18nMessage {
+
+ static {
+ ReloadableResourceBundleMessageSource messageSource =
+ new ReloadableResourceBundleMessageSource();
+ messageSource.setCacheSeconds(5);
+ messageSource.setBasenames("classpath:i18n/message");
+ I18nMessage.init(messageSource);
+ }
+
+ private static MessageSource messageSource;
+
+ public static void init(MessageSource messageSource) {
+ Objects.requireNonNull(messageSource, "MessageSource can't be null");
+ I18nMessage.messageSource = messageSource;
+ }
+
+ /**
+ * 读取国际化消息
+ *
+ * @param msgCode 消息码
+ * @param args 消息参数 例: new String[]{"1","2","3"}
+ * @return
+ */
+ public static String getMessage(String msgCode, Object[] args) {
+ try {
+ return I18nMessage.messageSource.getMessage(msgCode, args,
+ LocaleContextHolder.getLocale());
+ } catch (Exception e) {
+ if (log.isDebugEnabled()) {
+ e.printStackTrace();
+ }
+ log.error("===> 读取国际化消息失败, code:{}, args:{}, ex:{}", msgCode, args,
+ e.getMessage() == null ? e.toString() : e.getMessage());
+ }
+ return "-Unknown-";
+ }
+
+ /**
+ * 获取一条语言配置信息
+ *
+ * @param msgCode 消息码
+ * @return 对应配置的信息
+ */
+ public static String getMessage(String msgCode) {
+ return I18nMessage.getMessage(msgCode, null);
+ }
+}
diff --git a/src/main/java/com/example/myproject/common/base/OrderPageParam.java b/src/main/java/com/example/myproject/common/base/OrderPageParam.java
new file mode 100644
index 0000000..21ff38b
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/OrderPageParam.java
@@ -0,0 +1,99 @@
+package com.example.myproject.common.base;
+
+
+import com.example.myproject.common.CommonResultStatus;
+import com.example.myproject.common.Constants;
+import com.example.myproject.common.exception.RocketPTException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.StrUtil;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class OrderPageParam extends PageParam {
+
+ /**
+ * 排序字段
+ */
+ @Schema(description = "排序字段")
+ protected String prop;
+
+ /**
+ * 排序规则
+ */
+ @Schema(description = "排序规则")
+ protected String sort;
+
+ public void validOrder(List<String> orderKey) throws RocketPTException {
+ prop = StringUtils.isBlank(prop) ? null : StrUtil.toUnderlineCase(prop);
+ sort = StringUtils.isBlank(sort) ? Constants.Order.DEFAULT_ORDER_TYPE : sort;
+
+ if (Arrays.asList(Constants.Order.ORDER_TYPE).indexOf(sort) < 0) {
+ throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序方式錯誤");
+ }
+
+ if (StringUtils.isNotBlank(prop) && Arrays.asList(orderKey).indexOf(prop) < 0) {
+ throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序欄位錯誤");
+ }
+ }
+
+ public void validOrder() throws RocketPTException {
+ List<String> orderKey = getOrderKey();
+ prop = StringUtils.isBlank(prop) ? null : StrUtil.toUnderlineCase(prop);
+ sort = StringUtils.isBlank(sort) ? Constants.Order.DEFAULT_ORDER_TYPE : sort;
+
+ if (!ArrayUtil.contains(Constants.Order.ORDER_TYPE, sort)) {
+ throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序方式錯誤");
+ }
+
+ if (StringUtils.isNotBlank(prop) && !orderKey.contains(prop)) {
+ throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序欄位錯誤");
+ }
+ }
+
+ /**
+ * @return 反射获取字段列表
+ */
+ public List<String> getOrderKey() {
+ List<String> list = new ArrayList<>();
+
+ Field[] fields = getClass().getDeclaredFields();
+ if (fields != null) {
+ for (Field field : fields) {
+ field.setAccessible(true);
+ String name = field.getName();
+
+ list.add(StrUtil.toUnderlineCase(name));
+ }
+ }
+ return list;
+ }
+ /**
+ * @return 反射获取字段列表
+ */
+ public List<String> getOrderKey(Class clazz) {
+ List<String> list = new ArrayList<>();
+
+ Field[] fields = clazz.getDeclaredFields();
+ if (fields != null) {
+ for (Field field : fields) {
+ field.setAccessible(true);
+ String name = field.getName();
+
+ list.add(StrUtil.toUnderlineCase(name));
+ }
+ }
+ return list;
+ }
+
+}
diff --git a/src/main/java/com/example/myproject/common/base/PageParam.java b/src/main/java/com/example/myproject/common/base/PageParam.java
new file mode 100644
index 0000000..c59108d
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/PageParam.java
@@ -0,0 +1,25 @@
+package com.example.myproject.common.base;
+
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+
+@Getter
+@Setter
+@ToString
+public class PageParam {
+ @NotNull(message = "page参数不能为空")
+ @Min(value = 1L, message = "page参数必须是数字或数值小于限制")
+ protected Integer page;
+
+ @NotNull(message = "参数不能为空")
+ @Min(value = 1L, message = "参数必须是数字或数值小于限制")
+ @Max(value = 200L, message = "参数必须是数字或数值大于限制")
+ protected Integer size;
+
+
+}
diff --git a/src/main/java/com/example/myproject/common/base/PageUtil.java b/src/main/java/com/example/myproject/common/base/PageUtil.java
new file mode 100644
index 0000000..a4b51ce
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/PageUtil.java
@@ -0,0 +1,54 @@
+package com.example.myproject.common.base;
+
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+
+import java.util.List;
+
+public class PageUtil {
+
+
+ public static int DEFAULT_PAGE_SIZE = 20;
+
+ /**
+ * 开始分页
+ *
+ * @param param
+ */
+ public static void startPage(OrderPageParam param) {
+ Integer page = param.getPage();
+ if (page == null) {
+ param.setPage(1);
+ param.setSize(DEFAULT_PAGE_SIZE);
+ }
+
+ PageHelper.startPage(param.getPage(), param.getSize());
+
+ }
+
+ /**
+ * 开始分页
+ *
+ * @param param
+ */
+ public static void startPage(PageParam param) {
+ Integer page = param.getPage();
+ if (page == null) {
+ param.setPage(1);
+ param.setSize(DEFAULT_PAGE_SIZE);
+ }
+
+ PageHelper.startPage(param.getPage(), param.getSize());
+ }
+
+ /**
+ * 分页结果
+ *
+ * @param list
+ */
+ public static ResPage getPage(List list) {
+ PageInfo pageInfo = new PageInfo(list);
+ return new ResPage(pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getSize());
+
+ }
+}
diff --git a/src/main/java/com/example/myproject/common/base/ResPage.java b/src/main/java/com/example/myproject/common/base/ResPage.java
new file mode 100644
index 0000000..45a22dd
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/ResPage.java
@@ -0,0 +1,31 @@
+package com.example.myproject.common.base;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.ToString;
+
+/**
+ * 分页的返回值
+ */
+@Getter
+@Setter
+@ToString(callSuper = false)
+@NoArgsConstructor
+@AllArgsConstructor
+public class ResPage {
+
+ private long total;
+
+ private int page;
+
+ private int size;
+
+ public static ResPage getPage(long total, int page, int size) {
+ return new ResPage(total, page, size);
+ }
+
+ public static ResPage defaultPage() {
+ return new ResPage(10, 1, 10);
+ }
+}
diff --git a/src/main/java/com/example/myproject/common/base/Result.java b/src/main/java/com/example/myproject/common/base/Result.java
new file mode 100644
index 0000000..6d20f4f
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/Result.java
@@ -0,0 +1,178 @@
+package com.example.myproject.common.base;
+
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.ToString;
+
+/**
+ * 返回值实体类
+ */
+@Getter
+@Setter
+@ToString
+@NoArgsConstructor
+public class Result<T> {
+
+ private int code;
+
+ private String msg;
+
+ @JsonProperty
+ private T data;
+
+ private ResPage page;
+
+
+ public Result(Status status) {
+ this.code = status.getCode();
+ this.msg = status.getMsg();
+ this.data = null;
+ }
+
+ public Result(Status status, T data) {
+ this.code = status.getCode();
+ this.msg = status.getMsg();
+ this.data = data;
+ }
+
+ public Result(Status status, String msg) {
+ this.code = status.getCode();
+ this.msg = msg;
+ this.data = null;
+ }
+
+ public Result(Status status, int msgCode) {
+ this.code = status.getCode();
+ this.msg = I18nMessage.getMessage(String.valueOf(msgCode));
+ this.data = null;
+ }
+
+ public Result(Status status, String msg, T data) {
+ this.code = status.getCode();
+ this.msg = msg;
+ this.data = data;
+ }
+
+ public Result(Status status, int msgCode, T data) {
+ this.code = status.getCode();
+ this.msg = I18nMessage.getMessage(String.valueOf(msgCode));
+ this.data = data;
+ }
+
+ public Result(Status status, T data, ResPage page) {
+ this.code = status.getCode();
+ this.msg = status.getMsg();
+ this.data = data;
+ this.page = page;
+ }
+
+ public Result(Status status, String msg, T data, ResPage page) {
+ this.code = status.getCode();
+ this.msg = msg;
+ this.data = data;
+ this.page = page;
+ }
+
+ public Result(Status status, int msgCode, T data, ResPage page) {
+ this.code = status.getCode();
+ this.msg = I18nMessage.getMessage(String.valueOf(msgCode));
+ this.data = data;
+ this.page = page;
+ }
+
+ @JsonIgnore
+ public boolean isSuccess() {
+ return this.code == Status.SUCCESS.getCode();
+ }
+
+ @JsonIgnore
+ public boolean nonSuccess() {
+ return this.code != Status.SUCCESS.getCode();
+ }
+
+ public static <T> Result<T> success(String 取消收藏成功) {
+ return new Result<T>(Status.SUCCESS);
+ }
+
+ public static <T> Result<T> ok() {
+ return new Result<T>(Status.SUCCESS);
+ }
+
+ public static <T> Result<T> ok(T data) {
+ return new Result<T>(Status.SUCCESS, data);
+ }
+
+ public static <T> Result<T> illegal() {
+ return new Result<T>(Status.BAD_REQUEST);
+ }
+
+ public static <T> Result<T> unauthorized() {
+ return new Result<T>(Status.UNAUTHORIZED);
+ }
+
+ public static <T> Result<T> forbidden() {
+ return new Result<T>(Status.FORBIDDEN);
+ }
+
+ public static <T> Result<T> notFound() {
+ return new Result<T>(Status.NOT_FOUND);
+ }
+
+ public static <T> Result<T> failure() {
+ return new Result<T>(Status.FAILURE);
+ }
+
+ public static <T> Result<T> failure(String msg) {
+ return new Result<T>(Status.FAILURE, msg);
+ }
+
+ public static <T> Result<T> error(String msg) {
+ return new Result<T>(Status.FAILURE, msg);
+ }
+
+ public static <T> Result<T> conflict() {
+ return new Result<T>(Status.CONFLICT);
+ }
+
+ public static <T> Result<T> build(Status status, T data) {
+ return new Result<T>(status, data);
+ }
+
+ public static <T> Result<T> build(Status status, String msg) {
+ return new Result<T>(status, msg);
+ }
+
+ public static <T> Result<T> build(Status status, int msgCode) {
+ return new Result<T>(status, msgCode);
+ }
+
+ public static <T> Result<T> build(Status status, String msg, T data) {
+ return new Result<T>(status, msg, data);
+ }
+
+ public static <T> Result<T> build(Status status, int msgCode, T data) {
+ return new Result<T>(status, msgCode, data);
+ }
+
+ public static Result ok(Object data, ResPage page) {
+ return new Result(Status.SUCCESS, data, page);
+ }
+
+ public static Result build(Status status, Object data, ResPage page) {
+ return new Result(status, data, page);
+ }
+
+ public static Result build(Status status, String msg, Object data, ResPage page) {
+ return new Result(status, msg, data, page);
+ }
+
+ public static Result build(Status status, int msgCode, Object data, ResPage page) {
+ return new Result(status, msgCode, data, page);
+ }
+
+}
diff --git a/src/main/java/com/example/myproject/common/base/Status.java b/src/main/java/com/example/myproject/common/base/Status.java
new file mode 100644
index 0000000..4949d0c
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/Status.java
@@ -0,0 +1,57 @@
+package com.example.myproject.common.base;
+
+
+public enum Status {
+
+ /**
+ * 请求执行成功
+ */
+ SUCCESS(0, "操作成功"),
+
+ /**
+ * 请求验证失败
+ */
+ BAD_REQUEST(400, "操作验证失败"),
+
+ /**
+ * 权限不足
+ */
+ UNAUTHORIZED(401, "操作未授权"),
+
+ /**
+ * 请求拒绝
+ */
+ FORBIDDEN(403, "操作被拒绝"),
+
+ /**
+ * 未知请求
+ */
+ NOT_FOUND(404, "未知操作"),
+
+ /**
+ * 未知请求
+ */
+ CONFLICT(409, "请求发生冲突"),
+
+ /**
+ * 请求执行失败
+ */
+ FAILURE(500, "操作失败");
+
+ private int code;
+
+ private String msg;
+
+ Status(int code, String msg) {
+ this.code = code;
+ this.msg = msg;
+ }
+
+ public int getCode() {
+ return this.code;
+ }
+
+ public String getMsg() {
+ return this.msg;
+ }
+}
diff --git a/src/main/java/com/example/myproject/common/exception/RocketPTException.java b/src/main/java/com/example/myproject/common/exception/RocketPTException.java
new file mode 100644
index 0000000..7a475f2
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/exception/RocketPTException.java
@@ -0,0 +1,29 @@
+package com.example.myproject.common.exception;
+
+import com.example.myproject.common.CommonResultStatus;
+import com.example.myproject.common.ResultStatus;
+
+public class RocketPTException extends RuntimeException {
+ private final ResultStatus status;
+
+ public RocketPTException(ResultStatus status) {
+ super(status.getMessage());
+ this.status = status;
+ }
+
+ public RocketPTException(ResultStatus status, String message) {
+ super(message);
+ this.status = status;
+ }
+
+ public RocketPTException(String message) {
+ super(message);
+ this.status = CommonResultStatus.FAIL;
+ }
+
+ public ResultStatus getStatus() {
+ return status;
+ }
+
+
+}
diff --git a/src/main/java/com/example/myproject/config/MyMetaObjectHandler.java b/src/main/java/com/example/myproject/config/MyMetaObjectHandler.java
new file mode 100644
index 0000000..1031b93
--- /dev/null
+++ b/src/main/java/com/example/myproject/config/MyMetaObjectHandler.java
@@ -0,0 +1,24 @@
+package com.example.myproject.config;
+
+import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
+import org.apache.ibatis.reflection.MetaObject;
+import org.springframework.stereotype.Component;
+
+import java.time.LocalDateTime;
+
+@Component
+public class MyMetaObjectHandler implements MetaObjectHandler {
+
+
+ @Override
+ public void insertFill(MetaObject metaObject) {
+ this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
+ this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
+ }
+
+ // 更新时自动填充
+ @Override
+ public void updateFill(MetaObject metaObject) {
+ this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
+ }
+}
diff --git a/src/main/java/com/example/myproject/config/TrackerConfig.java b/src/main/java/com/example/myproject/config/TrackerConfig.java
new file mode 100644
index 0000000..67522b7
--- /dev/null
+++ b/src/main/java/com/example/myproject/config/TrackerConfig.java
@@ -0,0 +1,81 @@
+package com.example.myproject.config;
+
+import com.turn.ttorrent.tracker.TrackedTorrent;
+import com.turn.ttorrent.tracker.Tracker;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import javax.annotation.PreDestroy;
+import java.io.File;
+import java.io.IOException;
+import java.net.InetAddress;
+
+@Configuration
+public class TrackerConfig {
+ @Value("${pt.tracker.port}")
+ private int trackerPort;
+
+ @Value("${pt.tracker.torrent-dir}")
+ private String torrentDir;
+ @Value("${pt.tracker.announce-url}")
+ private String announceURL;
+
+ private Tracker tracker; // 存储 Tracker 实例
+
+ @Bean
+ public Tracker torrentTracker() throws IOException {
+ // 验证并创建目录
+ File dir = new File(torrentDir);
+ validateTorrentDirectory(dir);
+
+ // 初始化 Tracker
+ tracker = new Tracker(trackerPort, announceURL);
+ tracker.setAcceptForeignTorrents(false); // PT 站点必须关闭匿名模式
+ tracker.setAnnounceInterval(1800); // 30分钟 announce 间隔
+
+ // 加载种子文件
+ loadTorrents(tracker, dir);
+
+ // 启动 Tracker
+ tracker.start(true);
+ System.out.println("Tracker started on port " + trackerPort);
+ return tracker;
+ }
+
+ private void validateTorrentDirectory(File dir) throws IOException {
+ if (!dir.exists()) {
+ if (!dir.mkdirs()) {
+ throw new IOException("无法创建目录: " + dir.getAbsolutePath());
+ }
+ }
+ if (!dir.isDirectory()) {
+ throw new IllegalArgumentException("路径不是目录: " + dir.getAbsolutePath());
+ }
+ if (!dir.canRead() || !dir.canWrite()) {
+ throw new IOException("应用程序无权限访问目录: " + dir.getAbsolutePath());
+ }
+ }
+
+ private void loadTorrents(Tracker tracker, File dir) throws IOException {
+ if (!dir.exists() || !dir.isDirectory()) {
+ throw new IOException("无效的种子目录: " + dir.getAbsolutePath());
+ }
+
+ File[] torrentFiles = dir.listFiles((d, name) -> name.endsWith(".torrent"));
+ if (torrentFiles == null) {
+ throw new IOException("无法读取目录内容: " + dir.getAbsolutePath());
+ }
+
+ for (File f : torrentFiles) {
+ tracker.announce(TrackedTorrent.load(f));
+ }
+ }
+
+ @PreDestroy
+ public void stopTracker() {
+ if (tracker != null) {
+ tracker.stop();
+ System.out.println("Tracker stopped.");
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/controller/CommentController.java b/src/main/java/com/example/myproject/controller/CommentController.java
deleted file mode 100644
index f8eb135..0000000
--- a/src/main/java/com/example/myproject/controller/CommentController.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.entity.Comments;
-import com.example.myproject.service.CommentService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
-
-import java.util.List;
-import java.util.Map;
-
-@RestController
-@RequestMapping("/echo/forum/posts")
-public class CommentController {
-
- @Autowired
- private CommentService commentService;
-
- // 添加评论
- @PostMapping("/{post_id}/comments")
- public String addComment(@PathVariable("post_id") Long postId, @RequestBody Comments comment) {
- // 添加评论
- commentService.addComment(postId, comment);
- return "Comment added successfully!";
- }
-
- //获取所有评论
- @GetMapping("/{post_id}/getAllComments")
- public List<Map<String, Object>> getCommentsByPostId(@PathVariable("post_id") Long postId) {
- // 获取评论并填充用户信息
- return commentService.getCommentsByPostId(postId);
- }
-
-}
diff --git a/src/main/java/com/example/myproject/controller/DynamicController.java b/src/main/java/com/example/myproject/controller/DynamicController.java
deleted file mode 100644
index db1d0a2..0000000
--- a/src/main/java/com/example/myproject/controller/DynamicController.java
+++ /dev/null
@@ -1,107 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.entity.UserDynamic;
-import com.example.myproject.service.DynamicService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.util.List;
-import java.util.Map;
-
-@RestController
-@RequestMapping("/echo/dynamic")
-public class DynamicController {
-
- @Autowired
- private DynamicService dynamicService;
-
- // 创建好友动态接口
- @PostMapping("/{user_id}/createDynamic")
- public Map<String, Object> createDynamic(@PathVariable("user_id") Long userId,
- @RequestParam(value = "title", required = false) String title,
- @RequestParam(value = "content") String content,
- @RequestParam(value = "image_url", required = false) MultipartFile[] imageFiles) {
- return dynamicService.createDynamic(userId, title, content, imageFiles);
- }
-
- //删除好友动态
- @DeleteMapping("/me/deleteDynamic/{dynamic_id}")
- public ResponseEntity<String> deleteDynamic(@PathVariable("dynamic_id") Long dynamicId) {
- dynamicService.deleteDynamic(dynamicId);
- return ResponseEntity.ok("动态删除成功");
- }
-
- //好友动态评论
- @PostMapping("/{user_id}/feeds/{dynamic_id}/comments")
- public ResponseEntity<Map<String, Object>> addComment(
- @PathVariable("user_id") Long userId,
- @PathVariable("dynamic_id") Long dynamicId,
- @RequestBody Map<String, String> content) {
-
- String commentContent = content.get("content"); // 获取评论内容
- Map<String, Object> response = dynamicService.addComment(userId, dynamicId, commentContent);
-
- return ResponseEntity.ok(response);
- }
-
- //获取某个好友的所有动态
- @GetMapping("/{user_id}/getAdynamic")
- public ResponseEntity<Map<String, Object>> getAllUserDynamics(@PathVariable("user_id") Long userId) {
- Map<String, Object> response = dynamicService.getAllUserDynamics(userId);
-
- if (response == null || response.isEmpty()) {
- return ResponseEntity.noContent().build();
- }
-
- // 返回响应
- return ResponseEntity.ok(response);
- }
-
- @GetMapping("/{user_id}/getAllDynamics")
- public ResponseEntity<Map<String, Object>> getAllUserAndFriendsDynamics(@PathVariable("user_id") Long userId) {
- // 获取当前用户所有好友的ID
- List<Long> friendIds = dynamicService.getAllFriendIds(userId);
- friendIds.add(userId);
- Map<String, Object> response = dynamicService.getAllUserAndFriendsDynamics(friendIds);
-
- if (response == null || response.isEmpty()) {
- return ResponseEntity.noContent().build();
- }
-
- return ResponseEntity.ok(response);
- }
-
- //点赞好友动态
- @PostMapping("/like")
- public ResponseEntity<String> likeDynamic(@RequestBody Map<String, Long> request) {
- Long userId = request.get("userId");
- Long dynamicId = request.get("dynamicId");
- // 执行点赞
- boolean success = dynamicService.likeDynamic(userId, dynamicId);
-
- if (success) {
- return ResponseEntity.ok("Like successful");
- } else {
- return ResponseEntity.badRequest().body("Like failed");
- }
- }
-
- @DeleteMapping("/unlike")
- public ResponseEntity<String> unlikeDynamic(@RequestBody Map<String, Long> request) {
- Long userId = request.get("userId");
- Long dynamicId = request.get("dynamicId");
-
- // 执行取消点赞
- boolean success = dynamicService.unlikeDynamic(userId, dynamicId);
-
- if (success) {
- return ResponseEntity.ok("Unlike successful");
- } else {
- return ResponseEntity.badRequest().body("Unlike failed");
- }
- }
-
-
-}
diff --git a/src/main/java/com/example/myproject/controller/GroupController.java b/src/main/java/com/example/myproject/controller/GroupController.java
deleted file mode 100644
index a0830b6..0000000
--- a/src/main/java/com/example/myproject/controller/GroupController.java
+++ /dev/null
@@ -1,105 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.entity.Group;
-import com.example.myproject.entity.GroupComments;
-import com.example.myproject.service.GroupService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.util.HashMap;
-import java.util.Map;
-
-@RestController
-@RequestMapping("/echo/groups")
-public class GroupController {
-
- @Autowired
- private GroupService groupService;
-
- @PostMapping("/createGroup")
- public ResponseEntity<Map<String, Object>> createGroup(@RequestBody Group groupRequest) {
- // 调用服务层方法创建小组
- System.out.println("Received group name: " + groupRequest.getGroupName());
- return groupService.createGroup(groupRequest);
- }
-
-
- // 加入小组接口
- @PostMapping("/{group_id}/join")
- public ResponseEntity<Map<String, Object>> joinGroup(@PathVariable("group_id") Long groupId,
- @RequestBody Map<String, Long> requestBody) {
- Long userId = requestBody.get("user_id");
- return groupService.joinGroup(groupId, userId);
- }
-
- // 退出小组接口
- @PostMapping("/{group_id}/leave")
- public ResponseEntity<Map<String, Object>> leaveGroup(@PathVariable("group_id") Long groupId,
- @RequestBody Map<String, Long> requestBody) {
- Long userId = requestBody.get("user_id");
- return groupService.leaveGroup(groupId, userId);
- }
-
- // 获取小组成员接口
- @GetMapping("/{group_id}/members")
- public ResponseEntity<Map<String, Object>> getGroupMembers(@PathVariable("group_id") Long groupId) {
- return groupService.getGroupMembers(groupId);
- }
-
- //发布帖子
- @PostMapping("/{group_id}/createPost")
- public ResponseEntity<Map<String, Object>> createPost(
- @PathVariable("group_id") Long groupId,
- @RequestParam("user_id") Long userId,
- @RequestParam("content") String content,
- @RequestParam("title") String title,
- @RequestParam(value = "images", required = false) MultipartFile[] imageFiles) {
-
- Map<String, Object> response = groupService.createPost(groupId, userId, content, title, imageFiles);
- return ResponseEntity.ok(response);
- }
-
- //获取某个小组内的所有帖子
- @GetMapping("/{group_id}/getAllPosts")
- public ResponseEntity<Map<String, Object>> getAllPosts(@PathVariable("group_id") Long groupId) {
- Map<String, Object> response = groupService.getAllPosts(groupId);
- return ResponseEntity.ok(response);
- }
-
- //获取所有小组名称
- @PostMapping("/getAllGroups")
- public ResponseEntity<Map<String, Object>> searchGroups(@RequestBody Map<String, Object> params) {
- Map<String, Object> response = groupService.searchGroups(params);
- return ResponseEntity.ok(response);
- }
-
- // 点赞帖子
- @PostMapping("/{group_post_id}/like")
- public ResponseEntity<Map<String, Object>> likePost(
- @PathVariable("group_post_id") Long groupPostId) {
- Map<String, Object> response = groupService.likePost(groupPostId);
- return ResponseEntity.ok(response);
- }
-
- // 取消点赞
- @PostMapping("/{group_post_id}/unlike")
- public ResponseEntity<Map<String, Object>> unlikePost(
- @PathVariable("group_post_id") Long groupPostId) {
- Map<String, Object> response = groupService.unlikePost(groupPostId);
- return ResponseEntity.ok(response);
- }
-
- // 添加评论接口
- @PostMapping("/{group_post_id}/comment")
- public ResponseEntity<Map<String, Object>> addComment(
-
- @PathVariable("group_post_id") Long postId,
- @RequestBody GroupComments comment) {
-
- Map<String, Object> response = groupService.addComment(postId, comment);
- return ResponseEntity.ok(response);
- }
-
-}
diff --git a/src/main/java/com/example/myproject/controller/LevelController.java b/src/main/java/com/example/myproject/controller/LevelController.java
deleted file mode 100644
index 8781246..0000000
--- a/src/main/java/com/example/myproject/controller/LevelController.java
+++ /dev/null
@@ -1,42 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.service.LevelService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
-
-import java.util.Map;
-
-@RequestMapping("/echo/level")
-@RestController
-public class LevelController {
-
- @Autowired
- private LevelService levelService;
-
- @GetMapping("/getExperience")
- public Map<String, Object> getExperience(@RequestParam Long user_id) {
- return levelService.getUserExperience(user_id);
- }
-
- @PostMapping("/updateExperience")
- public Map<String, Object> updateExperience(@RequestBody Map<String, Object> params) {
- Long userId = Long.valueOf(params.get("user_id").toString());
- Integer experience = Integer.valueOf(params.get("experience").toString());
- String source = params.get("source").toString();
-
- return levelService.updateExperience(userId, experience, source);
- }
-
- // 检查用户是否满足升级条件
- @GetMapping("/upgrade-check")
- public Map<String, Object> checkUpgrade(@RequestParam Long user_id) {
- return levelService.checkUpgrade(user_id);
- }
-
- @PostMapping("/upgrades")
- public Map<String, Object> upgradeUserLevel(@RequestBody Map<String, Object> request) {
- Long userId = Long.valueOf(request.get("user_id").toString());
- Boolean canUpgrade = (Boolean) request.get("can_upgrade");
- return levelService.upgradeUserLevel(userId, canUpgrade);
- }
-}
diff --git a/src/main/java/com/example/myproject/controller/PostController.java b/src/main/java/com/example/myproject/controller/PostController.java
deleted file mode 100644
index 4233028..0000000
--- a/src/main/java/com/example/myproject/controller/PostController.java
+++ /dev/null
@@ -1,100 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.entity.Post;
-import com.example.myproject.service.PostService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-
-@RestController
-@RequestMapping("/echo/forum/posts")
-public class PostController {
-
- @Autowired
- private PostService postService;
-
- //创建新帖子(已完成)
- @PostMapping("/{user_id}/createPost")
- public Map<String, Object> createPost(
- @PathVariable("user_id") Long userId,
- @RequestParam(value = "postContent") String postContent,
- @RequestParam(value = "title", required = false) String title,
- @RequestParam(value = "imageUrl") MultipartFile[] imageFiles) {
- return postService.createPost(userId, postContent, title, imageFiles);
- }
-
- //编辑帖子(已完成)
- @PutMapping("/{post_id}/editPost")
- public String updatePost(@PathVariable("post_id") Long postId, @RequestBody Post post) {
- postService.updatePost(postId, post);
- return "Post updated successfully!";
- }
-
- //删除帖子(已完成)
- @DeleteMapping("/{post_id}/deletePost")
- public String deletePost(@PathVariable("post_id") Long postId) {
- postService.deletePost(postId);
- return "Post deleted successfully!";
- }
-
- //点赞帖子(已完成)
- @PostMapping("/{post_id}/like")
- public String likePost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
-
- Long userId = requestBody.get("user_id");
- postService.likePost(postId, userId);
-
- return "Post liked successfully!";
- }
-
- //取消点赞(已完成)
- @PostMapping("/{post_id}/unlike")
- public String unlikePost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
- Long userId = requestBody.get("user_id");
- postService.unlikePost(postId, userId);
-
- return "Post unliked successfully!";
- }
-
- //获取帖子列表(已完成)
- @GetMapping("/getAllPosts")
- public Map<String, Object> getAllPosts() {
- return postService.getAllPosts(); // 调用服务层的 getAllPosts 方法
- }
-
- @GetMapping("/{postId}/getPost")
- public Map<String, Object> getPostById(@PathVariable Long postId) {
- return postService.getPostById(postId);
- }
-
-
- //收藏帖子(已完成)
- @PostMapping("/{post_id}/collect")
- public String collectPost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
- Long userId = requestBody.get("user_id");
- postService.collectPost(postId, userId);
-
- return "Post collected successfully!";
- }
-
-
- //取消收藏(已完成)
- @DeleteMapping("/{post_id}/uncollect")
- public String uncollectPost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
- Long userId = requestBody.get("user_id");
- postService.uncollectPost(postId, userId);
- return "Post uncollected successfully!";
- }
-
- // 获取用户收藏的所有帖子
- @GetMapping("/{userId}/getAllcollections")
- public List<Map<String, Object>> getAllCollections(@PathVariable("userId") Long userId) {
- return postService.getAllCollections(userId);
- }
-
-
-}
diff --git a/src/main/java/com/example/myproject/controller/SeedCommentController.java b/src/main/java/com/example/myproject/controller/SeedCommentController.java
deleted file mode 100644
index 344b56c..0000000
--- a/src/main/java/com/example/myproject/controller/SeedCommentController.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.entity.SeedComment;
-import com.example.myproject.service.SeedCommentService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
-
-import java.util.Map;
-
-@RestController
-@RequestMapping("/echo/seeds")
-public class SeedCommentController {
-
- @Autowired
- private SeedCommentService seedCommentService;
-
- @PostMapping("/{seed_id}/comments")
- public String publishComment(@PathVariable("seed_id") int seedId,
- @RequestBody SeedComment seedComment) {
- // 调用服务类的逻辑处理评论发布
- return seedCommentService.publishComment(seedId, seedComment);
- }
-
- @GetMapping("/{seed_id}/getAllComments")
- public Map<String, Object> getAllComments(@PathVariable("seed_id") int seedId) {
- // 调用服务层获取所有评论数据
- return seedCommentService.getAllCommentsForSeed(seedId);
- }
-
- // 点赞切换接口
- @PutMapping("/{comment_id}/like-toggle")
- public Map<String, Object> toggleLike(@PathVariable("comment_id") Long commentId,
- @RequestBody Map<String, Object> request) {
- Long userId = Long.parseLong(request.get("user_id").toString());
- return seedCommentService.toggleLike(commentId, userId);
- }
-
-
-}
diff --git a/src/main/java/com/example/myproject/controller/TaskController.java b/src/main/java/com/example/myproject/controller/TaskController.java
deleted file mode 100644
index ab4b7ea..0000000
--- a/src/main/java/com/example/myproject/controller/TaskController.java
+++ /dev/null
@@ -1,67 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.service.TaskService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
-
-import java.util.Map;
-
-@RestController
-@RequestMapping("/echo/task/tutorial")
-public class TaskController {
-
- @Autowired
- private TaskService taskService;
-
- @GetMapping("/getAllTasks")
- public Map<String, Object> getAllTasks(@RequestParam("user_id") Long userId) {
- return taskService.getAllTasksForUser(userId);
- }
-
-
- @PostMapping("/updateStatus")
- public Map<String, Object> updateTaskStatus(@RequestBody Map<String, Object> request) {
- Long userId = Long.parseLong(request.get("user_id").toString());
- String taskId = request.get("task_id").toString();
- return taskService.updateTaskStatus(userId, taskId);
- }
-
- //获取当前经验和任务奖励
- @GetMapping("/getExperience")
- public Map<String, Object> getExperience(@RequestBody Map<String, Object> request) {
- Long userId = Long.parseLong(request.get("user_id").toString());
- return taskService.getUserExperience(userId);
- }
-
- //获取当前的指引步骤
- @GetMapping("/getNewStep")
- public Map<String, Object> getNewStep(@RequestParam("user_id") Long userId) {
- return taskService.getNewStep(userId);
- }
-
-
- //更新进度
- @PostMapping("/updateProgress")
- public Map<String, Object> updateProgress(@RequestBody Map<String, Object> request) {
- Long userId = Long.parseLong(request.get("user_id").toString());
- String taskId = request.get("task_id").toString();
- Integer progress = Integer.parseInt(request.get("progress").toString());
- return taskService.updateTaskProgress(userId, taskId, progress);
- }
-
- //领取任务奖励
- @PostMapping("/rewardClaim")
- public Map<String, Object> rewardClaim(@RequestBody Map<String, Object> request) {
- Long userId = Long.parseLong(request.get("user_id").toString());
- String taskId = request.get("task_id").toString();
- return taskService.claimReward(userId, taskId);
- }
-
- //检查任务奖励状态
- @PostMapping("/rewardReview")
- public Map<String, Object> rewardReview(@RequestBody Map<String, Object> request) {
- Long userId = Long.parseLong(request.get("user_id").toString());
- String taskId = request.get("task_id").toString();
- return taskService.checkRewardStatus(userId, taskId);
- }
-}
diff --git a/src/main/java/com/example/myproject/controller/TorrentController.java b/src/main/java/com/example/myproject/controller/TorrentController.java
new file mode 100644
index 0000000..cea2ccf
--- /dev/null
+++ b/src/main/java/com/example/myproject/controller/TorrentController.java
@@ -0,0 +1,293 @@
+package com.example.myproject.controller;
+
+import com.example.myproject.common.base.PageUtil;
+import com.example.myproject.entity.TorrentEntity;
+import com.example.myproject.service.TorrentService;
+import com.example.myproject.service.PromotionService;
+import com.example.myproject.dto.param.TorrentParam;
+import com.example.myproject.dto.vo.TorrentVO;
+import com.example.myproject.common.base.Result;
+import com.example.myproject.dto.param.TorrentUploadParam;
+import com.example.myproject.dto.TorrentUpdateDTO;
+import com.example.myproject.dto.PromotionCreateDTO;
+import com.example.myproject.entity.Promotion;
+import com.example.myproject.service.UserService;
+
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.media.Content;
+import io.swagger.v3.oas.annotations.media.Schema;
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.List;
+
+import cn.dev33.satoken.annotation.SaCheckLogin;
+import cn.dev33.satoken.stp.StpUtil;
+
+@RestController
+@RequestMapping("/seeds")
+public class TorrentController {
+
+ @Autowired
+ private TorrentService torrentService;
+
+ @Autowired
+ private PromotionService promotionService;
+
+ @Autowired
+ private UserService userService;
+
+
+ @SaCheckLogin
+ @Operation(summary = "种子列表查询", description = "种子列表条件查询-分页-排序")
+ @ApiResponse(responseCode = "0", description = "操作成功",
+ content = {@Content(mediaType = "application/json",
+ schema = @Schema(implementation = TorrentVO.class))
+ })
+ @PostMapping("/list")
+ public Result list(@RequestBody TorrentParam param) {
+ // 构建排序和模糊查询条件
+ param.validOrder(param.getOrderKey(TorrentEntity.class));
+ param.buildLike();
+
+ PageUtil.startPage(param);
+
+ // 查询数据
+ List<TorrentEntity> list = torrentService.search(param);
+
+ // 返回分页结果
+ return Result.ok(list, PageUtil.getPage(list));
+ }
+
+ @SaCheckLogin
+ @Operation(summary = "种子详情查询")
+ @ApiResponse(responseCode = "0", description = "操作成功", content = {
+ @Content(mediaType = "application/json", schema = @Schema(implementation =
+ TorrentEntity.class))
+ })
+ @PostMapping("/info/{id}")
+ public Result info(@PathVariable("id")Long id) {
+
+ TorrentEntity entity = torrentService.selectBySeedId(id);
+ return Result.ok(entity);
+ }
+
+@Operation(summary = "上传种子")
+
+ @PostMapping("/upload")
+ public Result uploadTorrent(
+ @RequestParam("file") MultipartFile file,
+ @ModelAttribute @Validated TorrentUploadParam param) throws IOException {
+ try {
+ // 验证用户权限
+ // Long userId = StpUtil.getLoginIdAsLong();
+ String userId = String.valueOf(param.getUploader());
+ param.setUploader(userId);
+
+ // 验证文件大小和类型
+ if (file.isEmpty() || file.getSize() > 10 * 1024 * 1024) { // 10MB限制
+ return Result.error("文件大小不符合要求");
+ }
+
+ if (!file.getOriginalFilename().toLowerCase().endsWith(".torrent")) {
+ return Result.error("只支持.torrent文件");
+ }
+
+ torrentService.uploadTorrent(file, param);
+ return Result.ok();
+ } catch (Exception e) {
+ return Result.error("种子上传失败: " + e.getMessage());
+ }
+ }
+
+ /**
+ * 获取种子文件
+ */
+ @GetMapping("/{seed_id}/download")
+ public void downloadTorrent(
+ @PathVariable("seed_id") Long seedId,
+ @RequestParam("passkey") String passkey,
+ HttpServletResponse response) throws IOException {
+
+ // 获取种子实体
+ TorrentEntity entity = torrentService.selectBySeedId(seedId);
+ if (entity == null) {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND, "种子不存在");
+ return;
+ }
+
+ // 获取并处理种子文件内容(需在service中实现passkey注入)
+ byte[] torrentBytes = torrentService.fetch(seedId, passkey);
+
+ // 设置下载文件名
+ String filename = entity.getFileName();
+ if (filename == null || filename.isBlank()) {
+ filename = seedId + ".torrent";
+ }
+ if (!filename.toLowerCase().endsWith(".torrent")) {
+ filename = filename + ".torrent";
+ }
+ filename = java.net.URLEncoder.encode(filename, java.nio.charset.StandardCharsets.UTF_8).replaceAll("\\+",
+ "%20");
+
+ // 设置响应头
+ response.setCharacterEncoding(java.nio.charset.StandardCharsets.UTF_8.name());
+ response.setContentLength(torrentBytes.length);
+ response.setContentType("application/x-bittorrent");
+ response.setHeader("Content-Disposition", "attachment;filename=" + filename);
+
+ // 写入文件内容
+ response.getOutputStream().write(torrentBytes);
+ response.getOutputStream().flush();
+ }
+
+ /**
+ * 收藏或者取消收藏
+ */
+ @PostMapping("/{seed_id}/favorite-toggle")
+ public Result favorite(
+ @PathVariable("seed_id") Long seedId,
+ @RequestParam("user_id") Long userId) {
+ try {
+
+ return torrentService.favorite(seedId, userId);
+ } catch (Exception e) {
+ return Result.error("失败: ");
+ }
+ }
+
+ @SaCheckLogin
+ @Operation(summary = "删除种子")
+ @DeleteMapping("/{torrentId}")
+ public Result deleteTorrent(@PathVariable Long torrentId) {
+ try {
+ // 验证用户权限
+ Long userId = StpUtil.getLoginIdAsLong();
+ if (!torrentService.canUserDeleteTorrent(torrentId, userId)) {
+ return Result.error("没有权限删除此种子");
+ }
+
+ torrentService.deleteTorrent(torrentId);
+ return Result.ok();
+ } catch (Exception e) {
+ return Result.error("删除失败: " + e.getMessage());
+ }
+ }
+
+ @SaCheckLogin
+ @Operation(summary = "修改种子信息")
+ @PutMapping("/{torrentId}")
+ public Result updateTorrent(
+ @PathVariable Long torrentId,
+ @RequestBody @Validated TorrentUpdateDTO updateDTO) {
+ try {
+ // 验证用户权限
+ Long userId = StpUtil.getLoginIdAsLong();
+ if (!torrentService.canUserUpdateTorrent(torrentId, userId)) {
+ return Result.error("没有权限修改此种子");
+ }
+
+ torrentService.updateTorrent(torrentId, updateDTO);
+ return Result.ok();
+ } catch (Exception e) {
+ return Result.error("更新失败: " + e.getMessage());
+ }
+ }
+
+ @SaCheckLogin
+ @Operation(summary = "创建促销活动")
+ @PostMapping("/promotions")
+ public Result createPromotion(@RequestBody @Validated PromotionCreateDTO promotionDTO) {
+ try {
+ // 验证用户权限(只有管理员可以创建促销)
+// if (!StpUtil.hasRole("admin")) {
+// return Result.error("没有权限创建促销活动");
+// }
+//
+ Promotion promotion = promotionService.createPromotion(promotionDTO);
+ return Result.ok(promotion);
+ } catch (Exception e) {
+ return Result.error("创建促销失败: " + e.getMessage());
+ }
+ }
+
+ @SaCheckLogin
+ @Operation(summary = "获取促销活动列表")
+ @GetMapping("/promotions")
+ public Result getPromotions() {
+ try {
+ List<Promotion> promotions = promotionService.getAllActivePromotions();
+ return Result.ok(promotions);
+ } catch (Exception e) {
+ return Result.error("获取促销列表失败: " + e.getMessage());
+ }
+ }
+
+ @SaCheckLogin
+ @Operation(summary = "获取促销详情")
+ @GetMapping("/promotions/{promotionId}")
+ public Result getPromotionDetails(@PathVariable Long promotionId) {
+ try {
+ Promotion promotion = promotionService.getPromotionById(promotionId);
+ if (promotion == null) {
+ return Result.error("促销活动不存在");
+ }
+ return Result.ok(promotion);
+ } catch (Exception e) {
+ return Result.error("获取促销详情失败: " + e.getMessage());
+ }
+ }
+
+ @SaCheckLogin
+ @Operation(summary = "删除促销活动")
+ @DeleteMapping("/promotions/{promotionId}")
+ public Result deletePromotion(@PathVariable Long promotionId) {
+ try {
+ // 验证用户权限(只有管理员可以删除促销)
+ if (!StpUtil.hasRole("admin")) {
+ return Result.error("没有权限删除促销活动");
+ }
+
+ promotionService.deletePromotion(promotionId);
+ return Result.ok();
+ } catch (Exception e) {
+ return Result.error("删除促销失败: " + e.getMessage());
+ }
+ }
+
+ // 下载种子(包含反作弊机制)
+ @PostMapping("/{torrentId}/download")
+ public ResponseEntity<?> downloadTorrent(@PathVariable Long torrentId,
+ @RequestParam Long userId) {
+// // 验证用户身份和权限
+// if (!userService.validateUser(userId)) {
+// return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
+// }
+
+ // 检查用户上传量是否足够
+ if (!torrentService.checkUserUploadRatio(userId)) {
+ return ResponseEntity.status(HttpStatus.FORBIDDEN)
+ .body("上传量不足,无法下载");
+ }
+
+ // 应用促销折扣(如果有)
+ double downloadSize = torrentService.calculateDownloadSize(torrentId, userId);
+
+ // 记录下载
+ torrentService.recordDownload(torrentId, userId, downloadSize);
+
+ return ResponseEntity.ok().build();
+ }
+
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/controller/UserController.java b/src/main/java/com/example/myproject/controller/UserController.java
index e96df8f..acda403 100644
--- a/src/main/java/com/example/myproject/controller/UserController.java
+++ b/src/main/java/com/example/myproject/controller/UserController.java
@@ -1,122 +1,190 @@
package com.example.myproject.controller;
-import com.example.myproject.entity.Users;
-import com.example.myproject.repository.UserRepository;
-import com.example.myproject.service.TaskService;
+import cn.dev33.satoken.annotation.SaCheckLogin;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.example.myproject.common.base.PageUtil;
+import com.example.myproject.dto.param.TorrentParam;
+import com.example.myproject.dto.vo.TorrentVO;
+import com.example.myproject.entity.TorrentEntity;
+import com.example.myproject.mapper.UserMapper;
+import com.example.myproject.mapper.VerificationTokenMapper;
+import com.example.myproject.entity.User;
+import com.example.myproject.entity.VerificationToken;
+import com.example.myproject.service.EmailService;
import com.example.myproject.service.UserService;
+import com.example.myproject.common.base.Result;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.media.Content;
+import io.swagger.v3.oas.annotations.media.Schema;
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
-import java.util.Map;
-import java.util.Optional;
+import javax.annotation.Resource;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.List;
@RestController
-@RequestMapping("/echo/user")
+@RequestMapping("/user")
+@Api(value = "用户管理接口", tags = {"用户管理"})
public class UserController {
- @Autowired
+ @Resource
private UserService userService;
@Autowired
- private UserRepository userRepository;
+ private AuthenticationManager authenticationManager;
- // 接口:生成邀请码
- @PostMapping("/getInviteCode")
- public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
- Long userId = Long.parseLong(request.get("user_id").toString());
- return userService.generateInviteCode(userId);
- }
+ @Autowired
+ private UserMapper userMapper; // 使用 MyBatis-Plus
- //注册
- @PostMapping("/register")
- public Map<String, Object> register(@RequestBody Map<String, Object> request) {
- String username = (String) request.get("username");
- String email = (String) request.get("email");
- String password = (String) request.get("password");
- String role = (String) request.get("role");
- String inviteCode = (String) request.get("inviteCode");
+ @Autowired
+ private VerificationTokenMapper verificationTokenMapper; // 替换 JPA
- // 调用服务层的注册方法
- String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
+ private static final Logger logger = LoggerFactory.getLogger(UserController.class);
- // 返回注册结果
- return Map.of("msg", resultMessage);
- }
-
- //登录
@PostMapping("/login")
- public Map<String, Object> login(@RequestBody Map<String, Object> request) {
- String username = (String) request.get("username");
- String password = (String) request.get("password");
+ @ApiOperation(value = "用户登录", notes = "使用用户名和密码进行登录")
+ public Result loginController(@RequestParam @ApiParam(value = "用户名", required = true) String username,
+ @RequestParam @ApiParam(value = "密码", required = true) String password) {
+ try {
+ Authentication authentication = authenticationManager.authenticate(
+ new UsernamePasswordAuthenticationToken(username, password)
+ );
+ SecurityContextHolder.getContext().setAuthentication(authentication);
- // 调用服务层的登录方法
- String resultMessage = userService.loginUser(username, password);
+ // 使用 MyBatis-Plus 查询
+ User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
- // 根据登录结果返回不同的响应
- if (resultMessage.equals("登录成功")) {
- // 查询用户信息
- Optional<Users> user = userRepository.findByUsername(username);
- if (user.isPresent()) {
- // 将用户的所有信息作为返回值
- return Map.of("msg", resultMessage, "user", user.get());
- } else {
- return Map.of("msg", "用户信息查询失败");
- }
- } else {
- return Map.of("msg", resultMessage);
+ System.out.println("Login successful for user: " + username);
+ return Result.ok(user);
+ } catch (AuthenticationException e) {
+ return Result.error("登录失败");
}
}
- //修改密码
- @PostMapping("/password")
- public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
- Long userId = Long.parseLong(request.get("user_id").toString());
- String oldPassword = (String) request.get("old_password");
- String newPassword = (String) request.get("new_password");
- String confirmPassword = (String) request.get("confirm_password");
-
- // 调用服务层的修改密码方法
- String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
-
- // 返回修改结果
- return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
- }
-
- // 获取用户个人资料
- @GetMapping("/{userId}/getProfile")
- public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
- return userService.getProfile(userId);
- }
-
- // 修改用户个人资料
- @PutMapping("/{userId}/editProfile")
- public Map<String, String> editProfile(
- @PathVariable("userId") Long userId,
- @RequestBody Map<String, Object> profileData) {
-
- // 获取请求体中的修改数据
- String avatarUrl = (String) profileData.get("avatarUrl");
- String nickname = (String) profileData.get("nickname");
- String gender = (String) profileData.get("gender");
- String description = (String) profileData.get("description");
- String hobbies = (String) profileData.get("hobbies");
-
- // 调用服务层方法进行修改
- boolean updated = userService.editProfile(userId, avatarUrl, nickname, gender, description, hobbies);
-
- // 返回操作结果消息
- if (updated) {
- return Map.of("message", "用户资料更新成功");
+ @PostMapping("/register")
+ @ApiOperation(value = "用户注册", notes = "使用用户信息进行注册")
+ public Result registerController(@RequestBody @ApiParam(value = "新用户信息", required = true) User newUser) {
+ if (userService.checkEmailExists(newUser.getEmail())) {
+ return Result.error( "邮箱已被使用,请使用其他邮箱注册或找回密码!");
+ }
+ boolean success = userService.preRegisterUser(newUser);
+ if (success) {
+ User responseUser = new User();
+ responseUser.setEmail(newUser.getEmail());
+ return Result.ok();
} else {
- return Map.of("message", "用户不存在");
+ return Result.error("账号已存在或注册失败!");
}
}
- // 计算分享率
- @GetMapping("/{user_id}/calculate-share-rate")
- public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
- return userService.calculateShareRate(userId);
+ public static class VerificationRequest {
+ private String email;
+ private String code;
+
+ public String getEmail() { return email; }
+ public void setEmail(String email) { this.email = email; }
+ public String getCode() { return code; }
+ public void setCode(String code) { this.code = code; }
}
+ @PostMapping("/verify-code")
+ @ApiOperation(value = "验证邮箱验证码", notes = "验证用户邮箱的验证码")
+ public Result verifyEmailCode(@RequestBody @ApiParam(value = "验证请求信息", required = true) VerificationRequest verificationRequest) {
+ String email = verificationRequest.getEmail();
+ String code = verificationRequest.getCode();
+ boolean isVerified = userService.verifyEmail(email, code);
+ if (isVerified) {
+ return Result.ok();
+ } else {
+ return Result.error( "验证码错误或已过期!");
+ }
+ }
+
+ @Autowired
+ private EmailService emailService;
+
+ public static class EmailRequest {
+ private String email;
+ public String getEmail() { return email; }
+ public void setEmail(String email) { this.email = email; }
+ }
+
+ @PostMapping("/get-verification-email")
+ @ApiOperation(value = "发送验证邮件", notes = "通过电子邮件发送验证邮件")
+ public ResponseEntity<Result> sendVerificationEmail(@RequestBody @ApiParam(value = "发送验证请求", required = true) EmailRequest emailVerificationRequest) {
+ String email = emailVerificationRequest.getEmail();
+ User user = userMapper.selectOne(new QueryWrapper<User>().eq("email", email));
+ if (user == null) {
+ logger.error("未找到与该邮箱地址相关联的用户: {}", email);
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST)
+ .body(Result.error("未找到与该邮箱地址相关联的用户"));
+ }
+
+ // 生成验证码
+ String token = RandomStringUtils.randomNumeric(6);
+ Instant expiryDate = Instant.now().plus(1, ChronoUnit.HOURS);
+ logger.info("生成的验证令牌: {}, 过期时间: {}", token, expiryDate);
+
+ VerificationToken verificationToken = new VerificationToken(token, user.getUsername(), email, user.getPassword(), expiryDate);
+
+ // 保存到 MyBatis-Plus 数据库
+ verificationTokenMapper.insert(verificationToken);
+
+ logger.info("验证令牌已保存,用户: {}", user.getUsername());
+ emailService.sendVerificationEmail(email, token);
+
+ return ResponseEntity.ok(Result.ok());
+ }
+ @PostMapping("/checkPassword")
+ public Result<String> checkPassword(@RequestParam Long userId, @RequestParam String password) {
+ boolean isPasswordCorrect = userService.checkPassword(userId, password);
+ if (isPasswordCorrect) {
+ return Result.ok();
+ } else {
+ return Result.error("原始密码输入错误");
+ }
+ }
+
+
+// @SaCheckLogin
+// @Operation(summary = "用户收藏列表", description = "获取用户收藏的种子列表-分页-排序")
+// @ApiResponse(responseCode = "0", description = "操作成功",
+// content = {@Content(mediaType = "application/json",
+// schema = @Schema(implementation = TorrentVO.class))
+// })
+// @PostMapping("/favorite/list")
+// public Result listFavorites(@RequestBody FavoriteParam param) {
+// if (param.getUserId() == null) {
+// return Result.error("缺少 userId");
+// }
+//
+// // 校验排序字段是否合理(可选)
+// param.validOrder(param.getOrderKey(TorrentEntity.class));
+//
+// PageUtil.startPage(param);
+//
+// List<TorrentEntity> list = favoriteService.getUserFavoritesPaged(param.getUserId());
+//
+// return Result.ok(list, PageUtil.getPage(list));
+// }
+//
+
+
}
diff --git a/src/main/java/com/example/myproject/controller/UserFollowController.java b/src/main/java/com/example/myproject/controller/UserFollowController.java
deleted file mode 100644
index f550beb..0000000
--- a/src/main/java/com/example/myproject/controller/UserFollowController.java
+++ /dev/null
@@ -1,47 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.service.UserFollowService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.*;
-
-import java.util.Map;
-
-@RestController
-@RequestMapping("/echo/users")
-public class UserFollowController {
-
- @Autowired
- private UserFollowService userFollowService;
-
- // 用户关注接口
- @PostMapping("/{followed_id}/follow")
- public ResponseEntity<Map<String, Object>> follow(@PathVariable("followed_id") Long followedId,
- @RequestBody Map<String, Long> request) {
- Long followerId = request.get("follower_id");
- Map<String, Object> response = userFollowService.follow(followerId, followedId);
- return ResponseEntity.ok(response);
- }
-
- // 取消关注接口
- @PostMapping("/{followed_id}/unfollow")
- public ResponseEntity<Map<String, Object>> unfollow(@PathVariable("followed_id") Long followedId,
- @RequestBody Map<String, Long> request) {
- Long followerId = request.get("follower_id");
- Map<String, Object> response = userFollowService.unfollow(followerId, followedId);
- return ResponseEntity.ok(response);
- }
-
- // 获取某个用户的粉丝列表
- @GetMapping("/{user_id}/followers")
- public ResponseEntity<Map<String, Object>> getFollowers(@PathVariable("user_id") Long userId) {
- Map<String, Object> response = userFollowService.getFollowers(userId);
- return ResponseEntity.ok(response);
- }
-
- @GetMapping("/{user_id}/following")
- public ResponseEntity<Map<String, Object>> getFollowing(@PathVariable("user_id") Long userId) {
- Map<String, Object> response = userFollowService.getFollowing(userId);
- return ResponseEntity.ok(response);
- }
-}
diff --git a/src/main/java/com/example/myproject/controller/UserMessageController.java b/src/main/java/com/example/myproject/controller/UserMessageController.java
deleted file mode 100644
index c7014a7..0000000
--- a/src/main/java/com/example/myproject/controller/UserMessageController.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.service.UserMessageService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.*;
-
-import java.util.Map;
-
-@RestController
-@RequestMapping("/echo/message")
-public class UserMessageController {
-
- @Autowired
- private UserMessageService userMessageService;
-
- @PostMapping("/sendMessages")
- public ResponseEntity<Map<String, Object>> sendMessage(@RequestBody Map<String, Object> params) {
- // 将参数转换为 Long 类型
- Long senderId = Long.valueOf(params.get("sender_id").toString());
- Long receiverId = Long.valueOf(params.get("receiver_id").toString());
- String content = (String) params.get("content");
- Map<String, Object> response = userMessageService.sendMessage(senderId, receiverId, content);
- return ResponseEntity.ok(response);
- }
-
- @GetMapping("/{user_id}/getUserMessages")
- public ResponseEntity<Map<String, Object>> getUserMessages(@PathVariable("user_id") Long userId) {
- Map<String, Object> response = userMessageService.getUserMessages(userId);
- return ResponseEntity.ok(response);
- }
-
- // 获取单条消息的详情
- @GetMapping("/{message_id}/getAMessage")
- public ResponseEntity<Map<String, Object>> getMessage(@PathVariable("message_id") Long messageId) {
- Map<String, Object> response = userMessageService.getMessage(messageId);
- return ResponseEntity.ok(response);
- }
-}
diff --git a/src/main/java/com/example/myproject/dto/PromotionCreateDTO.java b/src/main/java/com/example/myproject/dto/PromotionCreateDTO.java
new file mode 100644
index 0000000..211979b
--- /dev/null
+++ b/src/main/java/com/example/myproject/dto/PromotionCreateDTO.java
@@ -0,0 +1,35 @@
+package com.example.myproject.dto;
+
+import lombok.Data;
+import javax.validation.constraints.*;
+import java.time.LocalDateTime;
+import java.util.List;
+
+@Data
+public class PromotionCreateDTO {
+ @NotBlank(message = "促销名称不能为空")
+ @Size(max = 100, message = "促销名称长度不能超过100个字符")
+ private String name;
+
+ @Size(max = 500, message = "描述长度不能超过500个字符")
+ private String description;
+
+ @NotNull(message = "开始时间不能为空")
+ private LocalDateTime startTime;
+
+ @NotNull(message = "结束时间不能为空")
+ private LocalDateTime endTime;
+
+ @NotNull(message = "折扣比例不能为空")
+ @Min(value = 0, message = "折扣比例不能小于0")
+ @Max(value = 100, message = "折扣比例不能大于100")
+ private double discountPercentage;
+
+ @NotEmpty(message = "适用种子列表不能为空")
+ private List<Long> applicableTorrentIds;
+
+ @AssertTrue(message = "结束时间必须晚于开始时间")
+ public boolean isEndTimeAfterStartTime() {
+ return endTime != null && startTime != null && endTime.isAfter(startTime);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/dto/TorrentUpdateDTO.java b/src/main/java/com/example/myproject/dto/TorrentUpdateDTO.java
new file mode 100644
index 0000000..4b09321
--- /dev/null
+++ b/src/main/java/com/example/myproject/dto/TorrentUpdateDTO.java
@@ -0,0 +1,24 @@
+package com.example.myproject.dto;
+
+import lombok.Data;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.Size;
+
+@Data
+public class TorrentUpdateDTO {
+ @NotBlank(message = "标题不能为空")
+ @Size(max = 30, message = "名称长度不能超过30个字符")
+ private String title;
+
+ @Size(max = 1000, message = "描述长度不能超过1000个字符")
+ private String description;
+
+ @NotBlank(message = "分类不能为空")
+ private String category;
+
+
+ private String tags;
+ @NotBlank(message = "封面不能为空")
+
+ private String imageUrl;
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/dto/param/TorrentParam.java b/src/main/java/com/example/myproject/dto/param/TorrentParam.java
new file mode 100644
index 0000000..1ef832b
--- /dev/null
+++ b/src/main/java/com/example/myproject/dto/param/TorrentParam.java
@@ -0,0 +1,43 @@
+package com.example.myproject.dto.param;
+
+import com.example.myproject.common.base.OrderPageParam;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+/**
+ * 种子查询参数
+ */
+@Data
+@Schema(description = "种子查询参数")
+public class TorrentParam extends OrderPageParam {
+
+ @Schema(description = "关键字")
+ private String keyword;
+
+ @Schema(description = "分类")
+ private String category;
+
+ @Schema(description = "促销种子")
+ private String free;
+
+ private Set<String> likeExpressions;
+
+ public void buildLike() {
+ likeExpressions = new LinkedHashSet<>();
+ if (StringUtils.isEmpty(keyword)) {
+ return;
+ }
+ keyword = keyword.replace(".", " ");
+ String[] searchstrExploded = keyword.split(" ");
+ for (int i = 0; i < searchstrExploded.length && i < 10; i++) {
+ String searchstrElement = searchstrExploded[i].trim();
+ if (!searchstrElement.isEmpty()) {
+ likeExpressions.add(searchstrElement);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/dto/param/TorrentUploadParam.java b/src/main/java/com/example/myproject/dto/param/TorrentUploadParam.java
new file mode 100644
index 0000000..6ede468
--- /dev/null
+++ b/src/main/java/com/example/myproject/dto/param/TorrentUploadParam.java
@@ -0,0 +1,28 @@
+package com.example.myproject.dto.param;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+@Schema(description = "种子上传参数")
+public class TorrentUploadParam {
+ @Schema(description = "上传者")
+ private String uploader;
+
+ @Schema(description = "种子标题")
+ private String title;
+
+ @Schema(description = "种子描述")
+ private String description;
+
+ @Schema(description = "种子标签")
+ private String tags;
+
+ @Schema(description = "种子分类")
+ private String category;
+
+ @Schema(description = "种子封面图 URL")
+ private String imageUrl;
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/dto/vo/TorrentVO.java b/src/main/java/com/example/myproject/dto/vo/TorrentVO.java
new file mode 100644
index 0000000..e723cb3
--- /dev/null
+++ b/src/main/java/com/example/myproject/dto/vo/TorrentVO.java
@@ -0,0 +1,105 @@
+package com.example.myproject.dto.vo;
+
+import java.time.LocalDateTime;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import javax.validation.constraints.NotEmpty;
+import javax.validation.constraints.NotNull;
+import lombok.Data;
+
+@Data
+public class TorrentVO {
+ /**
+ * 种子ID
+ */
+ @Schema(description = "种子ID")
+ private Long id;
+
+ /**
+ * 标题
+ */
+ @NotEmpty
+ @Schema(description = "标题")
+ private String title;
+
+
+ /**
+ * 封面
+ */
+ @Schema(description = "封面")
+ private String imageUrl;
+ /**
+ * 描述
+ */
+ @NotEmpty
+ @Schema(description = "描述")
+ private String description;
+
+ /**
+ * 类别
+ */
+ @NotNull
+ @Schema(description = "类别")
+ private String category;
+
+
+ /**
+ * 添加日期
+ */
+ @Schema(description = "添加日期")
+ private LocalDateTime createTime;
+
+ /**
+ * 修改日期
+ */
+ @Schema(description = "修改日期")
+ private LocalDateTime updateTime;
+
+ /**
+ * 上传者
+ */
+ @Schema(description = "上传者")
+ private String uploader;
+ /**
+ * 文件大小
+ */
+ @Schema(description = "文件大小")
+ private Long size;
+
+
+
+ /**
+ * 评论数
+ */
+ @Schema(description = "评论数")
+ private Integer comments;
+ /**
+ * 浏览次数
+ */
+ @Schema(description = "浏览次数")
+ private Integer views;
+ /**
+ * 点击次数
+ */
+ @Schema(description = "点击次数")
+ private Integer hits;
+
+
+ /**
+ * 下载数
+ */
+ @Schema(description = "下载数")
+ private Integer leechers;
+ /**
+ * 做种数
+ */
+ @Schema(description = "做种数")
+ private Integer seeders;
+
+ /**
+ * 完成次数
+ */
+ @Schema(description = "完成次数")
+ private Integer completions;
+
+}
diff --git a/src/main/java/com/example/myproject/entity/Collections.java b/src/main/java/com/example/myproject/entity/Collections.java
deleted file mode 100644
index f7a8711..0000000
--- a/src/main/java/com/example/myproject/entity/Collections.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-
-@Entity
-@Table(name = "collections")
-public class Collections {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "collectionId")
- private Long collectionId;
-
- @Column(name = "user_id")
- private Long userId;
-
- @Column(name = "postNo")
- private Long postNo;
-
- // Getters and Setters
- public Long getCollectionId() {
- return collectionId;
- }
-
- public void setCollectionId(Long collectionId) {
- this.collectionId = collectionId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public Long getPostNo() {
- return postNo;
- }
-
- public void setPostNo(Long postNo) {
- this.postNo = postNo;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/Comments.java b/src/main/java/com/example/myproject/entity/Comments.java
deleted file mode 100644
index fdf89fa..0000000
--- a/src/main/java/com/example/myproject/entity/Comments.java
+++ /dev/null
@@ -1,116 +0,0 @@
-package com.example.myproject.entity;
-
-import com.fasterxml.jackson.annotation.JsonBackReference;
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "comments")
-public class Comments {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "comment_id")
- private Long commentId;
-
- @ManyToOne
- @JoinColumn(name = "post_id", nullable = false)
- @JsonBackReference
- private Post post;
-
- @Column(name = "content", nullable = false)
- private String content;
-
- @Column(name = "is_anonymous")
- private Boolean isAnonymous; // 是否匿名
-
- @Column(name = "likes_count")
- private Integer likesCount = 0; // 点赞数
-
- @Column(name = "reply_count")
- private Integer replyCount = 0; // 回复数
-
- @Column(name = "comment_time", nullable = false)
- private Date commentTime; // 评论时间
-
- @Column(name = "user_id", nullable = false)
- private Long userId; // 评论者的 user_id
-
-
- @Column(name = "com_comment_id", nullable = false)
- private Long com_comment_id;
-
- // Getters and Setters
-
- public Long getCommentId() {
- return commentId;
- }
-
- public void setCommentId(Long commentId) {
- this.commentId = commentId;
- }
-
- public Post getPost() {
- return post;
- }
-
- public void setPost(Post post) {
- this.post = post;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public Boolean getIsAnonymous() {
- return isAnonymous;
- }
-
- public void setIsAnonymous(Boolean isAnonymous) {
- this.isAnonymous = isAnonymous;
- }
-
- public Integer getLikesCount() {
- return likesCount;
- }
-
- public void setLikesCount(Integer likesCount) {
- this.likesCount = likesCount;
- }
-
- public Integer getReplyCount() {
- return replyCount;
- }
-
- public void setReplyCount(Integer replyCount) {
- this.replyCount = replyCount;
- }
-
- public Date getCommentTime() {
- return commentTime;
- }
-
- public void setCommentTime(Date commentTime) {
- this.commentTime = commentTime;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public Long getParentComment() {
- return com_comment_id;
- }
-
- public void setParentComment(Long com_comment_id) {
- this.com_comment_id = com_comment_id;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/DynamicComment.java b/src/main/java/com/example/myproject/entity/DynamicComment.java
deleted file mode 100644
index d5c84d3..0000000
--- a/src/main/java/com/example/myproject/entity/DynamicComment.java
+++ /dev/null
@@ -1,89 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "dynamic_comment")
-public class DynamicComment {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "comment_id")
- private Long commentId;
-
- @Column(name = "dynamic_id")
- private Long dynamicId;
-
- @Column(name = "user_id")
- private Long userId;
-
- @Column(name = "comment_content")
- private String commentContent;
-
- @Column(name = "comment_time")
- private Date commentTime;
-
- @Column(name = "is_anonymous")
- private Boolean isAnonymous = false;
-
- @Column(name = "parent_comment_id")
- private Long parentCommentId;
-
- // Getters and Setters
- public Long getCommentId() {
- return commentId;
- }
-
- public void setCommentId(Long commentId) {
- this.commentId = commentId;
- }
-
- public Long getDynamicId() {
- return dynamicId;
- }
-
- public void setDynamicId(Long dynamicId) {
- this.dynamicId = dynamicId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public String getCommentContent() {
- return commentContent;
- }
-
- public void setCommentContent(String commentContent) {
- this.commentContent = commentContent;
- }
-
- public Date getCommentTime() {
- return commentTime;
- }
-
- public void setCommentTime(Date commentTime) {
- this.commentTime = commentTime;
- }
-
- public Boolean getIsAnonymous() {
- return isAnonymous;
- }
-
- public void setIsAnonymous(Boolean isAnonymous) {
- this.isAnonymous = isAnonymous;
- }
-
- public Long getParentCommentId() {
- return parentCommentId;
- }
-
- public void setParentCommentId(Long parentCommentId) {
- this.parentCommentId = parentCommentId;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/DynamicLikes.java b/src/main/java/com/example/myproject/entity/DynamicLikes.java
deleted file mode 100644
index 986f903..0000000
--- a/src/main/java/com/example/myproject/entity/DynamicLikes.java
+++ /dev/null
@@ -1,56 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "dynamic_likes")
-public class DynamicLikes {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "like_id")
- private Long likeId;
-
- @Column(name = "dynamic_id")
- private Long dynamicId;
-
- @Column(name = "user_id")
- private Long userId;
-
- @Column(name = "like_time")
- private Date likeTime;
-
- // Getters and Setters
- public Long getLikeId() {
- return likeId;
- }
-
- public void setLikeId(Long likeId) {
- this.likeId = likeId;
- }
-
- public Long getDynamicId() {
- return dynamicId;
- }
-
- public void setDynamicId(Long dynamicId) {
- this.dynamicId = dynamicId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public Date getLikeTime() {
- return likeTime;
- }
-
- public void setLikeTime(Date likeTime) {
- this.likeTime = likeTime;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/EntityBase.java b/src/main/java/com/example/myproject/entity/EntityBase.java
new file mode 100644
index 0000000..48583cb
--- /dev/null
+++ b/src/main/java/com/example/myproject/entity/EntityBase.java
@@ -0,0 +1,61 @@
+package com.example.myproject.entity;
+
+
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+
+import java.util.Objects;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * Base class for an entity, as explained in the book "Domain Driven Design".
+ * All entities in this project have an identity attribute with type Long and
+ * name id. Inspired by the DDD Sample project.
+
+ */
+@Setter
+@Getter
+public abstract class EntityBase {
+
+ /**
+ * This identity field has the wrapper class type Long so that an entity which
+ * has not been saved is recognizable by a null identity.
+ */
+ @TableId(type = IdType.AUTO)
+ private Integer id;
+
+ @Override
+ public boolean equals(final Object object) {
+ if (!(object instanceof EntityBase)) {
+ return false;
+ }
+ if (!getClass().equals(object.getClass())) {
+ return false;
+ }
+ final EntityBase that = (EntityBase) object;
+ _checkIdentity(this);
+ _checkIdentity(that);
+ return this.id.equals(that.getId());
+ }
+
+
+ private void _checkIdentity(final EntityBase entity) {
+ if (entity.getId() == null) {
+ throw new IllegalStateException("Comparison identity missing in entity: " + entity);
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(this.getId());
+ }
+
+ @Override
+ public String toString() {
+ return this.getClass().getSimpleName() + "<" + getId() + ">";
+ }
+
+}
diff --git a/src/main/java/com/example/myproject/entity/ExperienceHistory.java b/src/main/java/com/example/myproject/entity/ExperienceHistory.java
deleted file mode 100644
index 8c52f6b..0000000
--- a/src/main/java/com/example/myproject/entity/ExperienceHistory.java
+++ /dev/null
@@ -1,67 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "experience_history")
-public class ExperienceHistory {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "history_id")
- private Long historyId;
-
- @Column(name = "user_id", nullable = false)
- private Long userId;
-
- @Column(name = "experience_change", nullable = false)
- private Integer experienceChange; // 经验值变化量
-
- @Column(name = "source", nullable = false)
- private String source; // 更新来源
-
- @Column(name = "update_time", nullable = false)
- private Date updateTime; // 更新操作的时间
-
- // Getters and Setters
- public Long getHistoryId() {
- return historyId;
- }
-
- public void setHistoryId(Long historyId) {
- this.historyId = historyId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public Integer getExperienceChange() {
- return experienceChange;
- }
-
- public void setExperienceChange(Integer experienceChange) {
- this.experienceChange = experienceChange;
- }
-
- public String getSource() {
- return source;
- }
-
- public void setSource(String source) {
- this.source = source;
- }
-
- public Date getUpdateTime() {
- return updateTime;
- }
-
- public void setUpdateTime(Date updateTime) {
- this.updateTime = updateTime;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/FavoriteEntity.java b/src/main/java/com/example/myproject/entity/FavoriteEntity.java
new file mode 100644
index 0000000..3f76cfe
--- /dev/null
+++ b/src/main/java/com/example/myproject/entity/FavoriteEntity.java
@@ -0,0 +1,36 @@
+package com.example.myproject.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+@TableName("favorite")
+@ApiModel("收藏实体类")
+public class FavoriteEntity {
+ @TableId(type = IdType.AUTO)
+ @ApiModelProperty(value = "收藏ID", example = "1")
+ private Long id;
+
+ @ApiModelProperty(value = "用户ID", example = "1001")
+ @JsonProperty("userId")
+ @TableField("user_id")
+ private Long userId;
+
+ @ApiModelProperty(value = "种子ID", example = "2001")
+ @JsonProperty("seedId")
+ @TableField("seed_id")
+ private Long seedId;
+
+ @ApiModelProperty(value = "收藏时间", example = "2024-05-13 12:00:00")
+ @JsonProperty("createTime")
+ @TableField("create_time")
+ private Date createTime;
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/entity/FriendRelation.java b/src/main/java/com/example/myproject/entity/FriendRelation.java
deleted file mode 100644
index c086001..0000000
--- a/src/main/java/com/example/myproject/entity/FriendRelation.java
+++ /dev/null
@@ -1,56 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "friend_relation")
-public class FriendRelation {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "relation_id")
- private Long relationId;
-
- @Column(name = "user_id")
- private Long userId;
-
- @Column(name = "friend_id")
- private Long friendId; // 好友ID
-
- @Column(name = "create_time")
- private Date createTime; // 好友关系创建时间
-
- // Getters and Setters
- public Long getRelationId() {
- return relationId;
- }
-
- public void setRelationId(Long relationId) {
- this.relationId = relationId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public Long getFriendId() {
- return friendId;
- }
-
- public void setFriendId(Long friendId) {
- this.friendId = friendId;
- }
-
- public Date getCreateTime() {
- return createTime;
- }
-
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/Group.java b/src/main/java/com/example/myproject/entity/Group.java
deleted file mode 100644
index ea4f2c7..0000000
--- a/src/main/java/com/example/myproject/entity/Group.java
+++ /dev/null
@@ -1,98 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "group_interests")
-public class Group {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "group_id")
- private Long groupId;
-
- @Column(name = "group_name")
- private String groupName;
-
- private String description; // 小组简介
-
- @Column(name = "create_time")
- private Date createTime; // 创建时间
-
- @Column(name = "user_id")
- private Long userId; // 创建该小组的用户ID
-
- @Column(name = "member_count")
- private Integer memberCount; // 小组成员人数
-
- private String category; // 小组类别
-
- @Column(name = "cover_image")
- private String coverImage; // 小组封面图片
-
- // Getters and Setters
- public Long getGroupId() {
- return groupId;
- }
-
- public void setGroupId(Long groupId) {
- this.groupId = groupId;
- }
-
- public String getGroupName() {
- return groupName;
- }
-
- public void setGroupName(String groupName) {
- this.groupName = groupName;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public Date getCreateTime() {
- return createTime;
- }
-
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public Integer getMemberCount() {
- return memberCount;
- }
-
- public void setMemberCount(Integer memberCount) {
- this.memberCount = memberCount;
- }
-
- public String getCategory() {
- return category;
- }
-
- public void setCategory(String category) {
- this.category = category;
- }
-
- public String getCoverImage() {
- return coverImage;
- }
-
- public void setCoverImage(String coverImage) {
- this.coverImage = coverImage;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/GroupComments.java b/src/main/java/com/example/myproject/entity/GroupComments.java
deleted file mode 100644
index fcf4205..0000000
--- a/src/main/java/com/example/myproject/entity/GroupComments.java
+++ /dev/null
@@ -1,77 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "group_comments")
-public class GroupComments {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "comment_id")
- private Long commentId; // 评论ID
-
- @Column(name = "group_post_id")
- private Long groupPostId;
-
- @Column(name = "user_id")
- private Long userId;
-
- private String content;
-
- @Column(name = "parent_comment_id")
- private Long parentCommentId;
-
- @Column(name = "time")
- private Date time; // 评论时间
-
- // Getters and Setters
- public Long getCommentId() {
- return commentId;
- }
-
- public void setCommentId(Long commentId) {
- this.commentId = commentId;
- }
-
- public Long getGroupPostId() {
- return groupPostId;
- }
-
- public void setGroupPostId(Long groupPostId) {
- this.groupPostId = groupPostId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public Long getParentCommentId() {
- return parentCommentId;
- }
-
- public void setParentCommentId(Long parentCommentId) {
- this.parentCommentId = parentCommentId;
- }
-
- public Date getTime() {
- return time;
- }
-
- public void setTime(Date time) {
- this.time = time;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/GroupMembers.java b/src/main/java/com/example/myproject/entity/GroupMembers.java
deleted file mode 100644
index e66e18c..0000000
--- a/src/main/java/com/example/myproject/entity/GroupMembers.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-
-@Entity
-@Table(name = "group_members")
-public class GroupMembers {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "relation_id")
- private Long relationId;
-
- @Column(name = "group_id", nullable = false)
- private Long groupId;
-
- @Column(name = "user_id", nullable = false)
- private Long userId;
-
- // Getters and Setters
- public Long getRelationId() {
- return relationId;
- }
-
- public void setRelationId(Long relationId) {
- this.relationId = relationId;
- }
-
- public Long getGroupId() {
- return groupId;
- }
-
- public void setGroupId(Long groupId) {
- this.groupId = groupId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/GroupPost.java b/src/main/java/com/example/myproject/entity/GroupPost.java
deleted file mode 100644
index 89867f7..0000000
--- a/src/main/java/com/example/myproject/entity/GroupPost.java
+++ /dev/null
@@ -1,109 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "group_post")
-public class GroupPost {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "group_post_id")
- private Long groupPostId;
-
- @Column(name = "group_id")
- private Long groupId;
-
- @Column(name = "user_id")
- private Long userId;
-
- private String content;
-
- private String image;
-
- @Column(name = "like_count")
- private Integer likeCount;
-
- @Column(name = "comment_count")
- private Integer commentCount; // 帖子评论数
-
- @Column(name = "time")
- private Date time; // 帖子创建时间
-
- private String title;
-
-
- // Getters and Setters
- public Long getGroupPostId() {
- return groupPostId;
- }
-
- public void setGroupPostId(Long groupPostId) {
- this.groupPostId = groupPostId;
- }
-
- public Long getGroupId() {
- return groupId;
- }
-
- public void setGroupId(Long groupId) {
- this.groupId = groupId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public String getImage() {
- return image;
- }
-
- public void setImage(String image) {
- this.image = image;
- }
-
- public Integer getLikeCount() {
- return likeCount;
- }
-
- public void setLikeCount(Integer likeCount) {
- this.likeCount = likeCount;
- }
-
- public Integer getCommentCount() {
- return commentCount;
- }
-
- public void setCommentCount(Integer commentCount) {
- this.commentCount = commentCount;
- }
-
- public Date getTime() {
- return time;
- }
-
- public void setTime(Date time) {
- this.time = time;
- }
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/Level.java b/src/main/java/com/example/myproject/entity/Level.java
deleted file mode 100644
index 6837ef9..0000000
--- a/src/main/java/com/example/myproject/entity/Level.java
+++ /dev/null
@@ -1,89 +0,0 @@
-package com.example.myproject.entity;
-
-
-
-import javax.persistence.*;
-
-@Entity
-@Table(name = "level")
-public class Level {
-
- @Id
- @Column(name = "level_id")
- private Long levelId; // 等级唯一标识
-
- @Column(name = "level_name", nullable = false)
- private String levelName; // 等级名称
-
- @Column(name = "required_experience", nullable = false)
- private Long requiredExperience; // 所需经验值
-
- @Column(name = "required_share_ratio")
- private Float requiredShareRatio; // 所需分享率
-
- @Column(name = "required_upload_gb")
- private Float requiredUploadGb; // 所需上传数据量
-
- @Column(name = "required_seeding_hours")
- private Float requiredSeedingHours; // 所需做种时长
-
- @Column(name = "permissions")
- private String permissions; // 拥有的权限
-
- // Getters and Setters
- public Long getLevelId() {
- return levelId;
- }
-
- public void setLevelId(Long levelId) {
- this.levelId = levelId;
- }
-
- public String getLevelName() {
- return levelName;
- }
-
- public void setLevelName(String levelName) {
- this.levelName = levelName;
- }
-
- public Long getRequiredExperience() {
- return requiredExperience;
- }
-
- public void setRequiredExperience(Long requiredExperience) {
- this.requiredExperience = requiredExperience;
- }
-
- public Float getRequiredShareRatio() {
- return requiredShareRatio;
- }
-
- public void setRequiredShareRatio(Float requiredShareRatio) {
- this.requiredShareRatio = requiredShareRatio;
- }
-
- public Float getRequiredUploadGb() {
- return requiredUploadGb;
- }
-
- public void setRequiredUploadGb(Float requiredUploadGb) {
- this.requiredUploadGb = requiredUploadGb;
- }
-
- public Float getRequiredSeedingHours() {
- return requiredSeedingHours;
- }
-
- public void setRequiredSeedingHours(Float requiredSeedingHours) {
- this.requiredSeedingHours = requiredSeedingHours;
- }
-
- public String getPermissions() {
- return permissions;
- }
-
- public void setPermissions(String permissions) {
- this.permissions = permissions;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/Likes.java b/src/main/java/com/example/myproject/entity/Likes.java
deleted file mode 100644
index 7a3dcda..0000000
--- a/src/main/java/com/example/myproject/entity/Likes.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-
-@Entity
-@Table(name = "likes")
-public class Likes {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY) // 使用自增主键
- @Column(name = "likeId")
- private Long likeId;
-
- @Column(name = "user_id")
- private Long userId;
-
- @Column(name = "postNo")
- private Long postNo;
-
- // Getters and Setters
- public Long getLikeId() {
- return likeId;
- }
-
- public void setLikeId(Long likeId) {
- this.likeId = likeId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public Long getPostNo() {
- return postNo;
- }
-
- public void setPostNo(Long postNo) {
- this.postNo = postNo;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/Post.java b/src/main/java/com/example/myproject/entity/Post.java
deleted file mode 100644
index bb94aac..0000000
--- a/src/main/java/com/example/myproject/entity/Post.java
+++ /dev/null
@@ -1,116 +0,0 @@
-package com.example.myproject.entity;
-
-import com.fasterxml.jackson.annotation.JsonManagedReference;
-
-import javax.persistence.*;
-
-import java.util.Date;
-import java.util.List;
-
-@Entity
-@Table(name = "post")
-public class Post {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "postNo")
- private Long postNo;
-
-
- @Column(name = "userId")
- private Long user_id;
-
- @Column(name = "postContent")
- private String postContent;
-
- @Column(name = "imageUrl")
- private String imageUrl;
-
- @Column(name = "postTime")
- private Date postTime;
-
- @Column(name = "postLikeNum")
- private Integer postLikeNum;
-
- @Column(name = "postCollectNum")
- private Integer postCollectNum;
-
- @Column(name = "title")
- private String title;
-
- @Column(name = "postType")
- private String postType;
-
- // Getters and Setters
- public Long getPostNo() {
- return postNo;
- }
-
- public void setPostNo(Long postNo) {
- this.postNo = postNo;
- }
-
- public Long getUser_id() {
- return user_id;
- }
-
- public void setUser_id(Long user) {
- this.user_id = user;
- }
-
- public String getPostContent() {
- return postContent;
- }
-
- public void setPostContent(String postContent) {
- this.postContent = postContent;
- }
-
- public String getImageUrl() {
- return imageUrl;
- }
-
- public void setImageUrl(String imgUrl) {
- this.imageUrl = imgUrl;
- }
-
- public Date getPostTime() {
- return postTime;
- }
-
- public void setPostTime(Date postTime) {
- this.postTime = postTime;
- }
-
- public Integer getPostLikeNum() {
- return postLikeNum;
- }
-
- public void setPostLikeNum(Integer postLikeNum) {
- this.postLikeNum = postLikeNum;
- }
-
- public Integer getPostCollectNum() {
- return postCollectNum;
- }
-
- public void setPostCollectNum(Integer postCollectNum) {
- this.postCollectNum = postCollectNum;
- }
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public String getPostType() {
- return postType;
- }
-
- public void setPostType(String postType) {
- this.postType = postType;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/Promotion.java b/src/main/java/com/example/myproject/entity/Promotion.java
new file mode 100644
index 0000000..4ca846b
--- /dev/null
+++ b/src/main/java/com/example/myproject/entity/Promotion.java
@@ -0,0 +1,34 @@
+package com.example.myproject.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+import java.util.List;
+
+@Data
+@TableName("promotion")
+public class Promotion {
+ @TableId(type = IdType.AUTO)
+ private Long id;
+
+ private String name;
+
+ private String description;
+
+ private LocalDateTime startTime;
+
+ private LocalDateTime endTime;
+
+ private double discountPercentage;
+
+ private String applicableTorrentIds;
+
+ private LocalDateTime createTime;
+
+ private LocalDateTime updateTime;
+
+ private Boolean isDeleted;
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/entity/SeedComment.java b/src/main/java/com/example/myproject/entity/SeedComment.java
deleted file mode 100644
index af6e91f..0000000
--- a/src/main/java/com/example/myproject/entity/SeedComment.java
+++ /dev/null
@@ -1,118 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.Entity;
-import javax.persistence.*;
-
-
-import java.util.Date;
-
-@Entity
-@Table(name = "seed_comments") // 映射到数据库表 seed_comments
-public class SeedComment {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增主键
- @Column(name = "comment_id") // 映射到表的 comment_id 列
- private long commentId;
-
-
- @Column(name = "seed_id", nullable = false)
- private long seedId;
-
-
- @Column(name = "com_comment_id", nullable = true)
- private long comCommentId; // 外键,指向父评论的 comment_id
-
-
- @Column(name = "user_id",nullable = false)
- private long userId;
-
- @Column(name = "content", nullable = false)
- private String content;
-
- @Column(name = "is_anonymous")
- private byte isAnonymous;
-
- @Column(name = "likes_count", nullable = false)
- private int likesCount;
-
- @Column(name = "reply_count", nullable = false)
- private int replyCount;
-
- @Column(name = "comment_time")
- private Date commentTime;
-
- // Getter 和 Setter 方法
-
- public long getCommentId() {
- return commentId;
- }
-
- public void setCommentId(long commentId) {
- this.commentId = commentId;
- }
-
- public long getSeedId() {
- return seedId;
- }
-
- public void setSeedId(long seedId) {
- this.seedId = seedId;
- }
-
- public long getComCommentId() {
- return comCommentId;
- }
-
- public void setComCommentId(long comCommentId) {
- this.comCommentId = comCommentId;
- }
-
- public long getUserId() {
- return userId;
- }
-
- public void setUserId(long userId) {
- this.userId = userId;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public byte getIsAnonymous() {
- return isAnonymous;
- }
-
- public void setIsAnonymous(byte isAnonymous) {
- this.isAnonymous = isAnonymous;
- }
-
- public int getLikesCount() {
- return likesCount;
- }
-
- public void setLikesCount(int likesCount) {
- this.likesCount = likesCount;
- }
-
- public int getReplyCount() {
- return replyCount;
- }
-
- public void setReplyCount(int replyCount) {
- this.replyCount = replyCount;
- }
-
- public Date getCommentTime() {
- return commentTime;
- }
-
- public void setCommentTime(Date commentTime) {
- this.commentTime = commentTime;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/SeedCommentLikes.java b/src/main/java/com/example/myproject/entity/SeedCommentLikes.java
deleted file mode 100644
index 65f7fc6..0000000
--- a/src/main/java/com/example/myproject/entity/SeedCommentLikes.java
+++ /dev/null
@@ -1,55 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-
-@Entity
-@Table(name = "seed_comment_likes")
-public class SeedCommentLikes {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "id")
- private Long id; // 点赞ID
-
- @Column(name = "comment_id", nullable = false)
- private Long commentId; // 被点赞的评论ID
-
- @Column(name = "user_id", nullable = false)
- private Long userId; // 点赞的用户ID
-
- @Column(name = "is_liked", nullable = false)
- private Boolean isLiked = true; // 点赞状态,默认为true(点赞)
-
- // Getters and Setters
- public Long getId() {
- return id;
- }
-
- public void setId(Long id) {
- this.id = id;
- }
-
- public Long getCommentId() {
- return commentId;
- }
-
- public void setCommentId(Long commentId) {
- this.commentId = commentId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public Boolean getIsLiked() {
- return isLiked;
- }
-
- public void setIsLiked(Boolean isLiked) {
- this.isLiked = isLiked;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/Task.java b/src/main/java/com/example/myproject/entity/Task.java
deleted file mode 100644
index 15796a1..0000000
--- a/src/main/java/com/example/myproject/entity/Task.java
+++ /dev/null
@@ -1,66 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-
-@Entity
-@Table(name = "tasks")
-public class Task {
-
- @Id
- @Column(name = "task_id")
- private String taskId;
-
- @Column(name = "title", nullable = false)
- private String title;
-
- @Column(name = "description", nullable = false)
- private String description;
-
- @Column(name = "reward_experience", nullable = false)
- private int rewardExperience; // 任务奖励的经验
-
- @Column(name = "reward_points", nullable = false)
- private int rewardPoints; // 任务奖励的积分
-
-
- public String getTaskId() {
- return taskId;
- }
-
- public void setTaskId(String taskId) {
- this.taskId = taskId;
- }
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
-
- public int getRewardExperience() {
- return rewardExperience;
- }
-
- public void setRewardExperience(int rewardExperience) {
- this.rewardExperience = rewardExperience;
- }
-
- public int getRewardPoints() {
- return rewardPoints;
- }
-
- public void setRewardPoints(int rewardPoints) {
- this.rewardPoints = rewardPoints;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/TorrentEntity.java b/src/main/java/com/example/myproject/entity/TorrentEntity.java
new file mode 100644
index 0000000..f62fb76
--- /dev/null
+++ b/src/main/java/com/example/myproject/entity/TorrentEntity.java
@@ -0,0 +1,108 @@
+package com.example.myproject.entity;
+
+import com.baomidou.mybatisplus.annotation.*;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+import java.util.Date;
+import java.util.List;
+
+@Data
+@TableName("torrent")
+@ApiModel("种子实体类")
+public class TorrentEntity {
+
+ @TableId(type = IdType.AUTO)
+ @ApiModelProperty(value = "种子ID", example = "1")
+ private Long id;
+
+ @JsonProperty("infoHash")
+ @ApiModelProperty(value = "种子信息哈希", example = "abcdef123456")
+ private String infoHash;
+
+ @JsonProperty("fileName")
+ @ApiModelProperty(value = "种子文件名", example = "movie_torrent_file.torrent")
+ private String fileName;
+
+ @JsonProperty("uploader")
+ @ApiModelProperty(value = "上传者", example = "user123")
+ private String uploader;
+
+ @JsonProperty("createdTime")
+ @ApiModelProperty(value = "上传时间", example = "2024-01-01 12:00:00")
+ @TableField(fill = FieldFill.INSERT)
+ private LocalDateTime createTime;
+
+ @JsonProperty("updateTime")
+ @ApiModelProperty(value = "更新时间", example = "2024-01-01 12:00:00")
+
+ @TableField(fill = FieldFill.INSERT_UPDATE)
+ private LocalDateTime updateTime;
+
+ @JsonProperty("size")
+ @ApiModelProperty(value = "种子文件大小", example = "123456")
+ private Long size;
+
+ @JsonProperty("title")
+ @ApiModelProperty(value = "种子标题", example = "《星际穿越》")
+ private String title;
+
+ @JsonProperty("description")
+ @ApiModelProperty(value = "种子描述", example = "这是一部好看的科幻电影")
+ private String description;
+
+ @JsonProperty("tags")
+ @ApiModelProperty(value = "种子标签", example = "[\"科幻\",\"动作\"]")
+ private String tags;
+
+ @JsonProperty("category")
+ @ApiModelProperty(value = "种子分类", example = "movie")
+ private String category;
+
+ @JsonProperty("imageUrl")
+ @ApiModelProperty(value = "种子封面图URL", example = "http://example.com/images/cover.jpg")
+ private String imageUrl;
+
+ @JsonProperty("leechers")
+ @ApiModelProperty(value = "下载次数", example = "123")
+ private Integer leechers;
+
+ @JsonProperty("seeders")
+ @ApiModelProperty(value = "做种数", example = "10")
+ private Integer seeders;
+
+ @JsonProperty("comments")
+ @ApiModelProperty(value = "评论数", example = "5")
+ private Integer comments;
+
+ @JsonProperty("views")
+ @ApiModelProperty(value = "浏览次数", example = "1000")
+ private Integer views;
+
+ @JsonProperty("hits")
+ @ApiModelProperty(value = "点击次数", example = "2000")
+ private Integer hits;
+
+ @JsonProperty("promotionTimeType")
+ @ApiModelProperty(value = "促销时间类型", example = "1")
+ private Integer promotionTimeType;
+
+ @JsonProperty("promotionUntil")
+ @ApiModelProperty(value = "促销截止日期", example = "2024-12-31T23:59:59")
+ private LocalDateTime promotionUntil;
+
+
+ @JsonProperty("torrentFile")
+ @ApiModelProperty(value = "种子文件", example = "base64 encoded torrent file")
+ private byte[] torrentFile;
+
+ @JsonProperty("isDeleted")
+ @ApiModelProperty(value = "是否删除", example = "false")
+ private Boolean isDeleted;
+
+ public TorrentEntity() {
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/entity/User.java b/src/main/java/com/example/myproject/entity/User.java
new file mode 100644
index 0000000..7574e67
--- /dev/null
+++ b/src/main/java/com/example/myproject/entity/User.java
@@ -0,0 +1,77 @@
+package com.example.myproject.entity;
+
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+
+@Data
+@TableName("user") // 指定数据库表名
+@ApiModel("用户实体类") // 用于描述模型
+public class User {
+
+ @TableId(type = IdType.AUTO) // 指定主键策略
+ @ApiModelProperty(value = "用户ID", example = "1")
+ private Long id;
+
+ @JsonProperty("username")
+ @ApiModelProperty(value = "用户名", example = "22301115")
+ private String username;
+
+ @JsonProperty("nickname")
+ @ApiModelProperty(value = "昵称", example = "cyl")
+ private String nickname;
+
+ @JsonProperty("role")
+ @ApiModelProperty(value = "角色", example = "Student")
+ private String role;
+
+ @JsonProperty("password")
+ @ApiModelProperty(value = "密码", example = "123")
+ private String password;
+
+ @JsonProperty("status")
+ @ApiModelProperty(value = "用户状态", example = "1")
+ private int status;
+
+ @JsonProperty("email")
+ @ApiModelProperty(value = "电子邮件地址", example = "john_doe@example.com")
+ private String email;
+
+ @JsonProperty("email_verified")
+ @ApiModelProperty(value = "邮箱验证状态", example = "true")
+ private boolean emailVerified;
+
+ @JsonProperty("avatar")
+ @ApiModelProperty(value = "头像")
+ private String avatar;
+
+ @JsonProperty("uploaded")
+ @ApiModelProperty(value = "上传量", example = "1000")
+ private Long uploaded;
+
+ @JsonProperty("downloaded")
+ @ApiModelProperty(value = "下载量", example = "500")
+ private Long downloaded;
+
+ @JsonProperty("create_time")
+ @ApiModelProperty(value = "创建时间", example = "2024-04-01T12:00:00")
+ private LocalDateTime createTime;
+
+ @JsonProperty("update_time")
+ @ApiModelProperty(value = "更新时间", example = "2024-04-01T12:00:00")
+ private LocalDateTime updateTime;
+
+ @JsonProperty("is_deleted")
+ @ApiModelProperty(value = "是否删除", example = "false")
+ private Boolean isDeleted;
+
+ public User() {
+ }
+}
diff --git a/src/main/java/com/example/myproject/entity/UserDetails.java b/src/main/java/com/example/myproject/entity/UserDetails.java
new file mode 100644
index 0000000..9af35f0
--- /dev/null
+++ b/src/main/java/com/example/myproject/entity/UserDetails.java
@@ -0,0 +1,23 @@
+package com.example.myproject.entity;
+
+import org.springframework.security.core.GrantedAuthority;
+
+import java.io.Serializable;
+import java.util.Collection;
+
+public interface UserDetails extends Serializable {
+ Collection<? extends GrantedAuthority> getAuthorities();
+
+ String getPassword();
+
+ String getUsername();
+
+ boolean isAccountNonExpired();
+
+ boolean isAccountNonLocked();
+
+ boolean isCredentialsNonExpired();
+
+ boolean isEnabled();
+}
+
diff --git a/src/main/java/com/example/myproject/entity/UserDynamic.java b/src/main/java/com/example/myproject/entity/UserDynamic.java
deleted file mode 100644
index a04d3ac..0000000
--- a/src/main/java/com/example/myproject/entity/UserDynamic.java
+++ /dev/null
@@ -1,100 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "user_dynamic")
-public class UserDynamic {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "dynamic_id")
- private Long dynamicId;
-
- @Column(name = "user_id")
- private Long userId; // 发布动态的用户ID
-
- @Column(name = "title")
- private String title; // 动态标题
-
- @Column(name = "content")
- private String content; // 动态内容
-
- @Column(name = "image_url")
- private String imageUrl; // 图片URL
-
- @Column(name = "time")
- private Date time; // 发布动态的时间
-
- @Column(name = "likes_count")
- private int likesCount = 0; // 点赞数
-
- @Column(name = "comments_count")
- private int commentsCount = 0; // 评论数
-
-
- public Long getDynamicId() {
- return dynamicId;
- }
-
- public void setDynamicId(Long dynamicId) {
- this.dynamicId = dynamicId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public String getImageUrl() {
- return imageUrl;
- }
-
- public void setImageUrl(String imageUrl) {
- this.imageUrl = imageUrl;
- }
-
- public Date getTime() {
- return time;
- }
-
- public void setTime(Date time) {
- this.time = time;
- }
-
- public int getLikesCount() {
- return likesCount;
- }
-
- public void setLikesCount(int likesCount) {
- this.likesCount = likesCount;
- }
-
- public int getCommentsCount() {
- return commentsCount;
- }
-
- public void setCommentsCount(int commentsCount) {
- this.commentsCount = commentsCount;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/UserFollow.java b/src/main/java/com/example/myproject/entity/UserFollow.java
deleted file mode 100644
index bfc14f7..0000000
--- a/src/main/java/com/example/myproject/entity/UserFollow.java
+++ /dev/null
@@ -1,56 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "follow")
-public class UserFollow {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "record_id")
- private Long recordId;
-
- @Column(name = "follower_id")
- private Long followerId;
-
- @Column(name = "followed_id")
- private Long followedId;
-
- @Column(name = "follow_time")
- private Date followTime;
-
- // Getters and Setters
- public Long getId() {
- return recordId;
- }
-
- public void setId(Long recordId) {
- this.recordId = recordId;
- }
-
- public Long getFollowerId() {
- return followerId;
- }
-
- public void setFollowerId(Long followerId) {
- this.followerId = followerId;
- }
-
- public Long getFollowedId() {
- return followedId;
- }
-
- public void setFollowedId(Long followedId) {
- this.followedId = followedId;
- }
-
- public Date getFollowTime() {
- return followTime;
- }
-
- public void setFollowTime(Date followTime) {
- this.followTime = followTime;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/UserInviteCode.java b/src/main/java/com/example/myproject/entity/UserInviteCode.java
deleted file mode 100644
index 8343c7e..0000000
--- a/src/main/java/com/example/myproject/entity/UserInviteCode.java
+++ /dev/null
@@ -1,67 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.time.LocalDateTime;
-
-@Entity
-@Table(name = "user_invite_code")
-public class UserInviteCode {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "invite_id")
- private Long inviteId;
-
- @Column(name = "user_id", nullable = false)
- private Long userId;
-
- @Column(name = "invite_code", nullable = false, unique = true)
- private String inviteCode;
-
- @Column(name = "is_used", nullable = false)
- private Boolean isUsed = false;
-
- @Column(name = "created_at", nullable = false)
- private LocalDateTime createdAt;
-
- // Getters and Setters
- public Long getInviteId() {
- return inviteId;
- }
-
- public void setInviteId(Long inviteId) {
- this.inviteId = inviteId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public String getInviteCode() {
- return inviteCode;
- }
-
- public void setInviteCode(String inviteCode) {
- this.inviteCode = inviteCode;
- }
-
- public Boolean getIsUsed() {
- return isUsed;
- }
-
- public void setIsUsed(Boolean isUsed) {
- this.isUsed = isUsed;
- }
-
- public LocalDateTime getCreatedAt() {
- return createdAt;
- }
-
- public void setCreatedAt(LocalDateTime createdAt) {
- this.createdAt = createdAt;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/UserMessages.java b/src/main/java/com/example/myproject/entity/UserMessages.java
deleted file mode 100644
index ee7e43e..0000000
--- a/src/main/java/com/example/myproject/entity/UserMessages.java
+++ /dev/null
@@ -1,67 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "user_messages")
-public class UserMessages {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "message_id")
- private Long messageId;
-
- @Column(name = "sender_id")
- private Long senderId;
-
- @Column(name = "receiver_id")
- private Long receiverId;
-
- @Column(name = "content")
- private String content;
-
- @Column(name = "time")
- private Date time;
-
-
- public Long getMessageId() {
- return messageId;
- }
-
- public void setMessageId(Long messageId) {
- this.messageId = messageId;
- }
-
- public Long getSenderId() {
- return senderId;
- }
-
- public void setSenderId(Long senderId) {
- this.senderId = senderId;
- }
-
- public Long getReceiverId() {
- return receiverId;
- }
-
- public void setReceiverId(Long receiverId) {
- this.receiverId = receiverId;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public Date getTime() {
- return time;
- }
-
- public void setTime(Date time) {
- this.time = time;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/UserTaskStatus.java b/src/main/java/com/example/myproject/entity/UserTaskStatus.java
deleted file mode 100644
index a66839d..0000000
--- a/src/main/java/com/example/myproject/entity/UserTaskStatus.java
+++ /dev/null
@@ -1,98 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-
-@Entity
-@Table(name = "user_task_status")
-public class UserTaskStatus {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "status_id")
- private Long statusId;
-
- @Column(name = "user_id", nullable = false)
- private Long userId;
-
- @Column(name = "task_id", nullable = false)
- private String taskId;
-
- @Column(name = "current_progress", nullable = false)
- private Float currentProgress;
-
- @Column(name = "current_experience", nullable = false)
- private Integer currentExperience;
-
- @Column(name = "current_points", nullable = false)
- private Integer currentPoints;
-
- @Column(name = "status", nullable = false)
- private String status;
-
- @Column(name = "is_reward_claimed", nullable = false)
- private Boolean isRewardClaimed = false;
-
- public Long getStatusId() {
- return statusId;
- }
-
- public void setStatusId(Long statusId) {
- this.statusId = statusId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public String getTaskId() {
- return taskId;
- }
-
- public void setTaskId(String taskId) {
- this.taskId = taskId;
- }
-
- public Float getCurrentProgress() {
- return currentProgress;
- }
-
- public void setCurrentProgress(Float currentProgress) {
- this.currentProgress = currentProgress;
- }
-
- public Integer getCurrentExperience() {
- return currentExperience;
- }
-
- public void setCurrentExperience(Integer currentExperience) {
- this.currentExperience = currentExperience;
- }
-
- public Integer getCurrentPoints() {
- return currentPoints;
- }
-
- public void setCurrentPoints(Integer currentPoints) {
- this.currentPoints = currentPoints;
- }
-
- public String getStatus() {
- return status;
- }
-
- public void setStatus(String status) {
- this.status = status;
- }
-
- public Boolean getIsRewardClaimed() {
- return isRewardClaimed;
- }
-
- public void setIsRewardClaimed(Boolean isRewardClaimed) {
- this.isRewardClaimed = isRewardClaimed;
- }
-}
diff --git a/src/main/java/com/example/myproject/entity/Users.java b/src/main/java/com/example/myproject/entity/Users.java
deleted file mode 100644
index ebe793f..0000000
--- a/src/main/java/com/example/myproject/entity/Users.java
+++ /dev/null
@@ -1,246 +0,0 @@
-package com.example.myproject.entity;
-
-import javax.persistence.*;
-import java.util.Date;
-
-@Entity
-@Table(name = "user")
-public class Users {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "user_id")
- private Long userId;
-
- @Column(name = "username", nullable = false,unique = true)
- private String username;
-
- @Column(name = "avatar_url", nullable = true)
- private String avatarUrl;
-
- @Column(name = "email", nullable = false)
- private String email;
-
- @Column(name = "password", nullable = false)
- private String password;
-
- @Column(name = "role", nullable = false)
- private String role;
-
- @Column(name = "invite_count")
- private Integer inviteCount;
-
- @Column(name = "level", nullable = false)
- private Long level;
-
- @Column(name = "upload_count")
- private Float uploadCount;
-
- @Column(name = "download_count")
- private Float downloadCount;
-
- @Column(name = "share_rate")
- private Float shareRate;
-
- @Column(name = "registration_date", nullable = false)
- private Date registrationDate;
-
- @Column(name = "last_login_time")
- private Date lastLoginTime;
-
- @Column(name = "user_points")
- private Integer userPoints;
-
- @Column(name = "is_promo")
- private Boolean isPromo;
-
- @Column(name = "current_experience", nullable = false)
- private Integer currentExperience; // 当前经验值
-
- @Column(name = "current_seeding_hours", nullable = false)
- private Float currentSeedingHours; // 当前做种时长
-
-
- @Column(name = "gender", nullable = true)
- private String gender; // 性别
-
- @Column(name = "description", nullable = true)
- private String description; // 个人描述
-
- @Column(name = "hobbies", nullable = true)
- private String hobbies;
-
- @Column(name = "registration_time", nullable = false, updatable = false)
- @Temporal(TemporalType.TIMESTAMP)
- private Date registrationTime;
-
- // Getters and Setters
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getAvatarUrl() {
- return avatarUrl;
- }
-
- public void setAvatarUrl(String avatarUrl) {
- this.avatarUrl = avatarUrl;
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public String getRole() {
- return role;
- }
-
- public void setRole(String role) {
- this.role = role;
- }
-
- public Integer getInviteCount() {
- return inviteCount;
- }
-
- public void setInviteCount(Integer inviteCount) {
- this.inviteCount = inviteCount;
- }
-
- public Long getLevel() {
- return level;
- }
-
- public void setLevel(Long level) {
- this.level = level;
- }
-
- public Float getUploadCount() {
- return uploadCount;
- }
-
- public void setUploadCount(Float uploadCount) {
- this.uploadCount = uploadCount;
- }
-
- public Float getDownloadCount() {
- return downloadCount;
- }
-
- public void setDownloadCount(Float downloadCount) {
- this.downloadCount = downloadCount;
- }
-
- public Float getShareRate() {
- return shareRate;
- }
-
- public void setShareRate(Float shareRate) {
- this.shareRate = shareRate;
- }
-
- public Date getRegistrationDate() {
- return registrationDate;
- }
-
- public void setRegistrationDate(Date registrationDate) {
- this.registrationDate = registrationDate;
- }
-
- public Date getLastLoginTime() {
- return lastLoginTime;
- }
-
- public void setLastLoginTime(Date lastLoginTime) {
- this.lastLoginTime = lastLoginTime;
- }
-
- public Integer getUserPoints() {
- return userPoints;
- }
-
- public void setUserPoints(Integer userPoints) {
- this.userPoints = userPoints;
- }
-
- public Boolean getIsPromo() {
- return isPromo;
- }
-
- public void setIsPromo(Boolean isPromo) {
- this.isPromo = isPromo;
- }
-
- public Integer getCurrentExperience() {
- return currentExperience;
- }
-
- public void setCurrentExperience(Integer currentExperience) {
- this.currentExperience = currentExperience;
- }
-
- public Float getCurrentSeedingHours() {
- return currentSeedingHours;
- }
-
- public void setCurrentSeedingHours(Float currentSeedingHours) {
- this.currentSeedingHours = currentSeedingHours;
- }
-
- public String getGender() {
- return gender;
- }
-
- public void setGender(String gender) {
- this.gender = gender;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public String getHobbies() {
- return hobbies;
- }
-
- public void setHobbies(String hobbies) {
- this.hobbies = hobbies;
- }
-
- public Date getRegistrationTime() {
- return registrationTime;
- }
-
- public void setRegistrationTime(Date registrationTime) {
- this.registrationTime = registrationTime;
- }
-
-}
diff --git a/src/main/java/com/example/myproject/entity/VerificationToken.java b/src/main/java/com/example/myproject/entity/VerificationToken.java
new file mode 100644
index 0000000..231fd95
--- /dev/null
+++ b/src/main/java/com/example/myproject/entity/VerificationToken.java
@@ -0,0 +1,58 @@
+package com.example.myproject.entity;
+
+import com.baomidou.mybatisplus.annotation.*;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.AllArgsConstructor;
+
+import java.time.Instant;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@TableName("verification_token")
+@ApiModel("验证令牌实体类")
+public class VerificationToken {
+
+ @TableId(type = IdType.AUTO)
+ @ApiModelProperty(value = "令牌ID")
+ private Long id;
+
+ @ApiModelProperty(value = "令牌字符串")
+ @TableField("token")
+ private String token;
+
+ @ApiModelProperty(value = "令牌过期日期")
+ @TableField("expiry_date")
+ private Instant expiryDate;
+
+ @ApiModelProperty(value = "用户名")
+ @TableField("username")
+ private String username;
+
+ @ApiModelProperty(value = "电子邮件地址")
+ @TableField("email")
+ private String email;
+
+ @ApiModelProperty(value = "加密后的密码")
+ @TableField("password")
+ private String password;
+
+ public VerificationToken(String token, String username, String email, String password, Instant expiryDate) {
+ this.token = token;
+ this.username = username;
+ this.email = email;
+ this.password = password;
+ this.expiryDate = expiryDate;
+ }
+
+ /**
+ * 检查令牌是否过期
+ * @return true 如果令牌已过期
+ */
+ public boolean isExpired() {
+ return expiryDate.isBefore(Instant.now());
+ }
+}
diff --git a/src/main/java/com/example/myproject/mapper/FavoriteMapper.java b/src/main/java/com/example/myproject/mapper/FavoriteMapper.java
new file mode 100644
index 0000000..08b8151
--- /dev/null
+++ b/src/main/java/com/example/myproject/mapper/FavoriteMapper.java
@@ -0,0 +1,15 @@
+package com.example.myproject.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.myproject.entity.FavoriteEntity;
+import org.apache.ibatis.annotations.*;
+
+@Mapper
+public interface FavoriteMapper extends BaseMapper<FavoriteEntity> {
+
+ @Select("SELECT * FROM favorite WHERE user_id = #{userId} AND seed_id = #{seedId} LIMIT 1")
+ FavoriteEntity selectByUserIdAndSeedId(@Param("userId") Long userId, @Param("seedId") Long seedId);
+
+ @Delete("DELETE FROM favorite WHERE user_id = #{userId} AND seed_id = #{seedId}")
+ void deleteByUserIdAndSeedId(@Param("userId") Long userId, @Param("seedId") Long seedId);
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/mapper/PromotionMapper.java b/src/main/java/com/example/myproject/mapper/PromotionMapper.java
new file mode 100644
index 0000000..a478835
--- /dev/null
+++ b/src/main/java/com/example/myproject/mapper/PromotionMapper.java
@@ -0,0 +1,52 @@
+package com.example.myproject.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.myproject.entity.Promotion;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+
+import java.time.LocalDateTime;
+import java.util.List;
+
+@Mapper
+public interface PromotionMapper extends BaseMapper<Promotion> {
+
+ @Select("SELECT * FROM promotion WHERE is_deleted = false " +
+ "AND start_time <= #{now} AND end_time >= #{now}")
+ List<Promotion> findActivePromotions(@Param("now") LocalDateTime now);
+
+ /**
+ * 查找某个torrentId是否在促销活动的applicable_torrent_ids字符串中
+ * 用MySQL的FIND_IN_SET判断
+ */
+ @Select("SELECT p.* FROM promotion p " +
+ "WHERE p.is_deleted = false " +
+ "AND p.start_time <= #{now} AND p.end_time >= #{now} " +
+ "AND FIND_IN_SET(#{torrentId}, p.applicable_torrent_ids) > 0")
+ List<Promotion> findActivePromotionsForTorrent(
+ @Param("torrentId") Long torrentId,
+ @Param("now") LocalDateTime now);
+
+ /**
+ * 校验种子id是否存在,返回计数,0代表不存在,>0代表存在
+ */
+ @Select("SELECT COUNT(*) FROM torrent WHERE id = #{torrentId}")
+ int checkTorrentExists(@Param("torrentId") Long torrentId);
+
+ /**
+ * 插入促销活动
+ */
+// int insert(Promotion promotion);
+
+ /**
+ * 根据ID更新促销活动(例如软删除)
+ */
+ int updateById(Promotion promotion);
+
+ /**
+ * 根据ID查询促销活动
+ */
+ Promotion selectById(@Param("id") Long id);
+}
diff --git a/src/main/java/com/example/myproject/mapper/TorrentMapper.java b/src/main/java/com/example/myproject/mapper/TorrentMapper.java
new file mode 100644
index 0000000..f9cdcdc
--- /dev/null
+++ b/src/main/java/com/example/myproject/mapper/TorrentMapper.java
@@ -0,0 +1,32 @@
+package com.example.myproject.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.myproject.dto.param.TorrentParam;
+import com.example.myproject.entity.TorrentEntity;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+
+import java.util.List;
+
+@Mapper
+public interface TorrentMapper extends BaseMapper<TorrentEntity> {
+
+ @Select("SELECT * FROM torrents WHERE info_hash = #{infoHash}")
+ TorrentEntity selectByInfoHash(String infoHash);
+
+ @Select("SELECT * FROM torrents WHERE seed_id = #{seedId}")
+ TorrentEntity selectBySeedId(Long seedId);
+
+ List<TorrentEntity> search(@Param("param") TorrentParam param);
+
+ @Update("UPDATE torrent SET downloads = downloads + 1 WHERE id = #{torrentId}")
+ void increaseDownloads(@Param("torrentId") Long torrentId);
+
+ boolean checkFavorite(@Param("seedId") Long seedId, @Param("userId") Long userId);
+
+ void addFavorite(@Param("seedId") Long seedId, @Param("userId") Long userId);
+
+ void removeFavorite(@Param("seedId") Long seedId, @Param("userId") Long userId);
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/mapper/UserMapper.java b/src/main/java/com/example/myproject/mapper/UserMapper.java
new file mode 100644
index 0000000..3a29b64
--- /dev/null
+++ b/src/main/java/com/example/myproject/mapper/UserMapper.java
@@ -0,0 +1,31 @@
+package com.example.myproject.mapper;
+import com.example.myproject.entity.User;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+import org.springframework.data.repository.query.Param;
+
+import java.util.List;
+
+@Mapper
+public interface UserMapper extends BaseMapper<User> {
+
+
+ User selectByUsername(@Param("username") String username);
+
+ User selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
+
+ User selectByEmail(@Param("email") String email);
+
+
+ // 根据用户名包含查找用户
+ List<User> selectByUsernameContaining(@Param("name") String name);
+
+ @Update("UPDATE user SET downloaded = downloaded + #{size} WHERE id = #{userId}")
+ void increaseDownloaded(@Param("userId") Long userId, @Param("size") double size);
+
+ boolean hasRole(@Param("userId") Long userId, @Param("role") String role);
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/mapper/VerificationTokenMapper.java b/src/main/java/com/example/myproject/mapper/VerificationTokenMapper.java
new file mode 100644
index 0000000..94a25ca
--- /dev/null
+++ b/src/main/java/com/example/myproject/mapper/VerificationTokenMapper.java
@@ -0,0 +1,13 @@
+package com.example.myproject.mapper;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.myproject.entity.VerificationToken;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+
+
+@Mapper
+public interface VerificationTokenMapper extends BaseMapper<VerificationToken> {
+
+ VerificationToken findByTokenAndEmail(String token, String email);
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/repository/CollectionsRepository.java b/src/main/java/com/example/myproject/repository/CollectionsRepository.java
deleted file mode 100644
index 300f233..0000000
--- a/src/main/java/com/example/myproject/repository/CollectionsRepository.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.Collections;
-import jakarta.transaction.Transactional;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.data.jpa.repository.Modifying;
-import org.springframework.data.jpa.repository.Query;
-import org.springframework.stereotype.Repository;
-
-import java.util.List;
-
-@Repository
-public interface CollectionsRepository extends JpaRepository<Collections, Long> {
- @Modifying
- @Transactional
- @Query("DELETE FROM Collections l WHERE l.userId = :userId AND l.postNo = :postNo")
- void deleteLikeByUserIdAndPostNo(Long userId, Long postNo);
- List<Collections> findByUserId(Long userId);
-}
diff --git a/src/main/java/com/example/myproject/repository/CommentRepository.java b/src/main/java/com/example/myproject/repository/CommentRepository.java
deleted file mode 100644
index 4f66309..0000000
--- a/src/main/java/com/example/myproject/repository/CommentRepository.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.Comments;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-
-public interface CommentRepository extends JpaRepository<Comments, Long> {
-
-}
diff --git a/src/main/java/com/example/myproject/repository/DynamicCommentRepository.java b/src/main/java/com/example/myproject/repository/DynamicCommentRepository.java
deleted file mode 100644
index f586b1d..0000000
--- a/src/main/java/com/example/myproject/repository/DynamicCommentRepository.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.DynamicComment;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.List;
-
-public interface DynamicCommentRepository extends JpaRepository<DynamicComment, Long> {
-
- List<DynamicComment> findByDynamicId(Long dynamicId);
-}
diff --git a/src/main/java/com/example/myproject/repository/DynamicLikesRepository.java b/src/main/java/com/example/myproject/repository/DynamicLikesRepository.java
deleted file mode 100644
index 1cec003..0000000
--- a/src/main/java/com/example/myproject/repository/DynamicLikesRepository.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.DynamicLikes;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.List;
-
-public interface DynamicLikesRepository extends JpaRepository<DynamicLikes, Long> {
-
- List<DynamicLikes> findByDynamicId(Long dynamicId);
- DynamicLikes findByUserIdAndDynamicId(Long userId, Long dynamicId);
-}
diff --git a/src/main/java/com/example/myproject/repository/ExperienceHistoryRepository.java b/src/main/java/com/example/myproject/repository/ExperienceHistoryRepository.java
deleted file mode 100644
index 76928e5..0000000
--- a/src/main/java/com/example/myproject/repository/ExperienceHistoryRepository.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.ExperienceHistory;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-public interface ExperienceHistoryRepository extends JpaRepository<ExperienceHistory, Long> {
-
-}
diff --git a/src/main/java/com/example/myproject/repository/FriendRelationRepository.java b/src/main/java/com/example/myproject/repository/FriendRelationRepository.java
deleted file mode 100644
index 8fcf94e..0000000
--- a/src/main/java/com/example/myproject/repository/FriendRelationRepository.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.FriendRelation;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.List;
-
-public interface FriendRelationRepository extends JpaRepository<FriendRelation, Long> {
- // 查找当前用户的所有好友ID
- List<FriendRelation> findByUserId(Long userId);
-}
diff --git a/src/main/java/com/example/myproject/repository/GroupCommentsRepository.java b/src/main/java/com/example/myproject/repository/GroupCommentsRepository.java
deleted file mode 100644
index 47df198..0000000
--- a/src/main/java/com/example/myproject/repository/GroupCommentsRepository.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.GroupComments;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.stereotype.Repository;
-
-@Repository
-public interface GroupCommentsRepository extends JpaRepository<GroupComments, Long> {
-
-}
diff --git a/src/main/java/com/example/myproject/repository/GroupMembersRepository.java b/src/main/java/com/example/myproject/repository/GroupMembersRepository.java
deleted file mode 100644
index 2d31462..0000000
--- a/src/main/java/com/example/myproject/repository/GroupMembersRepository.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.GroupMembers;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.stereotype.Repository;
-
-import java.util.List;
-
-@Repository
-public interface GroupMembersRepository extends JpaRepository<GroupMembers, Long> {
-
- // 检查用户是否已经是该小组的成员
- boolean existsByGroupIdAndUserId(Long groupId, Long userId);
-
- // 删除用户和小组的关联记录
- void deleteByGroupIdAndUserId(Long groupId, Long userId);
-
- // 通过 groupId 获取所有成员的 userId
- List<GroupMembers> findByGroupId(Long groupId);
-}
diff --git a/src/main/java/com/example/myproject/repository/GroupPostRepository.java b/src/main/java/com/example/myproject/repository/GroupPostRepository.java
deleted file mode 100644
index 9f5a348..0000000
--- a/src/main/java/com/example/myproject/repository/GroupPostRepository.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.GroupPost;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.stereotype.Repository;
-
-import java.util.List;
-
-@Repository
-public interface GroupPostRepository extends JpaRepository<GroupPost, Long> {
-
- List<GroupPost> findByGroupId(Long groupId);
-
-}
diff --git a/src/main/java/com/example/myproject/repository/GroupRepository.java b/src/main/java/com/example/myproject/repository/GroupRepository.java
deleted file mode 100644
index 7280e50..0000000
--- a/src/main/java/com/example/myproject/repository/GroupRepository.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.Group;
-import org.springframework.data.domain.Page;
-import org.springframework.data.domain.Pageable;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.data.jpa.repository.Query;
-import org.springframework.data.repository.query.Param;
-import org.springframework.stereotype.Repository;
-
-@Repository
-public interface GroupRepository extends JpaRepository<Group, Long> {
-
- // 模糊搜索小组名称并按类别筛选
- @Query("SELECT g FROM Group g WHERE g.groupName LIKE %:name% AND (:category IS NULL OR g.category = :category)")
- Page<Group> searchGroups(@Param("name") String name, @Param("category") String category, Pageable pageable);
-
- // 根据类别筛选小组
- @Query("SELECT g FROM Group g WHERE (:category IS NULL OR g.category = :category)")
- Page<Group> searchWithFilters(@Param("category") String category, Pageable pageable);
-}
diff --git a/src/main/java/com/example/myproject/repository/LevelRepository.java b/src/main/java/com/example/myproject/repository/LevelRepository.java
deleted file mode 100644
index 12e8269..0000000
--- a/src/main/java/com/example/myproject/repository/LevelRepository.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.Level;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.stereotype.Repository;
-
-@Repository
-public interface LevelRepository extends JpaRepository<Level, Long> {
-
-}
diff --git a/src/main/java/com/example/myproject/repository/LikesRepository.java b/src/main/java/com/example/myproject/repository/LikesRepository.java
deleted file mode 100644
index 79146c5..0000000
--- a/src/main/java/com/example/myproject/repository/LikesRepository.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.Likes;
-import jakarta.transaction.Transactional;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.data.jpa.repository.Modifying;
-import org.springframework.data.jpa.repository.Query;
-import org.springframework.stereotype.Repository;
-
-@Repository
-public interface LikesRepository extends JpaRepository<Likes, Long> {
- @Modifying
- @Transactional
- @Query("DELETE FROM Likes l WHERE l.userId = :userId AND l.postNo = :postNo")
- void deleteLikeByUserIdAndPostNo(Long userId, Long postNo);
-}
diff --git a/src/main/java/com/example/myproject/repository/PostRepository.java b/src/main/java/com/example/myproject/repository/PostRepository.java
deleted file mode 100644
index faf2cb7..0000000
--- a/src/main/java/com/example/myproject/repository/PostRepository.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.Post;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.Optional;
-
-public interface PostRepository extends JpaRepository<Post, Long> {
- // 根据 postNo 查找帖子
- Optional<Post> findByPostNo(Long postNo);
-
-}
diff --git a/src/main/java/com/example/myproject/repository/SeedCommentLikesRepository.java b/src/main/java/com/example/myproject/repository/SeedCommentLikesRepository.java
deleted file mode 100644
index 5c3d1bf..0000000
--- a/src/main/java/com/example/myproject/repository/SeedCommentLikesRepository.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.SeedCommentLikes;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.Optional;
-
-public interface SeedCommentLikesRepository extends JpaRepository<SeedCommentLikes, Long> {
-
- // 根据评论ID和用户ID查找点赞记录
- Optional<SeedCommentLikes> findByCommentIdAndUserId(Long commentId, Long userId);
-}
diff --git a/src/main/java/com/example/myproject/repository/SeedCommentRepository.java b/src/main/java/com/example/myproject/repository/SeedCommentRepository.java
deleted file mode 100644
index d8781cf..0000000
--- a/src/main/java/com/example/myproject/repository/SeedCommentRepository.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.SeedComment;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.stereotype.Repository;
-
-import java.util.List;
-
-@Repository
-public interface SeedCommentRepository extends JpaRepository<SeedComment, Long> {
-
- // 根据种子ID获取所有评论
- List<SeedComment> findBySeedId(int seedId);
-
- // 根据父评论ID获取所有回复
- List<SeedComment> findByComCommentId(long parentCommentId);
-}
diff --git a/src/main/java/com/example/myproject/repository/TaskRepository.java b/src/main/java/com/example/myproject/repository/TaskRepository.java
deleted file mode 100644
index 25e1b66..0000000
--- a/src/main/java/com/example/myproject/repository/TaskRepository.java
+++ /dev/null
@@ -1,7 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.Task;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-public interface TaskRepository extends JpaRepository<Task, String> {
-}
diff --git a/src/main/java/com/example/myproject/repository/UserDynamicRepository.java b/src/main/java/com/example/myproject/repository/UserDynamicRepository.java
deleted file mode 100644
index 575ae1f..0000000
--- a/src/main/java/com/example/myproject/repository/UserDynamicRepository.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.UserDynamic;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.stereotype.Repository;
-
-import java.util.List;
-
-@Repository
-public interface UserDynamicRepository extends JpaRepository<UserDynamic, Long> {
-
- List<UserDynamic> findByUserId(Long userId);
-
-}
diff --git a/src/main/java/com/example/myproject/repository/UserFollowRepository.java b/src/main/java/com/example/myproject/repository/UserFollowRepository.java
deleted file mode 100644
index 542d07f..0000000
--- a/src/main/java/com/example/myproject/repository/UserFollowRepository.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.UserFollow;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.stereotype.Repository;
-
-import java.util.List;
-
-@Repository
-public interface UserFollowRepository extends JpaRepository<UserFollow, Long> {
-
- // 查询是否存在某个用户的关注记录
- UserFollow findByFollowerIdAndFollowedId(Long followerId, Long followedId);
- List<UserFollow> findByFollowedId(Long followedId);
- List<UserFollow> findByFollowerId(Long followerId);
-}
diff --git a/src/main/java/com/example/myproject/repository/UserInviteCodeRepository.java b/src/main/java/com/example/myproject/repository/UserInviteCodeRepository.java
deleted file mode 100644
index 02fa390..0000000
--- a/src/main/java/com/example/myproject/repository/UserInviteCodeRepository.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.UserInviteCode;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.List;
-import java.util.Optional;
-
-public interface UserInviteCodeRepository extends JpaRepository<UserInviteCode, Long> {
-
- // 查找某个用户的所有邀请码
- List<UserInviteCode> findByUserId(Long userId);
-
- // 查找某个邀请码是否已经被使用
- Optional<UserInviteCode> findByInviteCode(String inviteCode);
-
- // 查找某个用户未使用的邀请码
- List<UserInviteCode> findByUserIdAndIsUsedFalse(Long userId);
-}
diff --git a/src/main/java/com/example/myproject/repository/UserMessagesRepository.java b/src/main/java/com/example/myproject/repository/UserMessagesRepository.java
deleted file mode 100644
index bad3715..0000000
--- a/src/main/java/com/example/myproject/repository/UserMessagesRepository.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.UserMessages;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.stereotype.Repository;
-
-import java.util.List;
-
-@Repository
-public interface UserMessagesRepository extends JpaRepository<UserMessages, Long> {
-
- // 查询当前用户的所有消息
- List<UserMessages> findBySenderIdOrReceiverId(Long senderId, Long receiverId);
-}
diff --git a/src/main/java/com/example/myproject/repository/UserRepository.java b/src/main/java/com/example/myproject/repository/UserRepository.java
deleted file mode 100644
index 6927c5f..0000000
--- a/src/main/java/com/example/myproject/repository/UserRepository.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.Users;
-import org.springframework.data.jpa.repository.JpaRepository;
-import java.util.Optional;
-
-public interface UserRepository extends JpaRepository<Users, Long> {
- Optional<Users> findByEmail(String email);
- Optional<Users> findByUsername(String username);
-}
diff --git a/src/main/java/com/example/myproject/repository/UserTaskStatusRepository.java b/src/main/java/com/example/myproject/repository/UserTaskStatusRepository.java
deleted file mode 100644
index 0153946..0000000
--- a/src/main/java/com/example/myproject/repository/UserTaskStatusRepository.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.example.myproject.repository;
-
-import com.example.myproject.entity.UserTaskStatus;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.List;
-import java.util.Optional;
-
-public interface UserTaskStatusRepository extends JpaRepository<UserTaskStatus, Long> {
- Optional<UserTaskStatus> findByUserIdAndTaskId(Long userId, String taskId);
- List<UserTaskStatus> findByUserIdAndStatus(Long userId, String status);
- Optional<UserTaskStatus> findFirstByUserIdAndStatus(Long userId, String status);
-}
diff --git a/src/main/java/com/example/myproject/service/CommentService.java b/src/main/java/com/example/myproject/service/CommentService.java
deleted file mode 100644
index c102ca2..0000000
--- a/src/main/java/com/example/myproject/service/CommentService.java
+++ /dev/null
@@ -1,125 +0,0 @@
-package com.example.myproject.service;
-
-import com.example.myproject.entity.Comments;
-import com.example.myproject.entity.Post;
-import com.example.myproject.entity.Users;
-import com.example.myproject.repository.CommentRepository;
-import com.example.myproject.repository.PostRepository;
-import com.example.myproject.repository.UserRepository;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-@Service
-public class CommentService {
-
- @Autowired
- private CommentRepository commentRepository;
-
- @Autowired
- private PostRepository postRepository;
-
- @Autowired
- private UserRepository userRepository;
-
- // 添加评论
- public void addComment(Long postId, Comments comment) {
- try {
- // 查找帖子
- Post post = postRepository.findById(postId).orElseThrow(() -> {
- throw new RuntimeException("Post with ID " + postId + " not found");
- });
-
- // 查找用户
- Users user = userRepository.findById(comment.getUserId()).orElseThrow(() -> {
- throw new RuntimeException("User with ID " + comment.getUserId() + " not found");
- });
-
- // 设置评论的相关属性
- comment.setPost(post);
- comment.setUserId(user.getUserId());
- comment.setCommentTime(new java.util.Date());
-
- if (comment.getParentComment() != null && comment.getParentComment() != 0) {
- // 查找父评论
- Comments parentComment = commentRepository.findById(comment.getParentComment())
- .orElseThrow(() -> new RuntimeException("Parent comment with ID " + comment.getParentComment() + " not found"));
- comment.setParentComment(parentComment.getCommentId()); // 设置父评论 ID
- } else {
- comment.setParentComment(0L); // 设置为 0,表示没有父评论
- }
-
- // 保存评论
- commentRepository.save(comment);
- } catch (Exception e) {
- // 捕获任何异常并打印堆栈信息
- e.printStackTrace();
- throw new RuntimeException("An error occurred while adding the comment: " + e.getMessage());
- }
- }
-
- // 获取指定帖子的所有评论
- public List<Map<String, Object>> getCommentsByPostId(Long postId) {
- List<Comments> comments = commentRepository.findAll()
- .stream()
- .filter(comment -> comment.getPost().getPostNo().equals(postId)) // 通过 post 的 postNo 过滤评论
- .collect(Collectors.toList());
-
- if (comments.isEmpty()) {
- return new ArrayList<>();
- }
-
- List<Map<String, Object>> result = new ArrayList<>();
- for (Comments comment : comments) {
- Map<String, Object> commentData = new LinkedHashMap<>();
- commentData.put("commentId", comment.getCommentId());
- commentData.put("post_id", comment.getPost().getPostNo());
- commentData.put("userId", comment.getUserId());
-
- Users user = userRepository.findById(comment.getUserId()).orElse(null);
- commentData.put("nickname", user != null ? user.getUsername() : null);
- commentData.put("avatar_url", user != null ? user.getAvatarUrl() : null);
-
- commentData.put("content", comment.getContent());
- commentData.put("isAnonymous", comment.getIsAnonymous());
- commentData.put("likesCount", comment.getLikesCount());
- commentData.put("replyCount", comment.getReplyCount());
- commentData.put("commentTime", comment.getCommentTime());
- commentData.put("parentComment", comment.getParentComment());
-
- Map<String, Object> parentCommentData = getParentCommentData(comment.getParentComment());
- commentData.put("parentCommentData", parentCommentData);
-
- result.add(commentData);
- }
-
- return result;
- }
-
- // 递归查找父评论的内容
- private Map<String, Object> getParentCommentData(Long parentCommentId) {
- Map<String, Object> parentCommentData = new LinkedHashMap<>();
- if (parentCommentId != null && parentCommentId != 0) {
- // 查找父评论
- Comments parentComment = commentRepository.findById(parentCommentId).orElse(null);
- if (parentComment != null) {
- parentCommentData.put("commentId", parentComment.getCommentId());
- parentCommentData.put("content", parentComment.getContent());
- parentCommentData.put("userId", parentComment.getUserId());
-
- Users user = userRepository.findById(parentComment.getUserId()).orElse(null);
- parentCommentData.put("nickname", user != null ? user.getUsername() : null);
- parentCommentData.put("avatar_url", user != null ? user.getAvatarUrl() : null);
-
- Map<String, Object> grandParentData = getParentCommentData(parentComment.getParentComment());
- parentCommentData.put("parentCommentData", grandParentData);
- }
- }
- return parentCommentData;
- }
-}
diff --git a/src/main/java/com/example/myproject/service/DynamicService.java b/src/main/java/com/example/myproject/service/DynamicService.java
deleted file mode 100644
index 853604d..0000000
--- a/src/main/java/com/example/myproject/service/DynamicService.java
+++ /dev/null
@@ -1,275 +0,0 @@
-package com.example.myproject.service;
-
-import com.example.myproject.entity.*;
-import com.example.myproject.repository.*;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.*;
-
-@Service
-public class DynamicService {
-
- @Autowired
- private UserDynamicRepository userDynamicRepository;
-
- @Autowired
- private UserRepository userRepository;
-
- @Autowired
- private DynamicCommentRepository dynamicCommentRepository;
-
- @Autowired
- private DynamicLikesRepository dynamicLikesRepository;
-
- @Autowired
- private FriendRelationRepository friendRelationRepository;
-
- private static final String IMAGE_DIR = "uploads/post/";
-
- // 创建好友动态
- public Map<String, Object> createDynamic(Long userId, String title, String content, MultipartFile[] imageFiles) {
- // 查找用户信息
- Users user = userRepository.findById(userId)
- .orElseThrow(() -> new RuntimeException("User not found"));
-
- // 创建新的动态
- UserDynamic userDynamic = new UserDynamic();
- userDynamic.setUserId(userId);
- userDynamic.setTitle(title != null ? title : "默认标题");
- userDynamic.setContent(content);
- userDynamic.setTime(new Date());
- userDynamic.setLikesCount(0);
- userDynamic.setCommentsCount(0);
-
- // 处理图片上传
- StringBuilder imageUrlsBuilder = new StringBuilder();
- if (imageFiles != null && imageFiles.length > 0) {
- for (int i = 0; i < imageFiles.length; i++) {
- if (i > 0) {
- imageUrlsBuilder.append(",");
- }
- try {
- String imageUrl = saveImage(imageFiles[i]);
- imageUrlsBuilder.append(imageUrl);
- } catch (IOException e) {
- throw new RuntimeException("Image upload failed: " + e.getMessage());
- }
- }
- }
-
- // 将多个图片 URL 拼接
- userDynamic.setImageUrl(imageUrlsBuilder.toString());
-
- // 保存数据库
- UserDynamic savedUserDynamic = userDynamicRepository.save(userDynamic);
-
- // 返回结果
- Map<String, Object> response = new HashMap<>();
- response.put("dynamicId", savedUserDynamic.getDynamicId());
- response.put("message", "动态创建成功");
- return response;
- }
-
- // 保存图片并返回图片的 URL
- public String saveImage(MultipartFile imageFile) throws IOException {
- String fileName = imageFile.getOriginalFilename();
- Path path = Paths.get(IMAGE_DIR + fileName);
- Files.createDirectories(path.getParent());
- Files.write(path, imageFile.getBytes());
- return "/images/" + fileName;
- }
-
- // 删除动态
- public void deleteDynamic(Long dynamicId) {
- if (userDynamicRepository.existsById(dynamicId)) {
- userDynamicRepository.deleteById(dynamicId);
- } else {
- throw new RuntimeException("Dynamic with id " + dynamicId + " not found");
- }
- }
-
- //好友动态评论
- public Map<String, Object> addComment(Long userId, Long dynamicId, String commentContent) {
- Users user = userRepository.findById(userId)
- .orElseThrow(() -> new RuntimeException("User not found"));
-
- UserDynamic dynamic = userDynamicRepository.findById(dynamicId)
- .orElseThrow(() -> new RuntimeException("Dynamic not found"));
-
- DynamicComment comment = new DynamicComment();
- comment.setDynamicId(dynamicId);
- comment.setUserId(userId);
- comment.setCommentContent(commentContent);
- comment.setCommentTime(new Date());
-
- // 保存评论
- DynamicComment savedComment = dynamicCommentRepository.save(comment);
-
- // 返回成功消息
- Map<String, Object> response = new HashMap<>();
- response.put("comment_id", savedComment.getCommentId());
- response.put("message", "评论成功");
- return response;
- }
-
- //返回某个好友的所有动态
- public Map<String, Object> getAllUserDynamics(Long userId) {
- // 查找用户的所有动态
- List<UserDynamic> userDynamics = userDynamicRepository.findByUserId(userId);
- Map<String, Object> postData = new LinkedHashMap<>();
- List<Map<String, Object>> dynamicList = new ArrayList<>();
-
- // 遍历动态,获取点赞和评论
- for (UserDynamic dynamic : userDynamics) {
- Map<String, Object> dynamicData = new LinkedHashMap<>();
- dynamicData.put("dynamic_id", dynamic.getDynamicId());
- dynamicData.put("user_id", dynamic.getUserId());
-
- Users user = userRepository.findById(dynamic.getUserId()).orElse(null);
- if (user != null) {
- dynamicData.put("username", user.getUsername());
- dynamicData.put("avatar_url", user.getAvatarUrl());
- } else {
- dynamicData.put("username", "Unknown");
- dynamicData.put("avatar_url", "http://example.com/default-avatar.jpg");
- }
-
- dynamicData.put("title", dynamic.getTitle());
- dynamicData.put("content", dynamic.getContent());
- dynamicData.put("images", dynamic.getImageUrl());
- dynamicData.put("time", dynamic.getTime());
-
- // 获取点赞
- List<DynamicLikes> likes = dynamicLikesRepository.findByDynamicId(dynamic.getDynamicId());
- List<Map<String, Object>> likeList = new ArrayList<>();
- for (DynamicLikes like : likes) {
- Map<String, Object> likeData = new HashMap<>();
- likeData.put("user_id", like.getUserId());
-
- // 获取点赞用户的用户名
- Users likeUser = userRepository.findById(like.getUserId()).orElse(null);
- if (likeUser != null) {
- likeData.put("username", likeUser.getUsername());
- } else {
- likeData.put("username", "Unknown");
- }
-
- likeList.add(likeData);
- }
- dynamicData.put("likes", likeList);
-
- // 获取评论
- List<DynamicComment> comments = dynamicCommentRepository.findByDynamicId(dynamic.getDynamicId());
- List<Map<String, Object>> commentList = new ArrayList<>();
- for (DynamicComment comment : comments) {
- Map<String, Object> commentData = new HashMap<>();
- commentData.put("comment_id", comment.getCommentId());
- commentData.put("user_id", comment.getUserId());
-
- // 获取评论用户的用户名
- Users commentUser = userRepository.findById(comment.getUserId()).orElse(null);
- if (commentUser != null) {
- commentData.put("username", commentUser.getUsername());
- } else {
- commentData.put("username", "Unknown");
- }
-
- commentData.put("content", comment.getCommentContent());
- commentData.put("time", comment.getCommentTime());
- commentList.add(commentData);
- }
- dynamicData.put("comments", commentList);
-
- dynamicList.add(dynamicData);
- }
-
- Map<String, Object> response = new HashMap<>();
- response.put("dynamic", dynamicList);
- return response;
- }
-
- //获取当前所有好友的id
- public List<Long> getAllFriendIds(Long userId) {
- List<FriendRelation> friendRelations = friendRelationRepository.findByUserId(userId);
- List<Long> friendIds = new ArrayList<>();
-
- for (FriendRelation relation : friendRelations) {
- // 添加好友ID
- if (!friendIds.contains(relation.getFriendId())) {
- friendIds.add(relation.getFriendId());
- }
- }
-
- return friendIds;
- }
-
- public Map<String, Object> getAllUserAndFriendsDynamics(List<Long> userIds) {
- List<Map<String, Object>> allDynamics = new ArrayList<>();
-
- // 遍历所有用户获取他们的动态
- for (Long userId : userIds) {
- Map<String, Object> userDynamics = getAllUserDynamics(userId);
- if (userDynamics != null && !userDynamics.isEmpty()) {
- allDynamics.addAll((List<Map<String, Object>>) userDynamics.get("dynamic"));
- }
- }
-
- // 返回所有动态的合集
- Map<String, Object> response = new HashMap<>();
- response.put("dynamic", allDynamics);
- return response;
- }
-
- //点赞动态
- public boolean likeDynamic(Long userId, Long dynamicId) {
- // 检查该用户是否已经点赞过该动态
- DynamicLikes existingLike = dynamicLikesRepository.findByUserIdAndDynamicId(userId, dynamicId);
- if (existingLike != null) {
- return false;
- }
-
- // 新增点赞记录
- DynamicLikes newLike = new DynamicLikes();
- newLike.setUserId(userId);
- newLike.setDynamicId(dynamicId);
- newLike.setLikeTime(new java.util.Date());
- dynamicLikesRepository.save(newLike);
-
- // 更新动态表的点赞数
- UserDynamic dynamic = userDynamicRepository.findById(dynamicId).orElse(null);
- if (dynamic != null) {
- dynamic.setLikesCount(dynamic.getLikesCount() + 1);
- userDynamicRepository.save(dynamic);
- }
-
- return true;
- }
-
- //取消点赞
- public boolean unlikeDynamic(Long userId, Long dynamicId) {
- // 查找该用户的点赞记录
- DynamicLikes existingLike = dynamicLikesRepository.findByUserIdAndDynamicId(userId, dynamicId);
- if (existingLike == null) {
- return false;
- }
-
- // 删除点赞记录
- dynamicLikesRepository.delete(existingLike);
-
- // 更新动态表的点赞数
- UserDynamic dynamic = userDynamicRepository.findById(dynamicId).orElse(null);
- if (dynamic != null) {
- dynamic.setLikesCount(dynamic.getLikesCount() - 1);
- userDynamicRepository.save(dynamic);
- }
-
- return true;
- }
-
-}
diff --git a/src/main/java/com/example/myproject/service/EmailService.java b/src/main/java/com/example/myproject/service/EmailService.java
new file mode 100644
index 0000000..1ecc8c4
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/EmailService.java
@@ -0,0 +1,20 @@
+
+package com.example.myproject.service;
+public interface EmailService {
+ /**
+ * 发送邮件给指定的邮箱。
+ *
+ * @param to 收件人邮箱地址
+ * @param subject 邮件主题
+ * @param text 邮件内容
+ * @return 发送是否成功
+ */
+ boolean sendEmail(String to, String subject, String text);
+ /**
+ * 生成并发送验证邮件
+ *
+ * @param email 收件人邮箱
+ * @param token
+ */
+ void sendVerificationEmail(String email, String token);
+}
diff --git a/src/main/java/com/example/myproject/service/GroupService.java b/src/main/java/com/example/myproject/service/GroupService.java
deleted file mode 100644
index 016d7e0..0000000
--- a/src/main/java/com/example/myproject/service/GroupService.java
+++ /dev/null
@@ -1,382 +0,0 @@
-package com.example.myproject.service;
-
-import com.example.myproject.entity.*;
-
-import com.example.myproject.repository.*;
-import jakarta.transaction.Transactional;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Page;
-import org.springframework.data.domain.PageRequest;
-import org.springframework.data.domain.Pageable;
-import org.springframework.data.domain.Sort;
-import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
-import java.io.IOException;
-import java.nio.file.Path;
-import java.nio.file.Files;
-
-import java.util.*;
-
-@Service
-public class GroupService {
-
- @Autowired
- private GroupRepository groupRepository;
-
- @Autowired
- private GroupMembersRepository groupMembersRepository;
-
-
- @Autowired
- private UserRepository userRepository;
-
- @Autowired
- private GroupPostRepository groupPostRepository;
-
- @Autowired
- private GroupCommentsRepository groupCommentsRepository;
-
- private static final String IMAGE_DIR = "uploads/groupPost/";
-
- @Transactional
- // 创建小组的方法
- public ResponseEntity<Map<String, Object>> createGroup(Group groupRequest) {
- try {
- // 创建新的小组对象
- Group newGroup = new Group();
- newGroup.setGroupName(groupRequest.getGroupName());
- newGroup.setDescription(groupRequest.getDescription());
- newGroup.setCreateTime(new Date());
- newGroup.setUserId(groupRequest.getUserId());
- newGroup.setMemberCount(1);
- newGroup.setCategory(groupRequest.getCategory());
- newGroup.setCoverImage(groupRequest.getCoverImage());
- Group createdGroup = groupRepository.save(newGroup);
-
- // 返回成功响应
- Map<String, Object> response = new HashMap<>();
- response.put("status", "success");
- response.put("message", "兴趣小组创建成功");
- response.put("group_id", createdGroup.getGroupId());
-
- return ResponseEntity.ok(response);
- } catch (Exception e) {
- // 返回失败响应
- Map<String, Object> errorResponse = new HashMap<>();
- errorResponse.put("status", "error");
- errorResponse.put("message", "小组创建失败");
- errorResponse.put("group_id", null);
-
- return ResponseEntity.status(400).body(errorResponse);
- }
- }
-
-
- // 加入小组
- @Transactional
- public ResponseEntity<Map<String, Object>> joinGroup(Long groupId, Long userId) {
- // 查找小组
- Optional<Group> groupOptional = groupRepository.findById(groupId);
- if (!groupOptional.isPresent()) {
- return ResponseEntity.status(400).body(
- Map.of("status", "failure", "message", "小组不存在")
- );
- }
-
- Group group = groupOptional.get();
-
- // 检查用户是否已经是该小组成员
- if (groupMembersRepository.existsByGroupIdAndUserId(groupId, userId)) {
- return ResponseEntity.status(400).body(
- Map.of("status", "failure", "message", "用户已经是该小组的成员")
- );
- }
-
- // 插入用户和小组的关联记录
- GroupMembers groupMembers = new GroupMembers();
- groupMembers.setGroupId(groupId);
- groupMembers.setUserId(userId);
- groupMembersRepository.save(groupMembers);
-
- // 更新小组的成员数
- group.setMemberCount(group.getMemberCount() + 1);
- groupRepository.save(group);
-
- return ResponseEntity.ok().body(
- Map.of("status", "success", "message", "成功加入小组")
- );
- }
-
- // 退出小组
- @Transactional
- public ResponseEntity<Map<String, Object>> leaveGroup(Long groupId, Long userId) {
- // 查找小组
- Optional<Group> groupOptional = groupRepository.findById(groupId);
- if (!groupOptional.isPresent()) {
- return ResponseEntity.status(400).body(
- Map.of("status", "failure", "message", "小组不存在")
- );
- }
-
- Group group = groupOptional.get();
-
- // 检查用户是否是该小组成员
- if (!groupMembersRepository.existsByGroupIdAndUserId(groupId, userId)) {
- return ResponseEntity.status(400).body(
- Map.of("status", "failure", "message", "用户不是该小组的成员")
- );
- }
-
- // 删除用户和小组的关联记录
- groupMembersRepository.deleteByGroupIdAndUserId(groupId, userId);
-
- // 更新小组的成员数
- group.setMemberCount(group.getMemberCount() - 1);
- groupRepository.save(group);
-
- return ResponseEntity.ok().body(
- Map.of("status", "success", "message", "成功退出小组")
- );
- }
-
- // 获取所有成员
- @Transactional
- public ResponseEntity<Map<String, Object>> getGroupMembers(Long groupId) {
- // 查找小组
- Optional<Group> groupOptional = groupRepository.findById(groupId);
- if (!groupOptional.isPresent()) {
- return ResponseEntity.status(400).body(
- Map.of("status", "failure", "message", "小组不存在")
- );
- }
-
- Group group = groupOptional.get();
-
- // 获取所有小组成员
- List<GroupMembers> groupMembersList = groupMembersRepository.findByGroupId(groupId);
-
- // 创建成员返回列表
- List<Map<String, Object>> members = new ArrayList<>();
- for (GroupMembers groupMember : groupMembersList) {
- Optional<Users> userOptional = userRepository.findById(groupMember.getUserId());
- if (userOptional.isPresent()) {
- Users user = userOptional.get();
- members.add(Map.of(
- "user_id", user.getUserId(),
- "username", user.getUsername(),
- "avatar_url", user.getAvatarUrl()
- ));
- }
- }
-
- return ResponseEntity.ok().body(
- Map.of("status", "success", "members", members)
- );
- }
-
-
- //发布帖子
- public Map<String, Object> createPost(Long groupId, Long userId, String postContent, String title, MultipartFile[] imageFiles) {
- // 创建一个新的帖子对象
- GroupPost post = new GroupPost();
- post.setGroupId(groupId);
- post.setUserId(userId);
- post.setContent(postContent);
- post.setTitle(title);
- post.setTime(new Date());
- post.setLikeCount(0);
- post.setCommentCount(0);
-
- // 处理图片上传,如果没有图片,返回空字符串
- String imageUrls = handleImages(imageFiles);
- post.setImage(imageUrls);
-
- // 保存帖子到数据库
- GroupPost savedPost = groupPostRepository.save(post);
-
- // 返回结果
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("post_id", savedPost.getGroupPostId());
- response.put("message", "帖子创建成功");
- return response;
- }
-
- // 处理图片上传的方法,返回图片的 URL 字符串
- private String handleImages(MultipartFile[] imageFiles) {
- if (imageFiles == null || imageFiles.length == 0) {
- return "";
- }
-
- StringBuilder imageUrlsBuilder = new StringBuilder();
-
- for (int i = 0; i < imageFiles.length; i++) {
- if (i > 0) {
- imageUrlsBuilder.append(",");
- }
- try {
- String imageUrl = saveImage(imageFiles[i]);
- imageUrlsBuilder.append(imageUrl);
- } catch (IOException e) {
- throw new RuntimeException("图片上传失败: " + e.getMessage());
- }
- }
- return imageUrlsBuilder.toString();
- }
-
- // 保存图片并返回图片的 URL
- private String saveImage(MultipartFile imageFile) throws IOException {
- if (imageFile == null) {
- throw new IOException("图片文件为空");
- }
-
- String fileName = imageFile.getOriginalFilename();
- Path path = Path.of(IMAGE_DIR + fileName);
- Files.createDirectories(path.getParent());
- Files.write(path, imageFile.getBytes());
- return "/images/" + fileName;
- }
-
- // 获取小组内的所有帖子
- public Map<String, Object> getAllPosts(Long groupId) {
- List<GroupPost> posts = groupPostRepository.findByGroupId(groupId);
-
- List<Map<String, Object>> postList = new ArrayList<>();
-
- for (GroupPost post : posts) {
- Map<String, Object> postData = new HashMap<>();
- Users user = userRepository.findById(post.getUserId()).orElseThrow(() -> new RuntimeException("User not found"));
- postData.put("group_post_id", post.getGroupPostId());
- postData.put("user_id", post.getUserId());
- postData.put("username", user.getUsername());
- postData.put("title", post.getTitle());
- postData.put("content", post.getContent());
- postData.put("time", post.getTime());
-
- postList.add(postData);
- }
-
- // 返回响应数据
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "success");
- response.put("posts", postList);
- return response;
- }
-
-
- // 获取小组列表接口
- public Map<String, Object> searchGroups(Map<String, Object> params) {
- String name = (String) params.get("name");
- String category = (String) params.get("category");
- Integer page = (Integer) params.getOrDefault("page", 1);
- Integer size = (Integer) params.getOrDefault("size", 10);
- String sortBy = (String) params.getOrDefault("sort_by", "memberCount");
-
- // 构建分页对象
- Pageable pageable;
-
- // 根据 `sortBy` 判断排序方式
- if ("createTime".equals(sortBy)) {
- pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.asc("createTime")));
- } else if ("groupName".equals(sortBy)) {
- pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.asc("groupName")));
- } else {
- pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("memberCount")));
- }
-
- Page<Group> groups;
- if (name != null && !name.isEmpty()) {
- // 小组名称模糊查询
- groups = groupRepository.searchGroups(name, category, pageable);
- } else {
- groups = groupRepository.searchWithFilters(category, pageable);
- }
-
- return buildResponse(groups);
- }
-
- // 构建返回的响应数据
- private Map<String, Object> buildResponse(Page<Group> groups) {
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "success");
- response.put("total", groups.getTotalElements());
- response.put("total_pages", groups.getTotalPages());
- response.put("current_page", groups.getNumber() + 1);
- response.put("items", groups.getContent());
-
- return response;
- }
-
- // 点赞帖子
- public Map<String, Object> likePost(Long GroupPostId) {
- // 查找帖子
- Optional<GroupPost> postOpt = groupPostRepository.findById(GroupPostId);
- if (!postOpt.isPresent()) {
- throw new RuntimeException("Post not found");
- }
-
- GroupPost post = postOpt.get();
-
- // 更新点赞数
- post.setLikeCount(post.getLikeCount() + 1);
- groupPostRepository.save(post);
-
- // 返回成功信息
- Map<String, Object> response = new HashMap<>();
- response.put("status", "success");
- response.put("message", "点赞成功");
- return response;
- }
-
- // 取消点赞
- public Map<String, Object> unlikePost(Long GroupPostId) {
- // 查找帖子
- Optional<GroupPost> postOpt = groupPostRepository.findById(GroupPostId);
- if (!postOpt.isPresent()) {
- throw new RuntimeException("Post not found");
- }
-
- GroupPost post = postOpt.get();
-
- // 更新点赞数
- if (post.getLikeCount() > 0) {
- post.setLikeCount(post.getLikeCount() - 1);
- groupPostRepository.save(post);
-
- // 返回成功信息
- Map<String, Object> response = new HashMap<>();
- response.put("status", "success");
- response.put("message", "取消点赞成功");
- return response;
- } else {
- // 如果点赞数已经是0,则返回相应消息
- Map<String, Object> response = new HashMap<>();
- response.put("status", "failure");
- response.put("message", "无法取消点赞,点赞数已经为0");
- return response;
- }
- }
-
- // 添加评论并更新帖子的评论数
- public Map<String, Object> addComment(Long postId, GroupComments comment) {
- // 查找帖子
- Optional<GroupPost> postOpt = groupPostRepository.findById(postId);
- if (!postOpt.isPresent()) {
- throw new RuntimeException("Post not found");
- }
-
- GroupPost post = postOpt.get();
-
- comment.setGroupPostId(postId);
- comment.setTime(new java.util.Date());
- groupCommentsRepository.save(comment);
- post.setCommentCount(post.getCommentCount() + 1);
- groupPostRepository.save(post);
- Map<String, Object> response = new HashMap<>();
- response.put("status", "success");
- response.put("message", "评论成功");
- return response;
- }
-
-
-}
diff --git a/src/main/java/com/example/myproject/service/LevelService.java b/src/main/java/com/example/myproject/service/LevelService.java
deleted file mode 100644
index 92f50ed..0000000
--- a/src/main/java/com/example/myproject/service/LevelService.java
+++ /dev/null
@@ -1,140 +0,0 @@
-package com.example.myproject.service;
-
-import com.example.myproject.entity.ExperienceHistory;
-import com.example.myproject.entity.Users;
-import com.example.myproject.entity.Level;
-import com.example.myproject.repository.ExperienceHistoryRepository;
-import com.example.myproject.repository.UserRepository;
-import com.example.myproject.repository.LevelRepository;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-
-import java.util.Date;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.HashMap;
-
-@Service
-public class LevelService {
-
- @Autowired
- private UserRepository userRepository;
-
- @Autowired
- private LevelRepository levelRepository;
-
- @Autowired
- private ExperienceHistoryRepository experienceHistoryRepository;
-
-
- public Map<String, Object> getUserExperience(Long userId) {
- // 获取用户信息
- Users user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("User not found"));
-
- // 获取当前等级
- Level currentLevel = levelRepository.findById(user.getLevel()).orElseThrow(() -> new RuntimeException("Level not found"));
-
- // 获取下一个等级的经验需求
- Level nextLevel = levelRepository.findById(currentLevel.getLevelId() + 1).orElse(null);
-
- // 构建响应数据
- Map<String, Object> response = new HashMap<>();
- response.put("current_experience", user.getCurrentExperience());
- response.put("level", currentLevel.getLevelName());
- response.put("next_level_experience", nextLevel != null ? nextLevel.getRequiredExperience() : 0);
-
- return response;
- }
-
- public Map<String, Object> updateExperience(Long userId, Integer experience, String source) {
- // 获取用户信息
- Users user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("用户不存在"));
-
- // 更新用户的经验值
- user.setCurrentExperience(user.getCurrentExperience() + experience);
-
- // 保存更新后的用户信息
- userRepository.save(user);
-
- // 创建经验更新历史记录
- ExperienceHistory experienceHistory = new ExperienceHistory();
- experienceHistory.setUserId(userId);
- experienceHistory.setExperienceChange(experience);
- experienceHistory.setSource(source);
- experienceHistory.setUpdateTime(new Date());
-
- // 保存更新历史
- experienceHistoryRepository.save(experienceHistory);
-
- // 返回更新后的经验值和消息
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "success");
- response.put("message", "经验更新成功");
- response.put("current_experience", user.getCurrentExperience()); // 返回更新后的经验值
-
- return response;
- }
-
- public Map<String, Object> checkUpgrade(Long userId) {
- // 获取用户信息
- Users user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("用户不存在"));
-
- // 获取当前等级
- Level currentLevel = levelRepository.findById(user.getLevel())
- .orElseThrow(() -> new RuntimeException("用户等级信息不存在"));
-
- // 获取下一个等级的经验需求
- Level nextLevel = levelRepository.findById(currentLevel.getLevelId() + 1).orElse(null);
-
- // 构建响应数据
- Map<String, Object> response = new LinkedHashMap<>();
-
- // 判断用户是否可以升级
- boolean canUpgrade = user.getCurrentExperience() >= (nextLevel != null ? nextLevel.getRequiredExperience() : 0);
- response.put("can_upgrade", canUpgrade);
- // 构建响应数据
-
- response.put("next_level_experience", nextLevel != null ? nextLevel.getRequiredExperience() : 0);
- response.put("current_experience", user.getCurrentExperience());
-
-
- return response;
- }
-
- public Map<String, Object> upgradeUserLevel(Long userId, Boolean canUpgrade) {
- // 获取用户信息
- Users user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("User not found"));
-
- // 创建响应对象
- Map<String, Object> response = new HashMap<>();
-
- if (canUpgrade) {
- // 获取当前等级
- Level currentLevel = levelRepository.findById(user.getLevel())
- .orElseThrow(() -> new RuntimeException("Level not found"));
-
- // 获取下一个等级的经验需求
- Level nextLevel = levelRepository.findById(currentLevel.getLevelId() + 1).orElse(null);
-
- if (nextLevel != null) {
- user.setLevel(user.getLevel() + 1);
- userRepository.save(user);
- Level newLevel = levelRepository.findById(user.getLevel())
- .orElseThrow(() -> new RuntimeException("Level not found"));
- response.put("message", "升级成功");
- response.put("status", "success");
- response.put("new_level", newLevel.getLevelName());
- } else {
- response.put("message", "用户已达到最高等级");
- response.put("status", "error");
- }
- } else {
- response.put("message", "用户不满足升级条件");
- response.put("status", "error");
- }
-
- return response;
- }
-
-}
diff --git a/src/main/java/com/example/myproject/service/PostService.java b/src/main/java/com/example/myproject/service/PostService.java
deleted file mode 100644
index 515de78..0000000
--- a/src/main/java/com/example/myproject/service/PostService.java
+++ /dev/null
@@ -1,270 +0,0 @@
-package com.example.myproject.service;
-
-import com.example.myproject.entity.Likes;
-import com.example.myproject.entity.Post;
-import com.example.myproject.entity.Users;
-import com.example.myproject.repository.CollectionsRepository;
-import com.example.myproject.repository.LikesRepository;
-import com.example.myproject.repository.PostRepository;
-import com.example.myproject.repository.UserRepository;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
-import com.example.myproject.entity.Collections;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.*;
-
-@Service
-public class PostService {
-
- @Autowired
- private PostRepository postRepository;
-
- @Autowired
- private UserRepository userRepository;
- @Autowired
- private LikesRepository likesRepository;
-
- @Autowired
- private CollectionsRepository collectionsRepository;
-
- private static final String IMAGE_DIR = "uploads/post/";
-
-
- //创建帖子
- public Map<String, Object> createPost(Long userId, String postContent, String title, MultipartFile[] imageFiles) {
- // 查找用户信息
- Users user = userRepository.findById(Long.valueOf(userId)).orElseThrow(() -> new RuntimeException("User not found"));
- Post post = new Post();
- post.setUser_id(user.getUserId());
- post.setPostTime(new Date());
- post.setPostLikeNum(0);
- post.setPostCollectNum(0);
- post.setPostContent(postContent);
- post.setTitle(title);
-
- // 处理多张图片的上传
- StringBuilder imageUrlsBuilder = new StringBuilder();
- if (imageFiles != null && imageFiles.length > 0) {
- for (int i = 0; i < imageFiles.length; i++) {
- if (i > 0) {
- imageUrlsBuilder.append(",");
- }
- try {
- String imageUrl = saveImage(imageFiles[i]);
- imageUrlsBuilder.append(imageUrl);
- } catch (IOException e) {
- throw new RuntimeException("Image upload failed: " + e.getMessage());
- }
- }
- }
- post.setImageUrl(imageUrlsBuilder.toString());
- Post savedPost = postRepository.save(post);
- Map<String, Object> response = new HashMap<>();
- response.put("postNo", savedPost.getPostNo());
- response.put("message", "帖子创建成功");
- return response;
- }
-
- // 保存图片并返回图片的 URL
- public String saveImage(MultipartFile imageFile) throws IOException {
- String fileName = imageFile.getOriginalFilename();
- Path path = Paths.get(IMAGE_DIR + fileName);
- Files.createDirectories(path.getParent());
- Files.write(path, imageFile.getBytes());
- return "/images/" + fileName;
- }
-
-
- //编辑帖子
- public void updatePost(Long postId, Post post) {
- Post existingPost = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found"));
- if (post.getTitle() != null) {
- existingPost.setTitle(post.getTitle());
- }
- if (post.getPostContent() != null) {
- existingPost.setPostContent(post.getPostContent());
- }
- if (post.getImageUrl() != null) {
- existingPost.setImageUrl(post.getImageUrl());
- }
- existingPost.setPostTime(post.getPostTime());
- // 保存更新后的帖子
- postRepository.save(existingPost);
- }
-
- //删除帖子
- public void deletePost(Long postId) {
- // 查找指定 ID 的帖子,如果不存在则抛出异常
- if (!postRepository.existsById(postId)) {
- throw new RuntimeException("Post not found");
- }
- // 删除该帖子
- postRepository.deleteById(postId);
- }
-
- //点赞帖子(已完成)
- public void likePost(Long postId, Long userId) {
- // 查找指定 ID 的帖子
- Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found"));
- post.setPostLikeNum(post.getPostLikeNum() + 1);
-
- // 保存帖子
- postRepository.save(post);
- Likes like = new Likes();
- like.setUserId(userId);
- like.setPostNo(postId);
- // 保存点赞记录
- likesRepository.save(like);
- }
-
- // 取消点赞帖子(已完成)
- public void unlikePost(Long postId, Long userId) {
- // 查找指定 ID 的帖子
- Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found"));
-
- // 如果点赞数大于 0,则减少点赞数
- if (post.getPostLikeNum() > 0) {
- post.setPostLikeNum(post.getPostLikeNum() - 1);
- }
-
- // 删除点赞表中对应的记录
- likesRepository.deleteLikeByUserIdAndPostNo(userId, postId);
-
- // 保存更新后的帖子
- postRepository.save(post);
- }
-
-
- // 获取帖子列表(已完成)
- public Map<String, Object> getAllPosts() {
- List<Post> posts = postRepository.findAll();
- List<Map<String, Object>> postList = new ArrayList<>();
-
- for (Post post : posts) {
- Map<String, Object> postMap = new LinkedHashMap<>();
- postMap.put("postNo", post.getPostNo());
- postMap.put("user_id", post.getUser_id());
- postMap.put("postContent", post.getPostContent());
- postMap.put("imgUrl", post.getImageUrl());
- postMap.put("title", post.getTitle());
- postMap.put("createdAt", post.getPostTime().toString());
- postMap.put("likeCount", post.getPostLikeNum());
- postMap.put("collectCount", post.getPostCollectNum());
-
- postList.add(postMap);
- }
-
- Map<String, Object> response = new HashMap<>();
- // 统计帖子数量
- response.put("total", postList.size());
- response.put("posts", postList);
-
- return response;
- }
-
- public Map<String, Object> getPostById(Long postId) {
- // 获取帖子
- Optional<Post> postOptional = postRepository.findById(postId);
-
- // 如果帖子存在
- if (postOptional.isPresent()) {
- Post post = postOptional.get();
- Map<String, Object> postData = new LinkedHashMap<>();
- postData.put("postNo", post.getPostNo());
- postData.put("user_id", post.getUser_id());
- Long userId = post.getUser_id();
- Users user = userRepository.findById(userId).orElse(null);
-
- if (user != null) {
- postData.put("username", user.getUsername());
- postData.put("avatar_url", user.getAvatarUrl());
- } else {
- postData.put("username", null);
- postData.put("avatar_url", null);
- }
-
- postData.put("postContent", post.getPostContent());
- postData.put("imageUrl", post.getImageUrl());
- postData.put("postTime", post.getPostTime());
- postData.put("postLikeNum", post.getPostLikeNum());
- postData.put("postCollectNum", post.getPostCollectNum());
- postData.put("title", post.getTitle());
-
- return postData;
- } else {
- throw new RuntimeException("Post not found with id: " + postId);
- }
- }
-
-
- //收藏
- public void collectPost(Long postId, Long userId) {
- Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found"));
- post.setPostCollectNum(post.getPostCollectNum() + 1);
- postRepository.save(post);
-
- // 添加到收藏表
- Collections collection = new Collections();
- collection.setUserId(userId);
- collection.setPostNo(postId);
- collectionsRepository.save(collection);
- }
-
- //取消收藏
- public void uncollectPost(Long postId, Long userId) {
- Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found"));
- // 减少帖子收藏数
- if (post.getPostCollectNum() > 0) {
- post.setPostCollectNum(post.getPostCollectNum() - 1);
- postRepository.save(post);
- }
-
- // 从收藏表中删除对应记录
- collectionsRepository.deleteLikeByUserIdAndPostNo(userId, postId);
- }
-
- // 获取用户收藏的所有帖子
- public List<Map<String, Object>> getAllCollections(Long userId) {
- // 获取用户收藏的帖子ID
- List<Collections> collections = collectionsRepository.findByUserId(userId);
-
- // 如果没有收藏的帖子,返回空列表
- if (collections.isEmpty()) {
- return new ArrayList<>();
- }
-
- List<Map<String, Object>> response = new ArrayList<>();
- for (Collections collection : collections) {
- // 根据 postNo 查询帖子
- Optional<Post> postOptional = postRepository.findById(collection.getPostNo());
- if (postOptional.isPresent()) {
- Post post = postOptional.get();
-
- // 获取帖子作者的信息
- Optional<Users> userOptional = userRepository.findById(post.getUser_id());
- if (userOptional.isPresent()) {
- Users user = userOptional.get();
-
- // 将帖子数据和用户信息封装到 Map 中
- Map<String, Object> postInfo = new LinkedHashMap<>();
- postInfo.put("postNo", post.getPostNo());
- postInfo.put("title", post.getTitle());
- postInfo.put("postContent", post.getPostContent());
- postInfo.put("imageUrl", post.getImageUrl());
- postInfo.put("userId", post.getUser_id());
- postInfo.put("username", user.getUsername()); // 帖子作者用户名
- postInfo.put("avatarUrl", user.getAvatarUrl()); // 帖子作者头像
-
- response.add(postInfo);
- }
- }
- }
- return response;
- }
-
-
-}
diff --git a/src/main/java/com/example/myproject/service/PromotionService.java b/src/main/java/com/example/myproject/service/PromotionService.java
new file mode 100644
index 0000000..087c13b
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/PromotionService.java
@@ -0,0 +1,17 @@
+package com.example.myproject.service;
+
+import com.example.myproject.entity.Promotion;
+import com.example.myproject.dto.PromotionCreateDTO;
+import java.util.List;
+
+public interface PromotionService {
+ Promotion createPromotion(PromotionCreateDTO promotionDTO);
+
+ List<Promotion> getAllActivePromotions();
+
+ Promotion getPromotionById(Long promotionId);
+
+ void deletePromotion(Long promotionId);
+
+ double getCurrentDiscount(Long torrentId);
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/SeedCommentService.java b/src/main/java/com/example/myproject/service/SeedCommentService.java
deleted file mode 100644
index 530b164..0000000
--- a/src/main/java/com/example/myproject/service/SeedCommentService.java
+++ /dev/null
@@ -1,158 +0,0 @@
-package com.example.myproject.service;
-
-import com.example.myproject.entity.SeedComment;
-import com.example.myproject.entity.SeedCommentLikes;
-import com.example.myproject.repository.SeedCommentLikesRepository;
-import com.example.myproject.repository.SeedCommentRepository;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-import java.util.*;
-
-@Service
-public class SeedCommentService {
-
- @Autowired
- private SeedCommentRepository seedCommentRepository;
-
- @Autowired
- private SeedCommentLikesRepository seedCommentLikesRepository;
-
- public String publishComment(int seedId, SeedComment seedComment) {
- try {
- // 如果是回复评论,检查父评论是否存在
- if (seedComment.getComCommentId() != 0) {
- // 查找父评论是否存在
- boolean parentExists = seedCommentRepository.existsById(seedComment.getComCommentId());
- if (!parentExists) {
- return "{ \"status\": \"error\", \"message\": \"父评论不存在\" }";
- }
- }
-
- // 设置评论的种子ID
- seedComment.setSeedId(seedId);
- seedComment.setCommentTime(new Date());
- seedComment.setLikesCount(0); // 默认点赞数为0
- seedComment.setReplyCount(0); // 默认回复数为0
-
- // 如果没有父评论,com_comment_id 默认为0
- if (seedComment.getComCommentId() == 0) {
- seedComment.setComCommentId(0);
- }
-
- // 保存评论到数据库
- seedCommentRepository.save(seedComment);
-
- // 返回成功信息
- return "{ \"status\": \"success\", \"message\": \"评论发布成功\" }";
- } catch (Exception e) {
- // 处理异常,返回失败信息
- return "{ \"status\": \"error\", \"message\": \"评论发布失败\" }";
- }
- }
-
- public Map<String, Object> getAllCommentsForSeed(int seedId) {
- // 获取该种子的所有评论(顶级评论)
- List<SeedComment> allComments = seedCommentRepository.findBySeedId(seedId);
- List<Map<String, Object>> responseComments = new ArrayList<>();
-
- for (SeedComment comment : allComments) {
- Map<String, Object> commentData = new HashMap<>();
- commentData.put("comment_id", comment.getCommentId());
- commentData.put("user_id", comment.getUserId());
- commentData.put("username", "User" + comment.getUserId()); // 通过用户ID查询用户名
- commentData.put("content", comment.getContent());
- commentData.put("parent_comment_id", comment.getComCommentId());
-
- // 获取该评论的所有回复
- List<Map<String, Object>> replies = getRepliesForComment(comment.getCommentId());
- commentData.put("replies", replies);
-
- responseComments.add(commentData);
- }
-
- // 返回最终结果
- Map<String, Object> result = new LinkedHashMap<>();
- result.put("status", "success");
- result.put("comments", responseComments);
-
- return result;
- }
-
- // 获取特定评论的所有回复
- private List<Map<String, Object>> getRepliesForComment(long parentCommentId) {
- List<SeedComment> replies = seedCommentRepository.findByComCommentId(parentCommentId);
- List<Map<String, Object>> replyResponses = new ArrayList<>();
-
- for (SeedComment reply : replies) {
- Map<String, Object> replyData = new LinkedHashMap<>();
- replyData.put("comment_id", reply.getCommentId());
- replyData.put("user_id", reply.getUserId());
- replyData.put("username", "User" + reply.getUserId()); // 通过用户ID查询用户名
- replyData.put("content", reply.getContent());
- replyData.put("parent_comment_id", reply.getComCommentId());
- replyResponses.add(replyData);
- }
-
- return replyResponses;
- }
-
- // 点赞切换逻辑
- public Map<String, Object> toggleLike(Long commentId, Long userId) {
- // 查找是否有该评论
- Optional<SeedComment> seedCommentOptional = seedCommentRepository.findById(commentId);
- if (!seedCommentOptional.isPresent()) {
- return Map.of("status", "error", "message", "评论不存在");
- }
-
- SeedComment seedComment = seedCommentOptional.get();
-
- // 查找该用户是否已点赞
- Optional<SeedCommentLikes> commentLikeOptional = seedCommentLikesRepository.findByCommentIdAndUserId(commentId, userId);
-
- // 如果用户未点赞,添加新的点赞记录
- if (commentLikeOptional.isEmpty()) {
- SeedCommentLikes newLike = new SeedCommentLikes();
- newLike.setCommentId(commentId);
- newLike.setUserId(userId);
- newLike.setIsLiked(true);
-
- // 更新 SeedComment 表中的点赞数
- seedComment.setLikesCount(seedComment.getLikesCount() + 1);
- seedCommentRepository.save(seedComment);
-
- // 保存点赞记录
- seedCommentLikesRepository.save(newLike);
-
- return Map.of("status", "success", "message", "操作成功", "liked", "true");
- } else {
- SeedCommentLikes commentLike = commentLikeOptional.get();
-
- // 如果已经点赞,取消点赞
- if (commentLike.getIsLiked()) {
- commentLike.setIsLiked(false);
-
- // 更新 SeedComment 表中的点赞数
- seedComment.setLikesCount(seedComment.getLikesCount() - 1);
- seedCommentRepository.save(seedComment);
-
- // 更新点赞记录
- seedCommentLikesRepository.save(commentLike);
-
- return Map.of("status", "success", "message", "操作成功", "liked", "false");
- } else {
- // 如果未点赞,进行点赞
- commentLike.setIsLiked(true);
-
- // 更新 SeedComment 表中的点赞数
- seedComment.setLikesCount(seedComment.getLikesCount() + 1);
- seedCommentRepository.save(seedComment);
-
- // 更新点赞记录
- seedCommentLikesRepository.save(commentLike);
-
- return Map.of("status", "success", "message", "操作成功", "liked", "true");
- }
- }
- }
-}
diff --git a/src/main/java/com/example/myproject/service/TaskService.java b/src/main/java/com/example/myproject/service/TaskService.java
deleted file mode 100644
index bd04661..0000000
--- a/src/main/java/com/example/myproject/service/TaskService.java
+++ /dev/null
@@ -1,295 +0,0 @@
-package com.example.myproject.service;
-
-import com.example.myproject.entity.Level;
-import com.example.myproject.entity.Task;
-import com.example.myproject.entity.UserTaskStatus;
-import com.example.myproject.repository.LevelRepository;
-import com.example.myproject.repository.TaskRepository;
-import com.example.myproject.repository.UserTaskStatusRepository;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import com.example.myproject.entity.Users;
-import com.example.myproject.repository.UserRepository;
-
-import java.util.*;
-
-@Service
-public class TaskService {
-
- @Autowired
- private TaskRepository taskRepository;
-
- @Autowired
- private UserTaskStatusRepository userTaskStatusRepository;
-
- @Autowired
- private UserRepository userRepository;
-
- @Autowired
- private LevelRepository levelRepository;
-
- public Map<String, Object> getAllTasksForUser(Long userId) {
- // 获取所有任务
- List<Task> tasks = taskRepository.findAll();
- List<Map<String, Object>> taskList = new ArrayList<>();
-
- for (Task task : tasks) {
- Optional<UserTaskStatus> userTaskStatus = userTaskStatusRepository
- .findByUserIdAndTaskId(userId, task.getTaskId());
-
- Map<String, Object> taskData = new LinkedHashMap<>();
- taskData.put("task_id", task.getTaskId());
- taskData.put("title", task.getTitle());
- taskData.put("description", task.getDescription());
- taskData.put("status", userTaskStatus.isPresent() ? userTaskStatus.get().getStatus() : "pending");
- taskData.put("progress", userTaskStatus.isPresent() ? userTaskStatus.get().getCurrentProgress() : 0);
-
- // 奖励
- Map<String, Integer> reward = new LinkedHashMap<>();
- reward.put("experience", task.getRewardExperience());
- reward.put("points", task.getRewardPoints());
- taskData.put("reward", reward);
-
- taskList.add(taskData);
- }
-
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("tasks", taskList);
- return response;
- }
-
- public Map<String, Object> updateTaskStatus(Long userId, String taskId) {
- // 获取用户任务状态
- Optional<UserTaskStatus> userTaskStatusOpt = userTaskStatusRepository.findByUserIdAndTaskId(userId, taskId);
- UserTaskStatus userTaskStatus = userTaskStatusOpt.get();
-
- // 检查任务进度
- if (userTaskStatus.getCurrentProgress() == 100f) {
- Optional<Task> taskOpt = taskRepository.findById(taskId);
- Task task = taskOpt.get();
-
- // 更新任务状态为已完成
- userTaskStatus.setStatus("completed");
- userTaskStatus.setCurrentExperience(task.getRewardExperience());
- userTaskStatus.setCurrentPoints(task.getRewardPoints());
-
- userTaskStatusRepository.save(userTaskStatus);
-
- // 构建返回数据
- Map<String, Object> response = new HashMap<>();
- response.put("status", "success");
- response.put("message", "任务状态已更新");
-
- Map<String, Integer> currentReward = new HashMap<>();
- currentReward.put("current_experience", task.getRewardExperience());
- currentReward.put("current_points", task.getRewardPoints());
- response.put("current_reward", currentReward);
-
- return response;
- } else {
- Map<String, Object> errorResponse = new HashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "任务未完成,无法更新状态");
- return errorResponse;
- }
- }
-
- public Map<String, Object> getUserExperience(Long userId) {
- // 获取用户信息
- Optional<Users> userOpt = userRepository.findById(userId);
- if (!userOpt.isPresent()) {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "用户不存在");
- return errorResponse;
- }
-
- Users user = userOpt.get();
- Integer currentExperience = user.getCurrentExperience();
- Optional<Level> levelOpt = levelRepository.findById(user.getLevel());
- String levelName = levelOpt.isPresent() ? levelOpt.get().getLevelName() : "未知等级";
-
- List<UserTaskStatus> completedTasks = userTaskStatusRepository.findByUserIdAndStatus(userId, "completed");
-
- int totalExperience = 0;
- int totalPoints = 0;
- for (UserTaskStatus taskStatus : completedTasks) {
- Optional<Task> taskOpt = taskRepository.findById(taskStatus.getTaskId());
- if (taskOpt.isPresent()) {
- Task task = taskOpt.get();
- totalExperience += task.getRewardExperience();
- totalPoints += task.getRewardPoints();
- }
- }
-
- // 构建返回数据
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("user_id", userId);
- response.put("current_experience", currentExperience);
- response.put("level", levelName);
- response.put("tasks_completed", completedTasks.size());
-
- Map<String, Integer> reward = new LinkedHashMap<>();
- reward.put("experience", totalExperience);
- reward.put("points", totalPoints);
-
- response.put("reward", reward);
-
- return response;
- }
-
- public Map<String, Object> getNewStep(Long userId) {
- // 获取用户的第一个未完成任务
- Optional<UserTaskStatus> userTaskStatusOpt = userTaskStatusRepository.findFirstByUserIdAndStatus(userId, "pending");
-
- if (!userTaskStatusOpt.isPresent()) {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "用户当前没有未完成的任务");
- return errorResponse;
- }
-
- UserTaskStatus userTaskStatus = userTaskStatusOpt.get();
- String taskId = userTaskStatus.getTaskId();
-
- // 获取该任务的描述
- Optional<Task> taskOpt = taskRepository.findById(taskId);
- if (!taskOpt.isPresent()) {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "任务不存在");
- return errorResponse;
- }
-
- Task task = taskOpt.get();
-
- // 获取总任务数
- long totalTasks = taskRepository.count();
-
- // 构建返回数据
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("current_step", taskId);
- response.put("total_steps", totalTasks);
- response.put("step_description", task.getDescription());
-
- return response;
- }
-
- public Map<String, Object> updateTaskProgress(Long userId, String taskId, Integer progress) {
- Optional<UserTaskStatus> userTaskStatusOpt = userTaskStatusRepository.findByUserIdAndTaskId(userId, taskId);
-
- if (!userTaskStatusOpt.isPresent()) {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "任务不存在或该任务不属于用户");
- return errorResponse;
- }
-
- UserTaskStatus userTaskStatus = userTaskStatusOpt.get();
- userTaskStatus.setCurrentProgress(Float.valueOf(progress));
- userTaskStatusRepository.save(userTaskStatus);
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "success");
- response.put("message", "进度已更新");
-
- return response;
- }
-
- public Map<String, Object> claimReward(Long userId, String taskId) {
- // 获取用户任务状态
- Optional<UserTaskStatus> userTaskStatusOpt = userTaskStatusRepository.findByUserIdAndTaskId(userId, taskId);
-
- if (!userTaskStatusOpt.isPresent()) {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "任务状态不存在");
- return errorResponse;
- }
-
- UserTaskStatus userTaskStatus = userTaskStatusOpt.get();
-
- if (userTaskStatus.getCurrentProgress() == 100f) {
- if (userTaskStatus.getIsRewardClaimed()) {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "奖励已经领取过了");
- return errorResponse;
- }
-
- Optional<Task> taskOpt = taskRepository.findById(taskId);
- if (!taskOpt.isPresent()) {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "任务不存在");
- return errorResponse;
- }
-
- Task task = taskOpt.get();
-
- userTaskStatus.setIsRewardClaimed(true);
- userTaskStatus.setCurrentExperience(task.getRewardExperience());
- userTaskStatus.setCurrentPoints(task.getRewardPoints());
-
- userTaskStatusRepository.save(userTaskStatus);
-
- Optional<Users> userOpt = userRepository.findById(userId);
- if (!userOpt.isPresent()) {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "用户不存在");
- return errorResponse;
- }
-
- Users user = userOpt.get();
-
- // 将任务奖励加到用户的积分和经验
- user.setUserPoints(user.getUserPoints() + task.getRewardPoints());
- user.setCurrentExperience(user.getCurrentExperience() + task.getRewardExperience());
-
- userRepository.save(user);
-
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "success");
- response.put("message", "奖励已领取");
-
- Map<String, Integer> reward = new LinkedHashMap<>();
- reward.put("experience", task.getRewardExperience());
- reward.put("points", task.getRewardPoints());
- response.put("reward", reward);
-
- return response;
- } else {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "任务未完成,无法领取奖励");
- return errorResponse;
- }
- }
-
- public Map<String, Object> checkRewardStatus(Long userId, String taskId) {
- // 获取用户任务状态
- Optional<UserTaskStatus> userTaskStatusOpt = userTaskStatusRepository.findByUserIdAndTaskId(userId, taskId);
-
- if (!userTaskStatusOpt.isPresent()) {
- Map<String, Object> errorResponse = new LinkedHashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "任务状态不存在");
- return errorResponse;
- }
-
- UserTaskStatus userTaskStatus = userTaskStatusOpt.get();
-
- // 检查奖励是否已领取
- if (userTaskStatus.getIsRewardClaimed()) {
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "reward_issued");
- response.put("message", "任务奖励已被领取");
- return response;
- }
-
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "reward_not_issued");
- response.put("message", "任务奖励未被领取");
- return response;
- }
-}
diff --git a/src/main/java/com/example/myproject/service/TorrentService.java b/src/main/java/com/example/myproject/service/TorrentService.java
new file mode 100644
index 0000000..941804d
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/TorrentService.java
@@ -0,0 +1,37 @@
+package com.example.myproject.service;
+
+import com.example.myproject.common.base.Result;
+import com.example.myproject.entity.TorrentEntity;
+import com.example.myproject.dto.param.TorrentParam;
+import com.example.myproject.dto.param.TorrentUploadParam;
+import com.example.myproject.dto.TorrentUpdateDTO;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.util.List;
+
+public interface TorrentService {
+ List<TorrentEntity> search(TorrentParam param);
+
+ TorrentEntity selectBySeedId(Long seedId);
+
+ void uploadTorrent(MultipartFile file, TorrentUploadParam param) throws IOException;
+
+ byte[] fetch(Long seedId, String passkey) throws IOException;
+
+ Result favorite(Long seedId, Long userId);
+
+ void deleteTorrent(Long seedId);
+
+ void updateTorrent(Long seedId, TorrentUpdateDTO updateDTO);
+
+ boolean canUserDeleteTorrent(Long seedId, Long userId);
+
+ boolean canUserUpdateTorrent(Long seedId, Long userId);
+
+ boolean checkUserUploadRatio(Long userId);
+
+ double calculateDownloadSize(Long torrentId, Long userId);
+
+ void recordDownload(Long torrentId, Long userId, double downloadSize);
+}
diff --git a/src/main/java/com/example/myproject/service/UserDetailsService.java b/src/main/java/com/example/myproject/service/UserDetailsService.java
new file mode 100644
index 0000000..85d4da2
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/UserDetailsService.java
@@ -0,0 +1,4 @@
+package com.example.myproject.service;
+
+public class UserDetailsService {
+}
diff --git a/src/main/java/com/example/myproject/service/UserFollowService.java b/src/main/java/com/example/myproject/service/UserFollowService.java
deleted file mode 100644
index 6222b48..0000000
--- a/src/main/java/com/example/myproject/service/UserFollowService.java
+++ /dev/null
@@ -1,122 +0,0 @@
-package com.example.myproject.service;
-
-import com.example.myproject.entity.Users;
-import com.example.myproject.entity.UserFollow;
-import com.example.myproject.repository.UserFollowRepository;
-import com.example.myproject.repository.UserRepository;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-import java.util.*;
-
-@Service
-public class UserFollowService {
-
- @Autowired
- private UserFollowRepository userFollowRepository;
-
- @Autowired
- private UserRepository userRepository;
-
- // 用户关注操作
- public Map<String, Object> follow(Long followerId, Long followedId) {
- if (followerId.equals(followedId)) {
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "error");
- response.put("message", "不能关注自己");
- return response;
- }
-
- // 检查是否已关注该用户
- UserFollow existingFollow = userFollowRepository.findByFollowerIdAndFollowedId(followerId, followedId);
- if (existingFollow == null) {
- // 创建新的关注记录
- UserFollow userFollow = new UserFollow();
- userFollow.setFollowerId(followerId);
- userFollow.setFollowedId(followedId);
- userFollow.setFollowTime(new java.util.Date());
-
- userFollowRepository.save(userFollow);
-
- // 返回关注成功响应
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "success");
- response.put("message", "关注成功");
- return response;
- } else {
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "error");
- response.put("message", "已经关注该用户");
- return response;
- }
- }
-
- // 取消关注操作
- public Map<String, Object> unfollow(Long followerId, Long followedId) {
- UserFollow userFollow = userFollowRepository.findByFollowerIdAndFollowedId(followerId, followedId);
-
- if (userFollow != null) {
- userFollowRepository.delete(userFollow);
-
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "success");
- response.put("message", "取消关注成功");
- return response;
- } else {
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "error");
- response.put("message", "未找到关注关系");
- return response;
- }
- }
-
- // 获取某个用户的所有粉丝
- public Map<String, Object> getFollowers(Long userId) {
- List<UserFollow> followers = userFollowRepository.findByFollowedId(userId);
-
- List<Map<String, Object>> followersList = new ArrayList<>();
- for (UserFollow follow : followers) {
- // 获取粉丝的用户信息
- Users follower = userRepository.findById(follow.getFollowerId()).orElse(null);
- if (follower != null) {
- Map<String, Object> followerInfo = new LinkedHashMap<>();
- followerInfo.put("user_id", follower.getUserId());
- followerInfo.put("username", follower.getUsername());
- followerInfo.put("avatar_url", follower.getAvatarUrl());
- followersList.add(followerInfo);
- }
- }
-
- // 返回响应数据
- Map<String, Object> response = new LinkedHashMap<>();
- response.put("status", "success");
- response.put("total", followersList.size());
- response.put("followers", followersList);
-
- return response;
- }
-
- // 获取某个用户的所有关注用户
- public Map<String, Object> getFollowing(Long userId) {
- List<UserFollow> following = userFollowRepository.findByFollowerId(userId);
-
- List<Map<String, Object>> followingList = new ArrayList<>();
- for (UserFollow follow : following) {
- // 获取被关注的用户信息
- Users followedUser = userRepository.findById(follow.getFollowedId()).orElse(null);
- if (followedUser != null) {
- Map<String, Object> followingInfo = new HashMap<>();
- followingInfo.put("user_id", followedUser.getUserId());
- followingInfo.put("username", followedUser.getUsername());
- followingInfo.put("avatar_url", followedUser.getAvatarUrl());
- followingList.add(followingInfo);
- }
- }
- Map<String, Object> response = new HashMap<>();
- response.put("status", "success");
- response.put("total", followingList.size());
- response.put("following", followingList);
-
- return response;
- }
-}
diff --git a/src/main/java/com/example/myproject/service/UserMessageService.java b/src/main/java/com/example/myproject/service/UserMessageService.java
deleted file mode 100644
index 05f62e7..0000000
--- a/src/main/java/com/example/myproject/service/UserMessageService.java
+++ /dev/null
@@ -1,112 +0,0 @@
-package com.example.myproject.service;
-
-import com.example.myproject.entity.UserMessages;
-import com.example.myproject.repository.UserMessagesRepository;
-import com.example.myproject.repository.UserRepository;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import java.util.Optional;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Date;
-import java.util.stream.Collectors;
-
-@Service
-public class UserMessageService {
-
- @Autowired
- private UserMessagesRepository userMessagesRepository;
-
- @Autowired
- private UserRepository userRepository;
-
- // 发送私信
- public Map<String, Object> sendMessage(Long senderId, Long receiverId, String content) {
- UserMessages message = new UserMessages();
- message.setSenderId(senderId);
- message.setReceiverId(receiverId);
- message.setContent(content);
- message.setTime(new Date());
- UserMessages savedMessage = userMessagesRepository.save(message);
-
- // 返回响应
- Map<String, Object> response = new HashMap<>();
- response.put("status", "success");
- response.put("message", "私信发送成功");
- response.put("message_id", savedMessage.getMessageId());
- return response;
- }
-
- // 获取当前用户的所有消息
- public Map<String, Object> getUserMessages(Long userId) {
- // 查询当前用户发送和接收的所有消息
- List<UserMessages> messages = userMessagesRepository.findBySenderIdOrReceiverId(userId, userId);
-
- // 获取发送者和接收者的用户名
- List<Map<String, Object>> messageDetails = messages.stream()
- .map(message -> {
- Map<String, Object> messageMap = new HashMap<>();
- messageMap.put("message_id", message.getMessageId());
- messageMap.put("sender_id", message.getSenderId());
- messageMap.put("receiver_id", message.getReceiverId());
- messageMap.put("content", message.getContent());
- messageMap.put("timestamp", message.getTime());
- String senderUsername = userRepository.findById(message.getSenderId())
- .map(user -> user.getUsername())
- .orElse("Unknown");
- messageMap.put("sender_username", senderUsername);
- String receiverUsername = userRepository.findById(message.getReceiverId())
- .map(user -> user.getUsername())
- .orElse("Unknown");
- messageMap.put("receiver_username", receiverUsername);
-
- return messageMap;
- })
- .collect(Collectors.toList());
-
- Map<String, Object> response = new HashMap<>();
- response.put("status", "success");
- response.put("messages", messageDetails);
- return response;
- }
-
- // 获取单条消息的详情
- public Map<String, Object> getMessage(Long messageId) {
- Optional<UserMessages> optionalMessage = userMessagesRepository.findById(messageId);
-
- if (optionalMessage.isPresent()) {
- UserMessages message = optionalMessage.get();
-
- // 获取发送者和接收者的用户名
- String senderUsername = userRepository.findById(message.getSenderId())
- .map(user -> user.getUsername())
- .orElse("Unknown");
- String receiverUsername = userRepository.findById(message.getReceiverId())
- .map(user -> user.getUsername())
- .orElse("Unknown");
-
- // 构建响应数据
- Map<String, Object> response = new HashMap<>();
- Map<String, Object> messageDetails = new HashMap<>();
- messageDetails.put("message_id", message.getMessageId());
- messageDetails.put("sender_id", message.getSenderId());
- messageDetails.put("sender_username", senderUsername);
- messageDetails.put("receiver_id", message.getReceiverId());
- messageDetails.put("receiver_username", receiverUsername);
- messageDetails.put("content", message.getContent());
- messageDetails.put("timestamp", message.getTime());
-
- response.put("status", "success");
- response.put("message", messageDetails);
- return response;
- } else {
- Map<String, Object> errorResponse = new HashMap<>();
- errorResponse.put("status", "error");
- errorResponse.put("message", "Message not found");
- return errorResponse;
- }
- }
-
-
-}
diff --git a/src/main/java/com/example/myproject/service/UserService.java b/src/main/java/com/example/myproject/service/UserService.java
index 0d37854..71435c7 100644
--- a/src/main/java/com/example/myproject/service/UserService.java
+++ b/src/main/java/com/example/myproject/service/UserService.java
@@ -1,269 +1,25 @@
package com.example.myproject.service;
-import com.example.myproject.entity.Users;
-import com.example.myproject.entity.UserInviteCode;
-import com.example.myproject.repository.UserRepository;
-import com.example.myproject.repository.UserInviteCodeRepository;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-import java.util.*;
-
-@Service
-public class UserService {
-
- @Autowired
- private UserRepository userRepository;
-
- @Autowired
- private UserInviteCodeRepository userInviteCodeRepository;
-
- // 生成邀请码
- public Map<String, Object> generateInviteCode(Long userId) {
- // 获取用户信息
- Users user = userRepository.findById(userId).orElse(null);
-
- // 如果用户不存在,返回错误
- if (user == null) {
- Map<String, Object> errorResponse = new HashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "用户不存在");
- return errorResponse;
- }
-
- // 检查用户的邀请数量
- if (user.getInviteCount() <= 0) {
- Map<String, Object> errorResponse = new HashMap<>();
- errorResponse.put("status", "failure");
- errorResponse.put("message", "没有剩余的邀请码");
- return errorResponse;
- }
-
- // 生成唯一的邀请码
- String inviteCode = generateUniqueInviteCode();
-
- // 将邀请码保存到 `user_invite_code` 表
- UserInviteCode userInviteCode = new UserInviteCode();
- userInviteCode.setUserId(userId);
- userInviteCode.setInviteCode(inviteCode);
- userInviteCode.setCreatedAt(java.time.LocalDateTime.now());
-
- userInviteCodeRepository.save(userInviteCode);
-
- // 更新用户的 `invite_count`,减少1
- user.setInviteCount(user.getInviteCount() - 1);
- userRepository.save(user);
-
- // 返回成功信息
- Map<String, Object> response = new HashMap<>();
- response.put("status", "success");
- response.put("message", "邀请码生成成功");
- response.put("invite_code", inviteCode);
-
- return response;
- }
-
- // 生成唯一的邀请码,使用26个字母(大小写)
- private String generateUniqueInviteCode() {
- String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- StringBuilder inviteCode = new StringBuilder();
-
- Random random = new Random();
- for (int i = 0; i < 10; i++) {
- inviteCode.append(characters.charAt(random.nextInt(characters.length())));
- }
-
- return inviteCode.toString();
- }
-
- public String registerUser(String username, String email, String password, String role, String inviteCode) {
- // 检查邮箱是否已经注册
- Optional<Users> existingEmailUser = userRepository.findByEmail(email);
- if (existingEmailUser.isPresent()) {
- return "该邮箱已被注册";
- }
-
- // 检查用户名是否已经存在
- Optional<Users> existingUsernameUser = userRepository.findByUsername(username); // 需要根据用户名查询
- if (existingUsernameUser.isPresent()) {
- return "该用户名已被注册";
- }
-
- // 检查邀请码是否有效
- if (inviteCode == null || inviteCode.isEmpty()) {
- return "邀请码不能为空";
- }
-
- Optional<UserInviteCode> invite = userInviteCodeRepository.findByInviteCode(inviteCode);
- if (invite.isEmpty() || invite.get().getIsUsed()) {
- return "邀请码无效或已被使用";
- }
-
- // 设置默认等级为2(由于邀请码有效)
- Long level = 2L;
-
- // 设置默认头像 URL
- String avatarUrl = "https://example.com/default-avatar.jpg"; // 默认头像
-
- // 获取邀请码对应的用户ID
- UserInviteCode inviteEntity = invite.get();
- Long inviteUserId = inviteEntity.getUserId();
-
- // 创建新用户
- Users newUser = new Users();
- newUser.setUsername(username);
- newUser.setEmail(email);
- newUser.setPassword(password);
- newUser.setRole(role);
- newUser.setInviteCount(0); // 初始邀请码数量为 0
- newUser.setLevel(level);
- newUser.setAvatarUrl(avatarUrl); // 设置默认头像
- newUser.setRegistrationDate(new java.util.Date()); // 设置注册日期
- newUser.setCurrentExperience(0); // 默认经验为 0
- newUser.setCurrentSeedingHours(0f); // 默认做种时长为 0
- newUser.setRegistrationTime(new java.util.Date()); // 设置注册时间为当前时间
-
- // 保存用户信息
- userRepository.save(newUser);
-
- // 更新邀请码的使用状态
- inviteEntity.setIsUsed(true);
- userInviteCodeRepository.save(inviteEntity);
-
- return "用户注册成功";
- }
- public String loginUser(String username, String password) {
- // 检查用户是否存在
- Optional<Users> userOptional = userRepository.findByUsername(username);
- if (userOptional.isEmpty()) {
- return "用户名不存在";
- }
+import com.baomidou.mybatisplus.extension.service.IService;
- Users user = userOptional.get();
+import com.example.myproject.entity.User;
- // 检查密码是否正确
- if (!user.getPassword().equals(password)) {
- return "密码错误";
- }
-
- // 登录成功
- return "登录成功";
- }
-
- public String changePassword(Long userId, String oldPassword, String newPassword, String confirmPassword) {
- // 查找用户
- Users user = userRepository.findById(userId).orElse(null);
-
- if (user == null) {
- return "用户不存在";
- }
-
- // 检查旧密码是否正确
- if (!user.getPassword().equals(oldPassword)) {
- return "旧密码错误";
- }
-
- // 检查新密码和确认密码是否一致
- if (!newPassword.equals(confirmPassword)) {
- return "新密码与确认密码不一致";
- }
-
- // 更新密码
- user.setPassword(newPassword);
- userRepository.save(user);
-
- return "密码修改成功";
- }
-
- // 获取用户个人资料
- public Map<String, Object> getProfile(Long userId) {
- Optional<Users> userOptional = userRepository.findById(userId);
-
- // 如果用户不存在,返回null或者可以抛出异常
- if (userOptional.isEmpty()) {
- return null; // 可以返回 null 或者根据需要返回错误信息
- }
-
- Users user = userOptional.get();
-
- // 将需要的字段放入 Map 中
- Map<String, Object> profile = new LinkedHashMap<>();
- profile.put("avatarUrl", user.getAvatarUrl());
- profile.put("username", user.getUsername());
- profile.put("email", user.getEmail());
- profile.put("gender", user.getGender());
- profile.put("description", user.getDescription());
- profile.put("hobbies", user.getHobbies());
- profile.put("level", user.getLevel());
- profile.put("Experience", user.getCurrentExperience());
- profile.put("uploadCount", user.getUploadCount());
- profile.put("downloadCount", user.getDownloadCount());
- profile.put("shareRate", user.getShareRate());
- profile.put("registrationTime", user.getRegistrationTime());
-
- return profile;
- }
-
- // 修改用户个人资料
- public boolean editProfile(Long userId, String avatarUrl, String nickname, String gender, String description, String hobbies) {
- Optional<Users> userOptional = userRepository.findById(userId);
-
- // 如果用户不存在,返回false
- if (userOptional.isEmpty()) {
- return false; // 用户不存在
- }
-
- Users user = userOptional.get();
-
- // 更新用户资料,只有传入值才会更新对应字段
- if (avatarUrl != null) {
- user.setAvatarUrl(avatarUrl);
- }
- if (nickname != null) {
- user.setUsername(nickname);
- }
- if (gender != null) {
- user.setGender(gender);
- }
- if (description != null) {
- user.setDescription(description);
- }
- if (hobbies != null) {
- user.setHobbies(hobbies);
- }
-
- // 保存更新后的用户信息
- userRepository.save(user);
-
- return true; // 更新成功
- }
-
- public Map<String, Object> calculateShareRate(Long userId) {
- // 查找用户
- Users user = userRepository.findById(userId).orElse(null);
- if (user == null) {
- return Map.of("status", "error", "message", "用户不存在");
- }
-
- // 获取上传量和下载量
- Float uploadCount = user.getUploadCount();
- Float downloadCount = user.getDownloadCount();
-
- // 计算分享率
- Float shareRate = 0f; // 默认分享率为0
- if (downloadCount != 0) {
- shareRate = uploadCount / downloadCount; // 分享率 = 上传量 / 下载量
- }
-
- // 更新用户的分享率
- user.setShareRate(shareRate);
- userRepository.save(user);
-
- // 返回结果
- return Map.of("status", "success", "message", "分享率计算成功", "shareRate", shareRate);
- }
+public interface UserService extends IService<User> {
+ User loginService(String username, String password);
-}
+ boolean preRegisterUser(User user);
+
+
+ boolean verifyEmail(String email, String token);
+
+
+ boolean checkEmailExists(String email);
+
+ boolean checkPassword(Long userId, String rawPassword);
+
+
+// Integer getUserId();
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/EmailServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/EmailServiceImpl.java
new file mode 100644
index 0000000..83500ea
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/serviceImpl/EmailServiceImpl.java
@@ -0,0 +1,51 @@
+
+package com.example.myproject.service.serviceImpl;
+import com.example.myproject.mapper.VerificationTokenMapper;
+import com.example.myproject.entity.VerificationToken;
+import com.example.myproject.service.EmailService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.mail.javamail.JavaMailSender;
+import org.springframework.mail.javamail.MimeMessageHelper;
+import org.springframework.stereotype.Service;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import java.time.Instant;
+@Service
+public class EmailServiceImpl implements EmailService {
+ private static final Logger log = LoggerFactory.getLogger(EmailServiceImpl.class);
+ @Autowired
+ private JavaMailSender mailSender;
+ @Autowired
+ private VerificationTokenMapper verificationTokenMapper;
+ @Value("${spring.mail.username}") // 从配置文件中注入发件人邮箱地址
+ private String fromEmail;
+ @Override
+ public boolean sendEmail(String to, String subject, String text) {
+ log.debug("开始发送验证邮件到:{},主题:{}", to, subject);
+ try {
+ MimeMessage message = mailSender.createMimeMessage();
+ MimeMessageHelper helper = new MimeMessageHelper(message, true);
+ helper.setFrom(fromEmail);
+ helper.setTo(to);
+ helper.setSubject(subject);
+ helper.setText(text, true);
+ mailSender.send(message);
+ log.info("邮件成功发送到:{}", to);
+ return true;
+ } catch (MessagingException e) {
+ log.error("发送邮件失败,收件人:{},错误信息:{}", to, e.getMessage(), e);
+ return false;
+ }
+ }
+ @Override
+ public void sendVerificationEmail(String email, String token){
+ // 发送邮件
+ String message = "<h1>Email Verification</h1>" +
+ "<p>Your verification code is: <b>" + token + "</b></p>" +
+ "<p>This code will expire in 1 hour.</p>";
+ sendEmail(email, "Verify Your Email", message);
+ }
+}
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/PromotionServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/PromotionServiceImpl.java
new file mode 100644
index 0000000..9d34cbc
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/serviceImpl/PromotionServiceImpl.java
@@ -0,0 +1,114 @@
+package com.example.myproject.service.serviceImpl;
+
+import com.example.myproject.entity.Promotion;
+import com.example.myproject.mapper.PromotionMapper;
+import com.example.myproject.service.PromotionService;
+import com.example.myproject.dto.PromotionCreateDTO;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.time.LocalDateTime;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@Service
+public class PromotionServiceImpl implements PromotionService {
+
+ @Autowired
+ private PromotionMapper promotionMapper;
+
+ @Override
+ @Transactional
+ public Promotion createPromotion(PromotionCreateDTO promotionDTO) {
+ // 验证时间
+ LocalDateTime now = LocalDateTime.now();
+ if (promotionDTO.getEndTime().isBefore(now)) {
+ throw new RuntimeException("结束时间不能早于当前时间");
+ }
+
+ // 验证种子ID是否存在
+ validateTorrentIds(promotionDTO.getApplicableTorrentIds());
+
+ // 创建促销活动
+ Promotion promotion = new Promotion();
+ promotion.setName(promotionDTO.getName());
+ promotion.setDescription(promotionDTO.getDescription());
+ promotion.setStartTime(promotionDTO.getStartTime());
+ promotion.setEndTime(promotionDTO.getEndTime());
+ promotion.setDiscountPercentage(promotionDTO.getDiscountPercentage());
+
+ // 把List<Long>转换成逗号分隔字符串
+ String applicableTorrentIdsStr = promotionDTO.getApplicableTorrentIds().stream()
+ .map(String::valueOf)
+ .collect(Collectors.joining(","));
+ promotion.setApplicableTorrentIds(applicableTorrentIdsStr);
+
+ promotion.setCreateTime(now);
+ promotion.setUpdateTime(now);
+ promotion.setIsDeleted(false);
+
+ promotionMapper.insert(promotion);
+ return promotion;
+ }
+
+ @Override
+ public List<Promotion> getAllActivePromotions() {
+ LocalDateTime now = LocalDateTime.now();
+ return promotionMapper.findActivePromotions(now);
+ }
+
+ @Override
+ public Promotion getPromotionById(Long promotionId) {
+ Promotion promotion = promotionMapper.selectById(promotionId);
+ if (promotion == null || promotion.getIsDeleted()) {
+ return null;
+ }
+ return promotion;
+ }
+
+ @Override
+ @Transactional
+ public void deletePromotion(Long promotionId) {
+ Promotion promotion = getPromotionById(promotionId);
+ if (promotion == null) {
+ throw new RuntimeException("促销活动不存在");
+ }
+
+ // 软删除
+ promotion.setIsDeleted(true);
+ promotion.setUpdateTime(LocalDateTime.now());
+ promotionMapper.updateById(promotion);
+ }
+
+ @Override
+ public double getCurrentDiscount(Long torrentId) {
+ LocalDateTime now = LocalDateTime.now();
+ List<Promotion> activePromotions = promotionMapper.findActivePromotionsForTorrent(torrentId, now);
+
+ // 如果有多个促销活动,取折扣最大的
+ return activePromotions.stream()
+ .mapToDouble(Promotion::getDiscountPercentage)
+ .max()
+ .orElse(0.0);
+ }
+
+ /**
+ * 验证种子ID是否存在
+ */
+ private void validateTorrentIds(List<Long> torrentIds) {
+ if (torrentIds == null || torrentIds.isEmpty()) {
+ throw new RuntimeException("适用种子列表不能为空");
+ }
+
+ // 检查所有种子ID是否都存在
+ List<Long> invalidIds = torrentIds.stream()
+ .filter(id -> promotionMapper.checkTorrentExists(id) == 0) // 改成 == 0
+ .collect(Collectors.toList());
+
+ if (!invalidIds.isEmpty()) {
+ throw new RuntimeException("以下种子ID不存在: " + invalidIds);
+ }
+ }
+}
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java
new file mode 100644
index 0000000..884c65d
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java
@@ -0,0 +1,296 @@
+package com.example.myproject.service.serviceImpl;
+
+import com.example.myproject.entity.TorrentEntity;
+import com.example.myproject.entity.User;
+import com.example.myproject.mapper.TorrentMapper;
+import com.example.myproject.mapper.UserMapper;
+import com.example.myproject.service.TorrentService;
+import com.example.myproject.service.PromotionService;
+import com.example.myproject.dto.param.TorrentParam;
+import com.example.myproject.dto.param.TorrentUploadParam;
+import com.example.myproject.dto.TorrentUpdateDTO;
+import com.turn.ttorrent.bcodec.BDecoder;
+import com.turn.ttorrent.bcodec.BEValue;
+import com.turn.ttorrent.bcodec.BEncoder;
+import com.turn.ttorrent.client.SimpleClient;
+import com.turn.ttorrent.common.creation.MetadataBuilder;
+import com.turn.ttorrent.tracker.Tracker;
+import com.turn.ttorrent.tracker.TrackedTorrent;
+import com.example.myproject.common.base.Result;
+
+import com.turn.ttorrent.tracker.TrackedTorrent;
+import org.apache.commons.codec.binary.Hex;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.time.LocalTime;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+@Service
+public class TorrentServiceImpl implements TorrentService {
+ @Autowired
+ private Tracker tracker;
+
+ @Autowired
+ private TorrentMapper torrentMapper;
+
+ private final Map<String, TrackedTorrent> torrentRegistry = new HashMap<>();
+
+
+ @Autowired
+ private UserMapper userMapper;
+
+ @Autowired
+ private PromotionService promotionService;
+
+ private static final double MIN_UPLOAD_RATIO = 0.5; // 最小上传比例要求
+
+ @Override
+ public List<TorrentEntity> search(TorrentParam param) {
+ return torrentMapper.search(param);
+ }
+
+ @Override
+ public TorrentEntity selectBySeedId(Long seedId) {
+ return torrentMapper.selectById(seedId);
+ }
+ private final ExecutorService seederExecutor = Executors.newCachedThreadPool();
+
+ @Override
+ @Transactional
+ public void uploadTorrent(MultipartFile file, TorrentUploadParam param) throws IOException {
+ // 验证用户权限
+ User user = userMapper.selectById(param.getUploader());
+ if (user == null) {
+ throw new RuntimeException("用户不存在");
+ }
+ String workingDir = System.getProperty("user.dir");
+ Path originalDir = Paths.get(workingDir, "data", "files");
+ Files.createDirectories(originalDir);
+ Path originalFilePath = originalDir.resolve(file.getOriginalFilename());
+ Files.copy(file.getInputStream(), originalFilePath, StandardCopyOption.REPLACE_EXISTING);
+
+ MetadataBuilder builder = new MetadataBuilder()
+ .addFile(originalFilePath.toFile(), file.getOriginalFilename()) // 添加原始文件
+ .setTracker(" ") // 设置Tracker地址
+ .setPieceLength(512 * 1024) // 分片大小512KB
+ .setComment("Generated by PT站")
+ .setCreatedBy("PT-Server");
+
+ // 处理种子文件
+ byte[] torrentBytes = file.getBytes();
+ String infoHash = null;
+ try {
+ infoHash = calculateInfoHash(torrentBytes);
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException(e);
+ }
+
+ // 保存种子文件到data/torrents目录
+ Path torrentDir = Paths.get(workingDir, "data", "torrents");
+ Files.createDirectories(torrentDir);
+ Path torrentPath = torrentDir.resolve(infoHash + ".torrent");
+ Files.copy(new ByteArrayInputStream(torrentBytes), torrentPath, StandardCopyOption.REPLACE_EXISTING);
+
+ // 注册到Tracker
+ TrackedTorrent torrent = TrackedTorrent.load(torrentPath.toFile());
+ tracker.announce(torrent);
+
+
+ // 异步启动做种客户端
+ seederExecutor.submit(() -> {
+ try {
+ startSeeding(torrentPath, originalDir);
+ } catch (Exception e) {
+ Result.error("做种失败: " + e.getMessage());
+ }
+ });
+
+
+
+
+ // 保存种子信息
+ TorrentEntity entity= new TorrentEntity();
+ entity.setUploader(param.getUploader());
+ entity.setFileName(file.getOriginalFilename());
+ entity.setSize(file.getSize());
+ entity.setCategory(param.getCategory());
+ entity.setTags(param.getTags());
+ entity.setTitle(param.getTitle());
+ entity.setImageUrl(param.getImageUrl());
+ entity.setTorrentFile(torrentBytes);
+ entity.setInfoHash(infoHash);
+
+ torrentMapper.insert(entity);
+ }
+
+ @Override
+ public byte[] fetch(Long seedId, String passkey) {
+ TorrentEntity torrent = selectBySeedId(seedId);
+ if (torrent == null) {
+ throw new RuntimeException("种子不存在");
+ }
+
+ byte[] torrentBytes = torrent.getTorrentFile();
+
+ try {
+ // 1. 解码 .torrent 文件为 Map
+ Map<String, BEValue> decoded = BDecoder.bdecode(new ByteArrayInputStream(torrentBytes)).getMap();
+
+ // 2. 获取原始 announce 字段
+ String announce = decoded.get("announce").getString();
+
+ // 3. 注入 passkey 到 announce URL
+ if (!announce.contains("passkey=")) {
+ announce = announce + "?passkey=" + passkey;
+ decoded.put("announce", new BEValue(announce));
+ }
+
+ // 4. 编码成新的 .torrent 文件字节数组
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ BEncoder.bencode(decoded, out);
+ return out.toByteArray();
+
+ } catch (Exception e) {
+ throw new RuntimeException("处理 torrent 文件失败", e);
+ }
+ }
+
+ @Override
+ @Transactional
+ public Result favorite(Long seedId, Long userId) {
+ try {
+ boolean exists = torrentMapper.checkFavorite(seedId, userId);
+ if (exists) {
+ torrentMapper.removeFavorite(seedId, userId);
+ return Result.success("取消收藏成功");
+ } else {
+ torrentMapper.addFavorite(seedId, userId);
+ return Result.success("收藏成功");
+ }
+ } catch (Exception e) {
+ return Result.error("失败: " + e.getMessage());
+ }
+ }
+
+ @Override
+ @Transactional
+ public void deleteTorrent(Long seedId) {
+ torrentMapper.deleteById(seedId);
+ }
+
+ @Override
+ @Transactional
+ public void updateTorrent(Long seedId, TorrentUpdateDTO updateDTO) {
+ TorrentEntity torrent = selectBySeedId(seedId);
+ if (torrent == null) {
+ throw new RuntimeException("种子不存在");
+ }
+
+
+ torrent.setDescription(updateDTO.getDescription());
+ torrent.setCategory(updateDTO.getCategory());
+ torrent.setTitle(updateDTO.getTitle());
+ torrent.setTags(updateDTO.getTags());
+ torrent.setImageUrl(updateDTO.getImageUrl());
+
+ torrentMapper.updateById(torrent);
+ }
+
+ @Override
+ public boolean canUserDeleteTorrent(Long seedId, Long userId) {
+ TorrentEntity torrent = selectBySeedId(seedId);
+ if (torrent == null) {
+ return false;
+ }
+
+ // 检查是否是种子发布者或管理员
+ return torrent.getUploader().equals(userId) ||
+ userMapper.hasRole(userId, "admin");
+ }
+
+ @Override
+ public boolean canUserUpdateTorrent(Long seedId, Long userId) {
+ return canUserDeleteTorrent(seedId, userId);
+ }
+
+ @Override
+ public boolean checkUserUploadRatio(Long userId) {
+ User user = userMapper.selectById(userId);
+ if (user == null) {
+ return false;
+ }
+
+ // 防止除以零
+ if (user.getDownloaded() == 0) {
+ return true;
+ }
+
+ double uploadRatio = user.getUploaded() / (double) user.getDownloaded();
+ return uploadRatio >= MIN_UPLOAD_RATIO;
+ }
+ /**
+ * 启动做种客户端
+ */
+ private void startSeeding(Path torrentPath, Path dataDir) throws Exception {
+ SimpleClient seederClient = new SimpleClient();
+ seederClient.downloadTorrent(
+ torrentPath.toString(),
+ dataDir.toString(),
+ InetAddress.getLocalHost());
+ // 保持做种状态(阻塞线程)
+ while (true) {
+ Thread.sleep(60000); // 每60秒检查一次
+ }
+ }
+
+
+ @Override
+ public double calculateDownloadSize(Long torrentId, Long userId) {
+ TorrentEntity torrent = selectBySeedId(torrentId);
+ if (torrent == null) {
+ throw new RuntimeException("种子不存在");
+ }
+
+ // 获取当前有效的促销活动
+ double discount = promotionService.getCurrentDiscount(torrentId);
+
+ // 计算实际下载量
+ return torrent.getSize() * (1 - discount / 100.0);
+ }
+
+ @Override
+ @Transactional
+ public void recordDownload(Long torrentId, Long userId, double downloadSize) {
+ // 更新用户下载量
+ userMapper.increaseDownloaded(userId, downloadSize);
+
+ // 更新种子下载次数
+ torrentMapper.increaseDownloads(torrentId);
+ }
+ /**
+ * 计算种子文件的infoHash
+ */
+ private String calculateInfoHash(byte[] torrentData) throws NoSuchAlgorithmException {
+ MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
+ sha1.update(torrentData);
+ byte[] hashBytes = sha1.digest();
+ return Hex.encodeHexString(hashBytes);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/UserDetailsServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/UserDetailsServiceImpl.java
new file mode 100644
index 0000000..4bbd5c3
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/serviceImpl/UserDetailsServiceImpl.java
@@ -0,0 +1,47 @@
+package com.example.myproject.service.serviceImpl;
+
+import com.example.myproject.mapper.UserMapper;
+import com.example.myproject.entity.User;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.authority.AuthorityUtils;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class UserDetailsServiceImpl implements UserDetailsService {
+
+ @Autowired
+ private UserMapper userMapper;
+
+ @Override
+ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
+ //username参数,是在登陆时,用户传递的表单数据username
+ //主要读取数据库3个值 username password authorities
+ User user= userMapper.selectByUsername(username);
+ if (user == null) {
+ throw new UsernameNotFoundException("用户名未找到");
+ }
+
+ String authorityName = user.getRole();
+ //为了返回一个UserDetails 使用User
+ List<GrantedAuthority> authorities = new ArrayList<>();
+ GrantedAuthority authority = new SimpleGrantedAuthority(authorityName);
+ authorities.add(authority);
+ return new org.springframework.security.core.userdetails.User(
+ user.getUsername(),
+ user.getPassword(),
+ true, // accountEnabled
+ true, // accountNonExpired
+ true, // credentialsNonExpired
+ true, // accountNonLocked
+ AuthorityUtils.createAuthorityList("ROLE_USER") // 设置用户的角色或权限
+ );
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/UserServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/UserServiceImpl.java
new file mode 100644
index 0000000..d0718fe
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/serviceImpl/UserServiceImpl.java
@@ -0,0 +1,122 @@
+
+package com.example.myproject.service.serviceImpl;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.example.myproject.mapper.UserMapper;
+import com.example.myproject.mapper.VerificationTokenMapper;
+import com.example.myproject.entity.User;
+import com.example.myproject.entity.VerificationToken;
+import com.example.myproject.service.EmailService;
+import com.example.myproject.service.UserService;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.stereotype.Service;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+//登录注册
+@Service
+public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
+ private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
+ @Autowired
+ private UserMapper userMapper; // Using MyBatis-Plus mapper
+ @Autowired
+ private EmailServiceImpl emailService;
+ @Autowired
+ private VerificationTokenMapper verificationTokenMapper; // Using MyBatis-Plus mapper
+ @Autowired
+ private PasswordEncoder passwordEncoder; // Injecting password encoder
+ @Override
+ public User loginService(String username, String password) {
+ log.debug("Attempting login with username: {}, password: {}", username, password);
+ User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username).eq("password", password));
+ if (user != null) {
+ user.setPassword(""); // Clear the password before returning
+ log.debug("Login successful, User ID: {}", user.getId());
+ } else {
+ log.debug("Login failed, incorrect username or password.");
+ }
+ return user;
+ }
+ @Override
+ public boolean preRegisterUser(User user) {
+ log.debug("Pre-registering user, username: {}, email: {}", user.getUsername(), user.getEmail());
+ // 检查用户名或邮箱是否已存在
+ boolean userExists = userMapper.selectOne(new QueryWrapper<User>().eq("username", user.getUsername())) != null ||
+ userMapper.selectOne(new QueryWrapper<User>().eq("email", user.getEmail())) != null;
+ if (userExists) {
+ log.debug("Pre-registration failed, username or email already exists.");
+ return false; // 用户名或邮箱已经存在
+ }
+ // 加密密码
+ String encryptedPassword = passwordEncoder.encode(user.getPassword());
+ // 生成验证码
+ String token = RandomStringUtils.randomNumeric(6);
+ // 设置过期时间为当前时间加一小时
+ Instant expiryDate = Instant.now().plus(1, ChronoUnit.HOURS);
+ // 创建验证令牌对象
+ VerificationToken verificationToken = new VerificationToken(
+ token,
+ user.getUsername(),
+ user.getEmail(),
+ encryptedPassword,
+ expiryDate
+ );
+ // 插入验证令牌
+ int rowsInserted = verificationTokenMapper.insert(verificationToken);
+ if (rowsInserted > 0) {
+ // 发送验证邮件
+ emailService.sendVerificationEmail(user.getEmail(), token);
+ log.debug("Pre-registration successful, verification code: {}, expiry date: {}", token, expiryDate);
+ return true; // 注册前验证成功
+ } else {
+ log.error("Failed to insert verification token into database.");
+ return false; // 如果插入验证令牌失败,返回失败
+ }
+ }
+ @Override
+ public boolean verifyEmail(String email, String token) {
+ log.debug("Verifying email, email: {}, token: {}", email, token);
+ VerificationToken verificationToken = verificationTokenMapper.selectOne(
+ new QueryWrapper<VerificationToken>().eq("token", token).eq("email", email)
+ );
+ if (verificationToken != null && !verificationToken.isExpired()) {
+ log.debug("Verification code is valid, username: {}", verificationToken.getUsername());
+ User user = userMapper.selectOne(new QueryWrapper<User>().eq("email", email));
+ if (user == null) {
+ user = new User();
+ user.setEmail(email);
+ user.setUsername(verificationToken.getUsername());
+ user.setPassword(verificationToken.getPassword());
+ user.setEmailVerified(true);
+ userMapper.insert(user); // Save new user
+ log.debug("New user created, User ID: {}", user.getId());
+ } else {
+ user.setEmailVerified(true);
+ userMapper.updateById(user); // Update existing user
+ log.debug("User email verified, User ID: {}", user.getId());
+ }
+ verificationTokenMapper.delete(new QueryWrapper<VerificationToken>().eq("token", token)); // Clean up the token
+ log.debug("Verification code deleted.");
+ return true;
+ }
+ log.debug("Verification code is invalid or expired.");
+ return false;
+ }
+ public boolean checkEmailExists(String email) {
+ log.debug("检查邮箱是否存在,邮箱:{}", email);
+ boolean exists = userMapper.selectCount(new QueryWrapper<User>().eq("email", email)) > 0;
+ log.debug("邮箱存在状态:{}", exists);
+ return exists;
+ }
+ public boolean checkPassword(Long userId, String password) {
+ User user = userMapper.selectById(userId);
+ if (user == null) {
+ throw new RuntimeException("用户不存在");
+ }
+ String encryptedPassword = user.getPassword();
+ return passwordEncoder.matches(password, encryptedPassword);
+ }
+}
diff --git a/src/main/java/com/example/myproject/utils/Result.java b/src/main/java/com/example/myproject/utils/Result.java
deleted file mode 100644
index e46f828..0000000
--- a/src/main/java/com/example/myproject/utils/Result.java
+++ /dev/null
@@ -1,131 +0,0 @@
-package com.example.myproject.utils;
-
-
-import org.springframework.http.ResponseEntity;
-
-public class Result<T> {
- private String code;
- private String msg;
- private T data;
-
- public String getCode() {
- return code;
- }
-
- public void setCode(String code) {
- this.code = code;
- }
-
- public String getMsg() {
- return msg;
- }
-
- public void setMsg(String msg) {
- this.msg = msg;
- }
-
- public T getData() {
- return data;
- }
-
- public void setData(T data) {
- this.data = data;
- }
-
- public Result() {
- }
-
- public Result(T data) {
- this.data = data;
- }
-
- public static Result success(ResponseEntity<?> resultMap, String 兴趣小组创建成功) {
- Result result = new Result<>();
- result.setCode("200");
- result.setMsg("成功");
- return result;
- }
-
- public static <T> Result<T> success(T data) {
- Result<T> result = new Result<>(data);
- result.setCode("200");
- result.setMsg("成功");
- return result;
- }
- public static <T> Result<T> success(String msg) {
- Result result = new Result();
- result.setCode("200");
- result.setMsg("成功");
- return result;
- }
-
- public static <T> Result<T> success(T data,String msg) {
- Result<T> result = new Result<>(data);
- result.setCode("200");
- result.setMsg(msg);
- return result;
- }
-
- public static Result error(String code, String msg) {
- Result result = new Result();
- result.setCode(code);
- result.setMsg(msg);
- return result;
- }
- /**
- * 创建一个表示文件大小超出限制的结果对象。
- *
- * @return 构造的文件过大错误结果对象
- */
- public static Result fileTooLarge() {
- Result result = new Result();
- result.setCode("413");
- result.setMsg("文件大小超出限制。");
- return result;
- }
-
- /**
- * 创建一个表示文件格式不支持的结果对象。
- *
- * @return 构造的文件格式错误结果对象
- */
- public static Result unsupportedFileType() {
- Result result = new Result();
- result.setCode("415");
- result.setMsg("不支持的文件格式。");
- return result;
- }
-
- /**
- * 创建一个表示文件未找到的结果对象。
- *
- * @return 构造的文件未找到错误结果对象
- */
- public static Result fileNotFound() {
- Result result = new Result();
- result.setCode("404");
- result.setMsg("文件未找到。");
- return result;
- }
-
- /**
- * 创建一个表示文件存储错误的结果对象。
- *
- * @param errorMsg 详细错误信息
- * @return 构造的文件存储错误结果对象
- */
- public static Result fileStorageError(String errorMsg) {
- Result result = new Result();
- result.setCode("500");
- result.setMsg("文件存储错误: " + errorMsg);
- return result;
- }
-
- public static Result permissionDenied() {
- Result result = new Result();
- result.setCode("401");
- result.setMsg("权限不足,无法执行该操作。");
- return result;
- }
-
-}
diff --git a/src/main/java/com/example/myproject/utils/VerifyCode.java b/src/main/java/com/example/myproject/utils/VerifyCode.java
new file mode 100644
index 0000000..689b8ce
--- /dev/null
+++ b/src/main/java/com/example/myproject/utils/VerifyCode.java
@@ -0,0 +1,156 @@
+package com.example.myproject.utils;
+
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Random;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Random;
+
+/*
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Random;
+
+/**
+ * @author 阿楠
+ * code生成工具类
+ */
+public class VerifyCode {
+ /**
+ * 生成验证码图片的宽度
+ */
+ private int width = 100;
+
+ /**
+ * 生成验证码图片的高度
+ */
+ private int height = 30;
+
+ /**
+ * 字符样式
+ */
+ private String[] fontNames = { "宋体", "楷体", "隶书", "微软雅黑" };
+
+ /**
+ * 定义验证码图片的背景颜色为白色
+ */
+ private Color bgColor = new Color(255, 255, 255);
+
+ /**
+ * 生成随机
+ */
+ private Random random = new Random();
+
+ /**
+ * 定义code字符
+ */
+ private String codes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+ /**
+ * 记录随机字符串
+ */
+ private String text;
+
+ /**
+ * 获取一个随意颜色
+ * @return
+ */
+ private Color randomColor() {
+ int red = random.nextInt(150);
+ int green = random.nextInt(150);
+ int blue = random.nextInt(150);
+ return new Color(red, green, blue);
+ }
+
+ /**
+ * 获取一个随机字体
+ *
+ * @return
+ */
+ private Font randomFont() {
+ String name = fontNames[random.nextInt(fontNames.length)];
+ int style = random.nextInt(4);
+ int size = random.nextInt(5) + 24;
+ return new Font(name, style, size);
+ }
+
+ /**
+ * 获取一个随机字符
+ *
+ * @return
+ */
+ private char randomChar() {
+ return codes.charAt(random.nextInt(codes.length()));
+ }
+
+ /**
+ * 创建一个空白的BufferedImage对象
+ *
+ * @return
+ */
+ private BufferedImage createImage() {
+ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+ Graphics2D g2 = (Graphics2D) image.getGraphics();
+ //设置验证码图片的背景颜色
+ g2.setColor(bgColor);
+ g2.fillRect(0, 0, width, height);
+ return image;
+ }
+
+ public BufferedImage getImage() {
+ BufferedImage image = createImage();
+ Graphics2D g2 = (Graphics2D) image.getGraphics();
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < 4; i++) {
+ String s = randomChar() + "";
+ sb.append(s);
+ g2.setColor(randomColor());
+ g2.setFont(randomFont());
+ float x = i * width * 1.0f / 4;
+ g2.drawString(s, x, height - 8);
+ }
+ this.text = sb.toString();
+ drawLine(image);
+ return image;
+ }
+
+ /**
+ * 绘制干扰线
+ *
+ * @param image
+ */
+ private void drawLine(BufferedImage image) {
+ Graphics2D g2 = (Graphics2D) image.getGraphics();
+ int num = 5;
+ for (int i = 0; i < num; i++) {
+ int x1 = random.nextInt(width);
+ int y1 = random.nextInt(height);
+ int x2 = random.nextInt(width);
+ int y2 = random.nextInt(height);
+ g2.setColor(randomColor());
+ g2.setStroke(new BasicStroke(1.5f));
+ g2.drawLine(x1, y1, x2, y2);
+ }
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public static void output(BufferedImage image, OutputStream out) throws IOException {
+ ImageIO.write(image, "JPEG", out);
+ }
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 80f468d..0590333 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1,12 +1,12 @@
server.port=8080
-spring.datasource.url=jdbc:mysql://localhost:3306/ptProject
+spring.datasource.url=jdbc:mysql://localhost:3306/pt
spring.datasource.username=root
-spring.datasource.password=123456
+spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
+
# Mail configuration
spring.mail.password=tljnebonhzhqecda
spring.mail.username=2370523716@qq.com
@@ -17,8 +17,15 @@
spring.jpa.enabled=false
-spring.jpa.hibernate.ddl-auto=update
+spring.jpa.hibernate.ddl-auto=none
spring.jpa.open-in-view=false
+# tracker??
+pt.tracker.port=6969
-mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml
\ No newline at end of file
+pt.tracker.torrent-dir=${user.dir}/data/torrents
+
+pt.tracker.allow-foreign=false
+pt.tracker.announce-url=/custom-announce
+
+mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml
diff --git a/src/main/resources/files/files.torrent b/src/main/resources/files/files.torrent
new file mode 100644
index 0000000..e04974f
--- /dev/null
+++ b/src/main/resources/files/files.torrent
@@ -0,0 +1 @@
+d8:announce22:https://tracker.byr.pt10:created by21:qBittorrent v4.5.3.1013:creation datei1747717901e4:infod5:filesld6:lengthi173e4:pathl13:valid.torrenteee4:name5:files12:piece lengthi16384e6:pieces20:/ñíèEô5ã<òûìÕQ¡ûee
\ No newline at end of file
diff --git a/src/main/resources/mapper/FavoriteMapper.xml b/src/main/resources/mapper/FavoriteMapper.xml
new file mode 100644
index 0000000..1048ec2
--- /dev/null
+++ b/src/main/resources/mapper/FavoriteMapper.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.example.myproject.mapper.FavoriteMapper">
+
+</mapper>
diff --git a/src/main/resources/mapper/PromotionMapper.xml b/src/main/resources/mapper/PromotionMapper.xml
new file mode 100644
index 0000000..72ffd95
--- /dev/null
+++ b/src/main/resources/mapper/PromotionMapper.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.example.myproject.mapper.PromotionMapper">
+
+ <insert id="insert" parameterType="com.example.myproject.entity.Promotion">
+ INSERT INTO promotion (
+ name, description, start_time, end_time, discount_percentage, applicable_torrent_ids
+ ) VALUES (
+ #{name}, #{description}, #{startTime}, #{endTime}, #{discountPercentage}, #{applicableTorrentIds}
+ )
+ </insert>
+
+</mapper>
diff --git a/src/main/resources/mapper/TorrentMapper.xml b/src/main/resources/mapper/TorrentMapper.xml
new file mode 100644
index 0000000..d5f018e
--- /dev/null
+++ b/src/main/resources/mapper/TorrentMapper.xml
@@ -0,0 +1,104 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.example.myproject.mapper.TorrentMapper">
+ <resultMap id="BaseResultMap" type="com.example.myproject.entity.TorrentEntity">
+ <id column="info_hash" property="infoHash"/>
+ <result column="file_name" property="fileName"/>
+ <result column="uploader" property="uploader"/>
+ <result column="upload_time" property="uploadTime"/>
+ <result column="size" property="size"/>
+ <result column="title" property="title"/>
+ <result column="description" property="description"/>
+ <result column="category" property="category"/>
+ <result column="image_url" property="imageUrl"/>
+ </resultMap>
+
+ <select id="selectByInfoHash" resultMap="BaseResultMap">
+ SELECT * FROM torrent WHERE info_hash = #{infoHash}
+ </select>
+ <select id="selectBySeedId" resultMap="BaseResultMap">
+ SELECT * FROM torrent WHERE seed_id = #{seedId}
+ </select>
+
+
+
+
+ <update id="update" parameterType="com.example.myproject.entity.TorrentEntity">
+ UPDATE torrent
+ SET file_name = #{fileName},
+ uploader = #{uploader},
+ upload_time = #{uploadTime},
+ size = #{size},
+ title = #{title},
+ description = #{description},
+ category = #{category},
+ image_url = #{imageUrl}
+ WHERE info_hash = #{infoHash}
+ </update>
+ <select id="search" resultType="com.example.myproject.entity.TorrentEntity">
+ SELECT * FROM torrent
+ <where>
+ <if test="param.category != null">
+ AND category = #{param.category}
+ </if>
+
+ <!-- <if test="param.free != null and param.free != ''">-->
+ <!-- AND free = #{param.free}-->
+ <!-- </if>-->
+ <if test="param.free != null">
+ <choose>
+ <!-- 筛选“正在促销中”的种子 -->
+ <when test="param.free == true">
+ AND EXISTS (
+ SELECT 1 FROM promotion p
+ WHERE
+ JSON_CONTAINS(p.applicable_torrent_ids, JSON_ARRAY(t.id))
+ AND NOW() BETWEEN p.start_time AND p.end_time
+ AND p.is_deleted = 0
+ )
+ </when>
+ <!-- 筛选“未在促销中”的种子 -->
+ <otherwise>
+ AND NOT EXISTS (
+ SELECT 1 FROM promotion p
+ WHERE
+ JSON_CONTAINS(p.applicable_torrent_ids, JSON_ARRAY(t.id))
+ AND NOW() BETWEEN p.start_time AND p.end_time
+ AND p.is_deleted = 0
+ )
+ </otherwise>
+ </choose>
+ </if>
+
+ <if test="param.likeExpressions != null and param.likeExpressions.size > 0">
+ AND (
+ <foreach collection="param.likeExpressions" item="item" open="(" separator=" AND " close=")">
+
+ ( title LIKE CONCAT('%', #{item}, '%') ) or ( description LIKE CONCAT('%', #{item}, '%') ) or ( tags LIKE CONCAT('%', #{item}, '%') )
+ </foreach>
+ )
+ </if>
+ </where>
+
+ <if test="param.prop != null and param.sort != null">
+ ORDER BY ${param.prop} ${param.sort}
+ </if>
+ </select>
+ <select id="checkFavorite" resultType="boolean">
+ SELECT COUNT(*) > 0
+ FROM favorite
+ WHERE seed_id = #{seedId} AND user_id = #{userId}
+ </select>
+ <insert id="addFavorite">
+ INSERT INTO favorite (seed_id, user_id)
+ VALUES (#{seedId}, #{userId})
+ </insert>
+ <delete id="removeFavorite">
+ DELETE FROM favorite
+ WHERE seed_id = #{seedId} AND user_id = #{userId}
+ </delete>
+
+
+</mapper>
\ No newline at end of file
diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml
new file mode 100644
index 0000000..f03ab0a
--- /dev/null
+++ b/src/main/resources/mapper/UserMapper.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.example.myproject.mapper.UserMapper">
+ <!-- 根据用户名查找用户 -->
+ <select id="selectByUsername" parameterType="string" resultType="com.example.myproject.entity.User">
+ SELECT * FROM user WHERE username = #{username}
+ </select>
+ <!-- 根据用户名和密码查找用户 -->
+ <select id="selectByUsernameAndPassword" parameterType="map" resultType="com.example.myproject.entity.User">
+ SELECT * FROM user WHERE username = #{username} AND password = #{password}
+ </select>
+ <!-- 根据邮箱查找用户 -->
+ <select id="selectByEmail" parameterType="string" resultType="com.example.myproject.entity.User">
+ SELECT * FROM user WHERE email = #{email}
+ </select>
+ <!-- 根据用户名包含查找用户 -->
+ <select id="selectByUsernameContaining" parameterType="string" resultType="com.example.myproject.entity.User">
+ SELECT * FROM user WHERE username LIKE CONCAT('%', #{name}, '%')
+ </select>
+</mapper>
diff --git a/src/main/resources/mapper/VerificationTokenMapper.xml b/src/main/resources/mapper/VerificationTokenMapper.xml
new file mode 100644
index 0000000..53b19a5
--- /dev/null
+++ b/src/main/resources/mapper/VerificationTokenMapper.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.example.myproject.mapper.VerificationTokenMapper">
+ <!-- 通过 token 和 email 查询 VerificationToken -->
+ <select id="findByTokenAndEmail" resultType="com.example.myproject.entity.VerificationToken">
+ SELECT * FROM verification_token
+ WHERE token = #{token} AND email = #{email}
+ </select>
+</mapper>
diff --git a/src/main/resources/output/valid.torrent b/src/main/resources/output/valid.torrent
new file mode 100644
index 0000000..6a90e52
--- /dev/null
+++ b/src/main/resources/output/valid.torrent
@@ -0,0 +1 @@
+d10:created by18:qBittorrent v5.1.013:creation datei1745948995e4:infod6:lengthi22e4:name15:example.torrent12:piece lengthi16384e6:pieces20:Fnð¶)ú<Ç æÂh£tl7:privatei1eee
\ No newline at end of file
diff --git a/src/test/java/com/example/myproject/controller/CommentControllerTest.java b/src/test/java/com/example/myproject/controller/CommentControllerTest.java
deleted file mode 100644
index 9dedd29..0000000
--- a/src/test/java/com/example/myproject/controller/CommentControllerTest.java
+++ /dev/null
@@ -1,81 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.entity.Comments;
-import com.example.myproject.entity.Users;
-import com.example.myproject.service.CommentService;
-import com.example.myproject.utils.Result;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.*;
-import org.springframework.beans.factory.annotation.Autowired;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static org.mockito.Mockito.*;
-import static org.junit.jupiter.api.Assertions.*;
-
-class CommentControllerTest {
-
- @InjectMocks
- private CommentController commentController;
- @Mock
- private CommentService commentService;
-
- @Mock
- private Users mockUser;
-
- @BeforeEach
- void setup() {
- MockitoAnnotations.openMocks(this);
- }
-
- // 测试添加评论
- @Test
- void addCommentTest() {
- Long postId = 1L;
- Comments comment = new Comments();
- comment.setUserId(1L);
- comment.setContent("This is a comment");
- comment.setParentComment(0L);
- // 模拟服务层的行为
- doNothing().when(commentService).addComment(eq(postId), eq(comment));
-
- // 调用控制器的方法
- String result = commentController.addComment(postId, comment);
-
- // 验证返回的结果
- assertEquals("Comment added successfully!", result);
-
- // 验证服务方法是否被调用
- verify(commentService, times(1)).addComment(eq(postId), eq(comment));
- }
-
- // 测试获取评论
- @Test
- void getCommentsByPostIdTest() {
- Long postId = 1L;
-
- // 模拟评论数据
- Map<String, Object> commentData = new HashMap<>();
- commentData.put("commentId", 1L);
- commentData.put("content", "This is a comment");
- commentData.put("userId", 1L);
- commentData.put("nickname", "user1");
-
- // 模拟服务层的行为
- when(commentService.getCommentsByPostId(postId)).thenReturn(List.of(commentData));
-
- // 调用控制器的方法
- List<Map<String, Object>> comments = commentController.getCommentsByPostId(postId);
-
- // 验证返回的评论数据
- assertEquals(1, comments.size());
- assertEquals("This is a comment", comments.get(0).get("content"));
- assertEquals("user1", comments.get(0).get("nickname"));
-
- // 验证服务方法是否被调用
- verify(commentService, times(1)).getCommentsByPostId(postId);
- }
-}
diff --git a/src/test/java/com/example/myproject/controller/DynamicControllerTest.java b/src/test/java/com/example/myproject/controller/DynamicControllerTest.java
deleted file mode 100644
index 7392162..0000000
--- a/src/test/java/com/example/myproject/controller/DynamicControllerTest.java
+++ /dev/null
@@ -1,168 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.controller.DynamicController;
-import com.example.myproject.service.DynamicService;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.springframework.http.ResponseEntity;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.mockito.Mockito.*;
-import static org.junit.jupiter.api.Assertions.*;
-
-class DynamicControllerTest {
-
- @InjectMocks
- private DynamicController dynamicController;
-
- @Mock
- private DynamicService dynamicService;
-
- @BeforeEach
- void setup() {
- MockitoAnnotations.openMocks(this);
- }
-
- // 测试创建动态接口
- @Test
- void testCreateDynamic() {
- Long userId = 1L;
- String content = "This is a test dynamic";
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("dynamicId", 123L);
- mockResponse.put("message", "动态创建成功");
-
- // 模拟 dynamicService.createDynamic 返回
- when(dynamicService.createDynamic(userId, null, content, null)).thenReturn(mockResponse);
-
- // 调用控制器方法
- Map<String, Object> responseMap = dynamicController.createDynamic(userId, null, content, null);
- ResponseEntity<Map<String, Object>> responseEntity = ResponseEntity.ok(responseMap);
-
- // 验证返回的结果
- assertEquals(200, responseEntity.getStatusCodeValue());
- assertEquals("动态创建成功", responseEntity.getBody().get("message"));
- assertEquals(123L, responseEntity.getBody().get("dynamicId"));
- }
-
-
- // 测试删除动态接口
- @Test
- void testDeleteDynamic() {
- Long dynamicId = 1L;
- // 模拟 dynamicService.deleteDynamic 方法行为
- doNothing().when(dynamicService).deleteDynamic(dynamicId);
-
- // 调用控制器方法
- ResponseEntity<String> response = dynamicController.deleteDynamic(dynamicId);
-
- // 验证返回的结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("动态删除成功", response.getBody());
- }
-
- // 测试添加评论接口
- @Test
- void testAddComment() {
- Long userId = 1L;
- Long dynamicId = 1L;
- Map<String, String> commentContent = new HashMap<>();
- commentContent.put("content", "This is a test comment");
-
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("comment_id", 101L);
- mockResponse.put("message", "评论成功");
-
- // 模拟 dynamicService.addComment 返回
- when(dynamicService.addComment(userId, dynamicId, "This is a test comment")).thenReturn(mockResponse);
-
- // 调用控制器方法
- ResponseEntity<Map<String, Object>> response = dynamicController.addComment(userId, dynamicId, commentContent);
-
- // 验证返回的结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("评论成功", response.getBody().get("message"));
- assertEquals(101L, response.getBody().get("comment_id"));
- }
-
- // 测试获取用户动态接口
- @Test
- void testGetAllUserDynamics() {
- Long userId = 1L;
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("dynamic", "mock_dynamic_data");
-
- // 模拟 dynamicService.getAllUserDynamics 返回
- when(dynamicService.getAllUserDynamics(userId)).thenReturn(mockResponse);
-
- // 调用控制器方法
- ResponseEntity<Map<String, Object>> response = dynamicController.getAllUserDynamics(userId);
-
- // 验证返回的结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("success", response.getBody().get("status"));
- assertNotNull(response.getBody().get("dynamic"));
- }
-
- // 测试获取所有好友动态接口
- @Test
- void testGetAllUserAndFriendsDynamics() {
- Long userId = 1L;
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("dynamic", "mock_friend_dynamic_data");
-
- // 模拟 dynamicService.getAllUserAndFriendsDynamics 返回
- when(dynamicService.getAllUserAndFriendsDynamics(anyList())).thenReturn(mockResponse);
-
- // 调用控制器方法
- ResponseEntity<Map<String, Object>> response = dynamicController.getAllUserAndFriendsDynamics(userId);
-
- // 验证返回的结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("success", response.getBody().get("status"));
- assertNotNull(response.getBody().get("dynamic"));
- }
-
- // 测试点赞动态接口
- @Test
- void testLikeDynamic() {
- Map<String, Long> request = new HashMap<>();
- request.put("userId", 1L);
- request.put("dynamicId", 1L);
-
- // 模拟 dynamicService.likeDynamic 返回
- when(dynamicService.likeDynamic(1L, 1L)).thenReturn(true);
-
- // 调用控制器方法
- ResponseEntity<String> response = dynamicController.likeDynamic(request);
-
- // 验证返回的结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("Like successful", response.getBody());
- }
-
- // 测试取消点赞接口
- @Test
- void testUnlikeDynamic() {
- Map<String, Long> request = new HashMap<>();
- request.put("userId", 1L);
- request.put("dynamicId", 1L);
-
- // 模拟 dynamicService.unlikeDynamic 返回
- when(dynamicService.unlikeDynamic(1L, 1L)).thenReturn(true);
-
- // 调用控制器方法
- ResponseEntity<String> response = dynamicController.unlikeDynamic(request);
-
- // 验证返回的结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("Unlike successful", response.getBody());
- }
-}
diff --git a/src/test/java/com/example/myproject/controller/GroupControllerTest.java b/src/test/java/com/example/myproject/controller/GroupControllerTest.java
deleted file mode 100644
index d8c8c2c..0000000
--- a/src/test/java/com/example/myproject/controller/GroupControllerTest.java
+++ /dev/null
@@ -1,209 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.controller.GroupController;
-import com.example.myproject.entity.Group;
-import com.example.myproject.entity.GroupComments;
-import com.example.myproject.service.GroupService;
-import com.example.myproject.utils.Result;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.springframework.http.ResponseEntity;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.mockito.Mockito.*;
-import static org.junit.jupiter.api.Assertions.*;
-
-
-class GroupControllerTest {
-
- @InjectMocks
- private GroupController groupController;
-
- @Mock
- private GroupService groupService;
-
- @BeforeEach
- void setup() {
- MockitoAnnotations.openMocks(this);
- }
-
- // 测试创建小组
-
- @Test
- void createGroupTest() {
- Group groupRequest = new Group();
- groupRequest.setGroupName("Test Group");
- groupRequest.setDescription("This is a test group");
-
- // 模拟服务层的返回值
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("group_id", 1L);
- mockResponse.put("message", "兴趣小组创建成功");
-
- ResponseEntity<Map<String, Object>> mockResponseEntity = ResponseEntity.ok(mockResponse);
- when(groupService.createGroup(groupRequest)).thenReturn(mockResponseEntity);
-
- // 调用控制器的方法
- ResponseEntity<?> responseEntity = groupController.createGroup(groupRequest);
- Map<String, Object> resultMap = (Map<String, Object>) responseEntity.getBody();
- Result<Map<String, Object>> result = Result.success(resultMap, "兴趣小组创建成功");
-
- // 验证返回的结果
- assertEquals("200", result.getCode());
- assertEquals("兴趣小组创建成功", result.getMsg());
- assertEquals(1L, result.getData().get("group_id"));
- }
-
-
- // 测试加入小组接口
- @Test
- void testJoinGroup() {
- Long groupId = 1L;
- Long userId = 1L;
-
- // 模拟服务层的返回值
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "成功加入小组");
- when(groupService.joinGroup(groupId, userId)).thenReturn(ResponseEntity.ok(mockResponse));
-
- // 调用控制器方法
- Map<String, Long> requestBody = Map.of("user_id", userId);
- ResponseEntity<Map<String, Object>> response = groupController.joinGroup(groupId, requestBody);
-
- // 验证返回的结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("success", response.getBody().get("status"));
- assertEquals("成功加入小组", response.getBody().get("message"));
- }
-
- // 测试退出小组接口
- @Test
- void testLeaveGroup() {
- Long groupId = 1L;
- Long userId = 1L;
-
- // 模拟服务层的返回值
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "成功退出小组");
- when(groupService.leaveGroup(groupId, userId)).thenReturn(ResponseEntity.ok(mockResponse));
-
- // 调用控制器方法
- Map<String, Long> requestBody = Map.of("user_id", userId);
- ResponseEntity<Map<String, Object>> response = groupController.leaveGroup(groupId, requestBody);
-
- // 验证返回的结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("success", response.getBody().get("status"));
- assertEquals("成功退出小组", response.getBody().get("message"));
- }
-
- // 测试获取小组成员接口
- @Test
- void testGetGroupMembers() {
- Long groupId = 1L;
-
- // 模拟服务层的返回值
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("members", "mock_members");
- when(groupService.getGroupMembers(groupId)).thenReturn(ResponseEntity.ok(mockResponse));
-
- // 调用控制器方法
- ResponseEntity<Map<String, Object>> response = groupController.getGroupMembers(groupId);
-
- // 验证返回的结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("success", response.getBody().get("status"));
- assertNotNull(response.getBody().get("members"));
- }
-
- // 测试发布帖子
- @Test
- void createPostTest() {
- Long groupId = 1L;
- Long userId = 2L;
- String postContent = "Test post content";
- String title = "Test post title";
-
- // 模拟服务层的返回值
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("post_id", 1L);
- mockResponse.put("message", "帖子创建成功");
-
- when(groupService.createPost(groupId, userId, postContent, title, null)).thenReturn(mockResponse);
-
- // 调用控制器的方法
- Map<String, Object> result = groupController.createPost(groupId, userId, postContent, title, null).getBody();
-
- // 验证返回的结果
- assertEquals("帖子创建成功", result.get("message"));
- assertEquals(1L, result.get("post_id"));
- }
-
- // 测试点赞帖子
- @Test
- void likePostTest() {
- Long postId = 1L;
-
- // 模拟服务层的返回值
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "点赞成功");
-
- when(groupService.likePost(postId)).thenReturn(mockResponse);
-
- // 调用控制器的方法
- Map<String, Object> resultMap = groupController.likePost(postId).getBody();
-
- // 验证返回的结果
- assertEquals("点赞成功", resultMap.get("message"));
- }
-
- // 测试取消点赞
- @Test
- void unlikePostTest() {
- Long postId = 1L;
-
- // 模拟服务层的返回值
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "取消点赞成功");
-
- when(groupService.unlikePost(postId)).thenReturn(mockResponse);
-
- // 调用控制器的方法
- Map<String, Object> resultMap = groupController.unlikePost(postId).getBody();
-
- // 验证返回的结果
- assertEquals("取消点赞成功", resultMap.get("message"));
- }
-
- // 测试添加评论
- @Test
- void addCommentTest() {
- Long postId = 1L;
- GroupComments comment = new GroupComments();
- comment.setUserId(2L);
- comment.setContent("This is a comment");
-
- // 模拟服务层的返回值
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "评论成功");
-
- when(groupService.addComment(postId, comment)).thenReturn(mockResponse);
-
- // 调用控制器的方法
- Map<String, Object> resultMap = groupController.addComment(postId, comment).getBody();
-
- // 验证返回的结果
- assertEquals("评论成功", resultMap.get("message"));
- }
-}
diff --git a/src/test/java/com/example/myproject/controller/LevelControllerTest.java b/src/test/java/com/example/myproject/controller/LevelControllerTest.java
deleted file mode 100644
index c3d77b9..0000000
--- a/src/test/java/com/example/myproject/controller/LevelControllerTest.java
+++ /dev/null
@@ -1,126 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.controller.LevelController;
-import com.example.myproject.service.LevelService;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.springframework.http.ResponseEntity;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.mockito.Mockito.*;
-import static org.junit.jupiter.api.Assertions.*;
-
-class LevelControllerTest {
-
- @InjectMocks
- private LevelController levelController;
-
- @Mock
- private LevelService levelService;
-
- @BeforeEach
- void setup() {
- MockitoAnnotations.openMocks(this);
- }
-
- // 测试获取用户经验接口
- @Test
- void testGetExperience() {
- Long userId = 1L;
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("current_experience", 1500);
- mockResponse.put("level", "Intermediate");
- mockResponse.put("next_level_experience", 2000);
-
- // 模拟 levelService.getUserExperience 返回
- when(levelService.getUserExperience(userId)).thenReturn(mockResponse);
-
- // 调用控制器方法
- Map<String, Object> response = levelController.getExperience(userId);
-
- // 验证返回的结果
- assertEquals(1500, response.get("current_experience"));
- assertEquals("Intermediate", response.get("level"));
- assertEquals(2000, response.get("next_level_experience"));
- }
-
- // 测试更新用户经验接口
- @Test
- void testUpdateExperience() {
- Long userId = 1L;
- Integer experience = 500;
- String source = "Quest";
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "经验更新成功");
- mockResponse.put("current_experience", 2000);
-
- Map<String, Object> params = new HashMap<>();
- params.put("user_id", userId);
- params.put("experience", experience);
- params.put("source", source);
-
- // 模拟 levelService.updateExperience 返回
- when(levelService.updateExperience(userId, experience, source)).thenReturn(mockResponse);
-
- // 调用控制器方法
- Map<String, Object> response = levelController.updateExperience(params);
-
- // 验证返回的结果
- assertEquals("success", response.get("status"));
- assertEquals("经验更新成功", response.get("message"));
- assertEquals(2000, response.get("current_experience"));
- }
-
- // 测试检查用户升级条件接口
- @Test
- void testCheckUpgrade() {
- Long userId = 1L;
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("can_upgrade", true);
- mockResponse.put("next_level_experience", 2000);
- mockResponse.put("current_experience", 1500);
-
- // 模拟 levelService.checkUpgrade 返回
- when(levelService.checkUpgrade(userId)).thenReturn(mockResponse);
-
- // 调用控制器方法
- Map<String, Object> response = levelController.checkUpgrade(userId);
-
- // 验证返回的结果
- assertTrue((Boolean) response.get("can_upgrade"));
- assertEquals(2000, response.get("next_level_experience"));
- assertEquals(1500, response.get("current_experience"));
- }
-
- // 测试升级用户等级接口
- @Test
- void testUpgradeUserLevel() {
- Long userId = 1L;
- Boolean canUpgrade = true;
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "升级成功");
- mockResponse.put("new_level", "Advanced");
-
- Map<String, Object> request = new HashMap<>();
- request.put("user_id", userId);
- request.put("can_upgrade", canUpgrade);
-
- // 模拟 levelService.upgradeUserLevel 返回
- when(levelService.upgradeUserLevel(userId, canUpgrade)).thenReturn(mockResponse);
-
- // 调用控制器方法
- Map<String, Object> response = levelController.upgradeUserLevel(request);
-
- // 验证返回的结果
- assertEquals("success", response.get("status"));
- assertEquals("升级成功", response.get("message"));
- assertEquals("Advanced", response.get("new_level"));
- }
-}
diff --git a/src/test/java/com/example/myproject/controller/PostControllerTest.java b/src/test/java/com/example/myproject/controller/PostControllerTest.java
deleted file mode 100644
index 6586086..0000000
--- a/src/test/java/com/example/myproject/controller/PostControllerTest.java
+++ /dev/null
@@ -1,279 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.entity.Collections;
-import com.example.myproject.entity.Post;
-import com.example.myproject.entity.Users;
-import com.example.myproject.repository.CollectionsRepository;
-import com.example.myproject.repository.PostRepository;
-import com.example.myproject.repository.UserRepository;
-import com.example.myproject.service.PostService;
-import com.example.myproject.utils.Result;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.*;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.util.*;
-
-import static org.mockito.Mockito.*;
-import static org.junit.jupiter.api.Assertions.*;
-
-class PostControllerTest {
-
- @InjectMocks
- private PostController postController;
-
- @Mock
- private PostService postService;
-
- @Mock
- private CollectionsRepository collectionsRepository;
-
- @Mock
- private PostRepository postRepository;
-
- @Mock
- private UserRepository userRepository;
-
- @InjectMocks
- private UserController userController;
-
- @BeforeEach
- void setup() {
- MockitoAnnotations.openMocks(this);
- }
-
- // 测试创建帖子
- void createPostTest() {
- Long userId = 1L;
- String postContent = "Test Post Content";
- String title = "Test Post Title";
- MultipartFile[] imageFiles = new MultipartFile[0]; // Empty array for simplicity
-
- // 模拟服务层的返回值
- Map<String, Object> responseMap = new HashMap<>();
- responseMap.put("postNo", 123L);
- responseMap.put("message", "帖子创建成功");
-
- when(postService.createPost(userId, postContent, title, imageFiles)).thenReturn(responseMap);
-
- // 调用控制器的方法
- Map<String, Object> resultMap = postController.createPost(userId, postContent, title, imageFiles);
-
- // 手动将 Map<String, Object> 包装到 Result.success()
- Result<Map<String, Object>> result = Result.success(resultMap, "帖子创建成功");
-
- // 验证返回的结果
- assertEquals("200", result.getCode());
- assertEquals("帖子创建成功", result.getMsg());
- assertEquals(123L, result.getData().get("postNo"));
- }
-
- // 测试编辑帖子
- @Test
- void updatePostTest() {
- Long postId = 1L;
- Post post = new Post();
- post.setPostNo(postId);
- post.setTitle("Updated Title");
- post.setPostContent("Updated Content");
-
- // 模拟服务层的行为
- doNothing().when(postService).updatePost(postId, post);
-
- // 调用控制器的方法
- String result = postController.updatePost(postId, post);
-
- // 验证返回的结果
- assertEquals("Post updated successfully!", result);
- }
-
- // 测试删除帖子
- @Test
- void deletePostTest() {
- Long postId = 1L;
-
- // 模拟服务层的行为
- doNothing().when(postService).deletePost(postId);
-
- // 调用控制器的方法
- String result = postController.deletePost(postId);
-
- // 验证返回的结果
- assertEquals("Post deleted successfully!", result);
- }
-
- // 测试点赞帖子
- @Test
- void likePostTest() {
- Long postId = 1L;
- Long userId = 1L;
-
- // 模拟服务层的行为
- doNothing().when(postService).likePost(postId, userId);
-
- // 创建请求体
- Map<String, Long> requestBody = new HashMap<>();
- requestBody.put("user_id", userId);
-
- // 调用控制器的方法
- String result = postController.likePost(postId, requestBody);
-
- // 验证返回的结果
- assertEquals("Post liked successfully!", result);
- }
-
- // 测试取消点赞帖子
- @Test
- void unlikePostTest() {
- Long postId = 1L;
- Long userId = 1L;
-
- // 模拟服务层的行为
- doNothing().when(postService).unlikePost(postId, userId);
-
- // 创建请求体
- Map<String, Long> requestBody = new HashMap<>();
- requestBody.put("user_id", userId);
-
- // 调用控制器的方法
- String result = postController.unlikePost(postId, requestBody);
-
- // 验证返回的结果
- assertEquals("Post unliked successfully!", result);
- }
-
- // 测试收藏帖子
- @Test
- void collectPostTest() {
- Long postId = 1L;
- Long userId = 1L;
-
- // 模拟服务层的行为
- doNothing().when(postService).collectPost(postId, userId);
-
- // 创建请求体
- Map<String, Long> requestBody = new HashMap<>();
- requestBody.put("user_id", userId);
-
- // 调用控制器的方法
- String result = postController.collectPost(postId, requestBody);
-
- // 验证返回的结果
- assertEquals("Post collected successfully!", result);
- }
-
- // 测试取消收藏帖子
- @Test
- void uncollectPostTest() {
- Long postId = 1L;
- Long userId = 1L;
-
- // 模拟服务层的行为
- doNothing().when(postService).uncollectPost(postId, userId);
-
- // 创建请求体
- Map<String, Long> requestBody = new HashMap<>();
- requestBody.put("user_id", userId);
-
- // 调用控制器的方法
- String result = postController.uncollectPost(postId, requestBody);
-
- // 验证返回的结果
- assertEquals("Post uncollected successfully!", result);
- }
-
- // 测试获取所有帖子
- @Test
- void getAllPostsTest() {
- // 模拟服务层的行为
- Map<String, Object> responseMap = new HashMap<>();
- responseMap.put("total", 5);
- responseMap.put("posts", new ArrayList<>());
-
- when(postService.getAllPosts()).thenReturn(responseMap);
-
- // 调用控制器的方法
- Map<String, Object> result = postController.getAllPosts();
-
- // 验证返回的结果
- assertEquals(5, result.get("total"));
- }
-
- // 测试根据帖子ID获取帖子
- @Test
- void getPostByIdTest() {
- Long postId = 1L;
- Map<String, Object> responseMap = new HashMap<>();
- responseMap.put("postNo", 123L);
- responseMap.put("message", "Post details");
-
- // 模拟服务层的行为
- when(postService.getPostById(postId)).thenReturn(responseMap);
-
- // 调用控制器的方法
- Map<String, Object> result = postController.getPostById(postId);
-
- // 验证返回的结果
- assertEquals("Post details", result.get("message"));
- assertEquals(123L, result.get("postNo"));
- }
-
- // 测试获取用户收藏的所有帖子接口
- @Test
- void testGetAllCollections() {
- Long userId = 1L;
-
- // 模拟用户收藏的数据
- Collections collection = new Collections();
- collection.setCollectionId(1L);
- collection.setUserId(userId);
- collection.setPostNo(101L);
-
- List<Collections> collections = new ArrayList<>();
- collections.add(collection);
-
- // 模拟帖子数据
- Post post = new Post();
- post.setPostNo(101L);
- post.setTitle("Test Post Title");
- post.setPostContent("Test Post Content");
- post.setImageUrl("https://example.com/image.jpg");
- post.setUser_id(2L); // 作者用户ID
-
- Users user = new Users();
- user.setUserId(2L);
- user.setUsername("testUser");
- user.setAvatarUrl("https://example.com/avatar.jpg");
-
- // 设置模拟服务层的返回
- when(postService.getAllCollections(userId)).thenReturn(Arrays.asList(
- Map.of(
- "postNo", post.getPostNo(),
- "title", post.getTitle(),
- "postContent", post.getPostContent(),
- "imageUrl", post.getImageUrl(),
- "userId", post.getUser_id(),
- "username", user.getUsername(),
- "avatarUrl", user.getAvatarUrl()
- )
- ));
-
- // 调用控制器方法
- List<Map<String, Object>> result = postController.getAllCollections(userId);
-
- // 验证返回的结果
- assertNotNull(result);
- assertEquals(1, result.size()); // 验证返回数据的数量
-
- // 验证返回的数据结构
- Map<String, Object> postInfo = result.get(0);
- assertEquals(101L, postInfo.get("postNo"));
- assertEquals("Test Post Title", postInfo.get("title"));
- assertEquals("Test Post Content", postInfo.get("postContent"));
- assertEquals("https://example.com/image.jpg", postInfo.get("imageUrl"));
- assertEquals(2L, postInfo.get("userId"));
- assertEquals("testUser", postInfo.get("username"));
- assertEquals("https://example.com/avatar.jpg", postInfo.get("avatarUrl"));
- }
-}
diff --git a/src/test/java/com/example/myproject/controller/SeedCommentControllerTest.java b/src/test/java/com/example/myproject/controller/SeedCommentControllerTest.java
deleted file mode 100644
index 7fa6ff3..0000000
--- a/src/test/java/com/example/myproject/controller/SeedCommentControllerTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.entity.SeedComment;
-import com.example.myproject.service.SeedCommentService;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.springframework.http.ResponseEntity;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.mockito.Mockito.*;
-import static org.junit.jupiter.api.Assertions.*;
-
-public class SeedCommentControllerTest {
-
- @InjectMocks
- private SeedCommentController seedCommentController;
-
- @Mock
- private SeedCommentService seedCommentService;
-
- @BeforeEach
- public void setUp() {
- MockitoAnnotations.openMocks(this); // 初始化 Mockito 注解
- }
-
- // 测试发布评论
- @Test
- public void testPublishComment() {
- int seedId = 1;
- SeedComment seedComment = new SeedComment();
- seedComment.setContent("Test comment content");
- seedComment.setUserId(1);
- seedComment.setComCommentId(0); // 顶级评论
-
- // 模拟服务层的返回值
- String expectedResponse = "{ \"status\": \"success\", \"message\": \"评论发布成功\" }";
- when(seedCommentService.publishComment(seedId, seedComment)).thenReturn(expectedResponse);
-
- // 调用控制器的方法
- String result = seedCommentController.publishComment(seedId, seedComment);
-
- // 验证结果
- assertEquals(expectedResponse, result);
- }
-
- // 测试获取种子下所有评论
- @Test
- public void testGetAllComments() {
- int seedId = 1;
-
- // 模拟服务层的返回值
- Map<String, Object> expectedResponse = new HashMap<>();
- expectedResponse.put("status", "success");
- expectedResponse.put("comments", "dummyComments"); // 示例返回数据
-
- when(seedCommentService.getAllCommentsForSeed(seedId)).thenReturn(expectedResponse);
-
- // 调用控制器的方法
- Map<String, Object> result = seedCommentController.getAllComments(seedId);
-
- // 验证结果
- assertEquals(expectedResponse, result);
- }
-
- // 测试点赞切换操作
- @Test
- public void testToggleLike() {
- Long commentId = 1L;
- Map<String, Object> request = new HashMap<>();
- request.put("user_id", 1L);
-
- // 模拟服务层的返回值
- Map<String, Object> expectedResponse = new HashMap<>();
- expectedResponse.put("status", "success");
- expectedResponse.put("message", "操作成功");
- expectedResponse.put("liked", "true");
-
- when(seedCommentService.toggleLike(commentId, 1L)).thenReturn(expectedResponse);
-
- // 调用控制器的方法
- Map<String, Object> result = seedCommentController.toggleLike(commentId, request);
-
- // 验证结果
- assertEquals(expectedResponse, result);
- }
-}
diff --git a/src/test/java/com/example/myproject/controller/TaskControllerTest.java b/src/test/java/com/example/myproject/controller/TaskControllerTest.java
deleted file mode 100644
index d0bb754..0000000
--- a/src/test/java/com/example/myproject/controller/TaskControllerTest.java
+++ /dev/null
@@ -1,197 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.controller.TaskController;
-import com.example.myproject.service.TaskService;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.springframework.http.ResponseEntity;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.mockito.Mockito.*;
-import static org.junit.jupiter.api.Assertions.*;
-
-class TaskControllerTest {
-
- @InjectMocks
- private TaskController taskController;
-
- @Mock
- private TaskService taskService;
-
- @BeforeEach
- void setup() {
- MockitoAnnotations.openMocks(this);
- }
-
- // 测试获取当前用户的新手任务列表接口
- @Test
- void testGetAllTasks() {
- Long userId = 1L;
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("tasks", "mock_tasks");
-
- // 模拟 taskService.getAllTasksForUser 返回
- when(taskService.getAllTasksForUser(userId)).thenReturn(mockResponse);
-
- // 调用控制器方法,改为通过请求参数传递 user_id
- Map<String, Object> response = taskController.getAllTasks(userId);
-
- // 验证返回的结果
- assertEquals("mock_tasks", response.get("tasks"));
- }
-
- // 测试更新任务状态接口
- @Test
- void testUpdateTaskStatus() {
- Long userId = 1L;
- String taskId = "task_123";
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "任务状态已更新");
-
- // 模拟 taskService.updateTaskStatus 返回
- when(taskService.updateTaskStatus(userId, taskId)).thenReturn(mockResponse);
-
- // 创建请求体
- Map<String, Object> request = new HashMap<>();
- request.put("user_id", userId);
- request.put("task_id", taskId);
-
- // 调用控制器方法
- Map<String, Object> response = taskController.updateTaskStatus(request);
-
- // 验证返回的结果
- assertEquals("success", response.get("status"));
- assertEquals("任务状态已更新", response.get("message"));
- }
-
- // 测试获取当前经验和任务奖励接口
- @Test
- void testGetExperience() {
- Long userId = 1L;
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("current_experience", 1500);
- mockResponse.put("level", "Intermediate");
- mockResponse.put("reward", Map.of("experience", 1000, "points", 200));
-
- // 模拟 taskService.getUserExperience 返回
- when(taskService.getUserExperience(userId)).thenReturn(mockResponse);
-
- // 创建请求体
- Map<String, Object> request = new HashMap<>();
- request.put("user_id", userId);
-
- // 调用控制器方法
- Map<String, Object> response = taskController.getExperience(request);
-
- // 验证返回的结果
- assertEquals(1500, response.get("current_experience"));
- assertEquals("Intermediate", response.get("level"));
- assertTrue(response.containsKey("reward"));
- }
-
- // 测试获取当前的指引步骤接口
- @Test
- void testGetNewStep() {
- Long userId = 1L;
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("current_step", "step_1");
- mockResponse.put("total_steps", 5);
- mockResponse.put("step_description", "Complete the introduction task");
-
- // 模拟 taskService.getNewStep 返回
- when(taskService.getNewStep(userId)).thenReturn(mockResponse);
-
- // 调用控制器方法,改为通过请求参数传递 user_id
- Map<String, Object> response = taskController.getNewStep(userId);
-
- // 验证返回的结果
- assertEquals("step_1", response.get("current_step"));
- assertEquals(5, response.get("total_steps"));
- assertEquals("Complete the introduction task", response.get("step_description"));
- }
-
- // 测试更新进度接口
- @Test
- void testUpdateProgress() {
- Long userId = 1L;
- String taskId = "task_123";
- Integer progress = 50;
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "进度已更新");
-
- // 模拟 taskService.updateTaskProgress 返回
- when(taskService.updateTaskProgress(userId, taskId, progress)).thenReturn(mockResponse);
-
- // 创建请求体
- Map<String, Object> request = new HashMap<>();
- request.put("user_id", userId);
- request.put("task_id", taskId);
- request.put("progress", progress);
-
- // 调用控制器方法
- Map<String, Object> response = taskController.updateProgress(request);
-
- // 验证返回的结果
- assertEquals("success", response.get("status"));
- assertEquals("进度已更新", response.get("message"));
- }
-
- // 测试领取任务奖励接口
- @Test
- void testRewardClaim() {
- Long userId = 1L;
- String taskId = "task_123";
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "奖励已领取");
- mockResponse.put("reward", Map.of("experience", 1000, "points", 200));
-
- // 模拟 taskService.claimReward 返回
- when(taskService.claimReward(userId, taskId)).thenReturn(mockResponse);
-
- // 创建请求体
- Map<String, Object> request = new HashMap<>();
- request.put("user_id", userId);
- request.put("task_id", taskId);
-
- // 调用控制器方法
- Map<String, Object> response = taskController.rewardClaim(request);
-
- // 验证返回的结果
- assertEquals("success", response.get("status"));
- assertEquals("奖励已领取", response.get("message"));
- assertTrue(response.containsKey("reward"));
- }
-
- // 测试检查任务奖励状态接口
- @Test
- void testRewardReview() {
- Long userId = 1L;
- String taskId = "task_123";
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "reward_not_issued");
- mockResponse.put("message", "任务奖励未被领取");
-
- // 模拟 taskService.checkRewardStatus 返回
- when(taskService.checkRewardStatus(userId, taskId)).thenReturn(mockResponse);
-
- // 创建请求体
- Map<String, Object> request = new HashMap<>();
- request.put("user_id", userId);
- request.put("task_id", taskId);
-
- // 调用控制器方法
- Map<String, Object> response = taskController.rewardReview(request);
-
- // 验证返回的结果
- assertEquals("reward_not_issued", response.get("status"));
- assertEquals("任务奖励未被领取", response.get("message"));
- }
-}
diff --git a/src/test/java/com/example/myproject/controller/TorrentControllerTest.java b/src/test/java/com/example/myproject/controller/TorrentControllerTest.java
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/test/java/com/example/myproject/controller/TorrentControllerTest.java
diff --git a/src/test/java/com/example/myproject/controller/UserControllerTest.java b/src/test/java/com/example/myproject/controller/UserControllerTest.java
index a97e169..d332f31 100644
--- a/src/test/java/com/example/myproject/controller/UserControllerTest.java
+++ b/src/test/java/com/example/myproject/controller/UserControllerTest.java
@@ -1,234 +1,162 @@
package com.example.myproject.controller;
-import com.example.myproject.entity.Users;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.example.myproject.entity.User;
+import com.example.myproject.service.EmailService;
import com.example.myproject.service.UserService;
-import com.example.myproject.repository.UserRepository;
+import com.example.myproject.mapper.UserMapper;
+import com.example.myproject.mapper.VerificationTokenMapper;
+import com.example.myproject.common.base.Result;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.*;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.AuthenticationException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
-
-import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
class UserControllerTest {
- @Mock
- private UserService userService;
-
- @Mock
- private UserRepository userRepository;
-
@InjectMocks
private UserController userController;
+ @Mock
+ private UserService userService;
+ @Mock
+ private UserMapper userMapper;
+ @Mock
+ private EmailService emailService;
+ @Mock
+ private AuthenticationManager authenticationManager;
+ @Mock
+ private VerificationTokenMapper verificationTokenMapper;
+
@BeforeEach
- void setUp() {
- // 初始化模拟对象
+ void setup() {
MockitoAnnotations.openMocks(this);
}
- // 测试生成邀请码接口
@Test
- void testGenerateInviteCode() {
- // 设置输入数据
- Map<String, Object> request = new HashMap<>();
- request.put("user_id", 1L);
+ void loginController_success() {
+ String username = "testuser";
+ String password = "testpass";
+ User mockUser = new User();
+ mockUser.setUsername(username);
- // 模拟服务层的返回
- Map<String, Object> responseMap = new HashMap<>();
- responseMap.put("status", "success");
- responseMap.put("message", "邀请码生成成功");
- responseMap.put("invite_code", "ABC123XYZ");
+ Authentication mockAuth = mock(Authentication.class);
+ when(authenticationManager.authenticate(any())).thenReturn(mockAuth);
+ when(userMapper.selectOne(any(QueryWrapper.class))).thenReturn(mockUser);
- when(userService.generateInviteCode(1L)).thenReturn(responseMap);
+ Result result = userController.loginController(username, password);
- // 调用控制器方法
- Map<String, Object> resultMap = userController.generateInviteCode(request);
-
- // 验证返回结果
- assertEquals("success", resultMap.get("status"));
- assertEquals("邀请码生成成功", resultMap.get("message"));
- assertEquals("ABC123XYZ", resultMap.get("invite_code"));
-
- // 验证服务层方法是否被调用
- verify(userService, times(1)).generateInviteCode(1L);
+ assertEquals("200", result.getCode());
+ assertEquals(mockUser, result.getData());
}
- // 测试用户修改密码接口
+
@Test
- void testChangePassword() {
- Long userId = 1L;
- Map<String, Object> request = new HashMap<>();
- request.put("user_id", userId);
- request.put("old_password", "oldPassword");
- request.put("new_password", "newPassword");
- request.put("confirm_password", "newPassword");
+ void loginController_failure() {
+ String username = "user";
+ String password = "wrongpass";
- // 模拟服务层的返回
- String resultMessage = "密码修改成功";
- when(userService.changePassword(userId, "oldPassword", "newPassword", "newPassword"))
- .thenReturn(resultMessage);
+ // 模拟认证失败,抛出 Bad credentials 异常
+ when(authenticationManager.authenticate(any()))
+ .thenThrow(new AuthenticationException("Bad credentials") {});
- // 调用控制器方法
- Map<String, Object> resultMap = userController.changePassword(request);
- // 验证返回结果
- assertEquals("密码修改成功", resultMap.get("message"));
- assertEquals("success", resultMap.get("status"));
+ // 调用登录接口
+ Result result = userController.loginController(username, password);
- // 验证服务层方法是否被调用
- verify(userService, times(1)).changePassword(userId, "oldPassword", "newPassword", "newPassword");
- }
-
- // 测试用户注册接口
- @Test
- void testRegister() {
- // 设置输入数据
- Map<String, Object> request = new HashMap<>();
- request.put("username", "testUser");
- request.put("email", "test@example.com");
- request.put("password", "password");
- request.put("role", "USER");
- request.put("inviteCode", "12345");
-
- // 模拟服务层的返回
- String resultMessage = "用户注册成功";
- when(userService.registerUser("testUser", "test@example.com", "password", "USER", "12345"))
- .thenReturn(resultMessage);
-
- // 调用控制器方法
- Map<String, Object> resultMap = userController.register(request);
-
- // 验证返回结果
- assertEquals("用户注册成功", resultMap.get("msg"));
-
- // 验证服务层方法是否被调用
- verify(userService, times(1)).registerUser("testUser", "test@example.com", "password", "USER", "12345");
+ // 断言返回的状态码和消息
+ assertEquals("401", result.getCode());
+ assertTrue(result.getMsg().contains("登录失败"));
}
@Test
- void testLogin() {
- String username = "testUser";
- String password = "password";
-
- // 模拟服务层的返回
- String loginMessage = "登录成功";
- when(userService.loginUser("testUser", "password")).thenReturn(loginMessage);
-
- // 模拟用户查询
- Users user = new Users();
- user.setUserId(1L);
- user.setUsername("testUser");
+ void registerController_emailExists() {
+ User user = new User();
user.setEmail("test@example.com");
- when(userRepository.findByUsername("testUser")).thenReturn(Optional.of(user));
- // 设置请求数据
- Map<String, Object> request = new HashMap<>();
- request.put("username", username);
- request.put("password", password);
+ when(userService.checkEmailExists(user.getEmail())).thenReturn(true);
- // 调用控制器方法
- Map<String, Object> resultMap = userController.login(request);
+ Result result = userController.registerController(user);
- // 验证返回结果
- assertEquals("登录成功", resultMap.get("msg"));
-
- // 验证 user 不是 Map 类型,而是 Users 对象
- Users loggedInUser = (Users) resultMap.get("user");
- assertNotNull(loggedInUser);
- assertEquals("testUser", loggedInUser.getUsername());
- assertEquals("test@example.com", loggedInUser.getEmail());
-
- // 验证服务层方法是否被调用
- verify(userService, times(1)).loginUser("testUser", "password");
+ assertEquals("邮箱冲突", result.getCode());
}
-
- // 测试获取用户个人资料接口
@Test
- void testGetProfile() {
- Long userId = 1L;
-
- // Mock 用户数据
- Users user = new Users();
- user.setUserId(userId);
- user.setUsername("testUser");
+ void registerController_success() {
+ User user = new User();
user.setEmail("test@example.com");
- user.setAvatarUrl("https://example.com/avatar.jpg");
- // 模拟服务层返回
- Map<String, Object> profileData = new HashMap<>();
- profileData.put("avatarUrl", user.getAvatarUrl());
- profileData.put("username", user.getUsername());
- profileData.put("email", user.getEmail());
- when(userService.getProfile(userId)).thenReturn(profileData);
+ when(userService.checkEmailExists(user.getEmail())).thenReturn(false);
+ when(userService.preRegisterUser(user)).thenReturn(true);
- // 调用控制器方法
- Map<String, Object> resultMap = userController.getProfile(userId);
+ Result result = userController.registerController(user);
- // 验证返回结果
- assertEquals("testUser", resultMap.get("username"));
- assertEquals("test@example.com", resultMap.get("email"));
- assertEquals("https://example.com/avatar.jpg", resultMap.get("avatarUrl"));
-
- // 验证服务层方法是否被调用
- verify(userService, times(1)).getProfile(userId);
- }
-
- // 测试修改用户个人资料接口
- @Test
- void testEditProfile() {
- Long userId = 1L;
- Map<String, Object> profileData = new HashMap<>();
- profileData.put("avatarUrl", "https://example.com/avatar.jpg");
- profileData.put("nickname", "newNickname");
- profileData.put("gender", "Male");
- profileData.put("description", "Updated description");
- profileData.put("hobbies", "Reading, Hiking");
-
- // 模拟服务层的返回
- when(userService.editProfile(userId, "https://example.com/avatar.jpg", "newNickname", "Male", "Updated description", "Reading, Hiking"))
- .thenReturn(true);
-
- // 调用控制器方法
- Map<String, String> resultMap = userController.editProfile(userId, profileData);
-
- // 验证返回结果
- assertEquals("用户资料更新成功", resultMap.get("message"));
-
- // 验证服务层方法是否被调用
- verify(userService, times(1)).editProfile(userId, "https://example.com/avatar.jpg", "newNickname", "Male", "Updated description", "Reading, Hiking");
+ assertEquals("200", result.getCode());
+ assertEquals(user.getEmail(), ((User) result.getData()).getEmail());
}
@Test
- public void testCalculateShareRate() {
- Long userId = 1L;
+ void verifyEmailCode_success() {
+ when(userService.verifyEmail("test@example.com", "123456")).thenReturn(true);
- // 模拟 UserService 返回的结果
- Map<String, Object> expectedResponse = Map.of(
- "status", "success",
- "message", "分享率计算成功",
- "shareRate", 0.5f
- );
+ UserController.VerificationRequest request = new UserController.VerificationRequest();
+ request.setEmail("test@example.com");
+ request.setCode("123456");
- // 设置用户数据
- Users user = new Users();
- user.setUserId(userId);
- user.setUploadCount(50f); // 上传量为50
- user.setDownloadCount(100f); // 下载量为100
- when(userService.calculateShareRate(userId)).thenReturn(expectedResponse);
+ Result result = userController.verifyEmailCode(request);
- // 调用接口
- Map<String, Object> result = userController.calculateShareRate(userId);
+ assertEquals("200", result.getCode());
+ }
- // 验证结果
- assertEquals(expectedResponse, result);
- verify(userService, times(1)).calculateShareRate(userId); // 验证服务方法是否被调用
+ @Test
+ void verifyEmailCode_failure() {
+ when(userService.verifyEmail("test@example.com", "000000")).thenReturn(false);
+
+ UserController.VerificationRequest request = new UserController.VerificationRequest();
+ request.setEmail("test@example.com");
+ request.setCode("000000");
+
+ Result result = userController.verifyEmailCode(request);
+
+ assertEquals("验证失败", result.getCode());
+ }
+
+ @Test
+ void checkPassword_success() {
+ when(userService.checkPassword(1L, "abc123")).thenReturn(true);
+
+ Result<String> result = userController.checkPassword(1L, "abc123");
+
+ assertEquals("200", result.getCode());
+ assertEquals("原始密码输入正确", result.getMsg());
+ }
+
+ @Test
+ void checkPassword_failure() {
+ when(userService.checkPassword(1L, "wrong")).thenReturn(false);
+
+ Result<String> result = userController.checkPassword(1L, "wrong");
+
+ assertEquals("305", result.getCode());
+ assertEquals("原始密码输入错误", result.getMsg());
+ }
+
+ @Test
+ void sendVerificationEmail_userNotFound() {
+ UserController.EmailRequest request = new UserController.EmailRequest();
+ request.setEmail("notfound@example.com");
+
+ when(userMapper.selectOne(any())).thenReturn(null);
+
+ ResponseEntity<Result> response = userController.sendVerificationEmail(request);
+
+ assertEquals(400, response.getStatusCodeValue());
+ assertEquals("1", response.getBody().getCode());
}
}
diff --git a/src/test/java/com/example/myproject/controller/UserFollowControllerTest.java b/src/test/java/com/example/myproject/controller/UserFollowControllerTest.java
deleted file mode 100644
index 29bf47f..0000000
--- a/src/test/java/com/example/myproject/controller/UserFollowControllerTest.java
+++ /dev/null
@@ -1,132 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.controller.UserFollowController;
-import com.example.myproject.service.UserFollowService;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.springframework.http.ResponseEntity;
-import java.util.List;
-
-import java.util.Map;
-
-import static org.mockito.Mockito.*;
-import static org.junit.jupiter.api.Assertions.*;
-
-class UserFollowControllerTest {
-
- @InjectMocks
- private UserFollowController userFollowController;
-
- @Mock
- private UserFollowService userFollowService;
-
- @BeforeEach
- void setup() {
- MockitoAnnotations.openMocks(this);
- }
-
- // 测试用户关注接口
- @Test
- void testFollow() {
- Long followerId = 1L;
- Long followedId = 2L;
- Map<String, Object> mockResponse = Map.of(
- "status", "success",
- "message", "关注成功"
- );
-
- // 模拟 userFollowService.follow() 返回
- when(userFollowService.follow(followerId, followedId)).thenReturn(mockResponse);
-
- // 创建请求体
- Map<String, Long> request = Map.of("follower_id", followerId);
-
- // 调用控制器方法
- ResponseEntity<Map<String, Object>> responseEntity = userFollowController.follow(followedId, request);
-
- // 验证返回的状态码和内容
- assertEquals(200, responseEntity.getStatusCodeValue());
- assertEquals("success", responseEntity.getBody().get("status"));
- assertEquals("关注成功", responseEntity.getBody().get("message"));
- }
-
- // 测试取消关注接口
- @Test
- void testUnfollow() {
- Long followerId = 1L;
- Long followedId = 2L;
- Map<String, Object> mockResponse = Map.of(
- "status", "success",
- "message", "取消关注成功"
- );
-
- // 模拟 userFollowService.unfollow() 返回
- when(userFollowService.unfollow(followerId, followedId)).thenReturn(mockResponse);
-
- // 创建请求体
- Map<String, Long> request = Map.of("follower_id", followerId);
-
- // 调用控制器方法
- ResponseEntity<Map<String, Object>> responseEntity = userFollowController.unfollow(followedId, request);
-
- // 验证返回的状态码和内容
- assertEquals(200, responseEntity.getStatusCodeValue());
- assertEquals("success", responseEntity.getBody().get("status"));
- assertEquals("取消关注成功", responseEntity.getBody().get("message"));
- }
-
- // 测试获取某个用户的粉丝列表接口
- @Test
- void testGetFollowers() {
- Long userId = 1L;
- Map<String, Object> mockResponse = Map.of(
- "status", "success",
- "total", 2,
- "followers", List.of(
- Map.of("user_id", 2L, "username", "user2", "avatar_url", "url2"),
- Map.of("user_id", 3L, "username", "user3", "avatar_url", "url3")
- )
- );
-
- // 模拟 userFollowService.getFollowers() 返回
- when(userFollowService.getFollowers(userId)).thenReturn(mockResponse);
-
- // 调用控制器方法
- ResponseEntity<Map<String, Object>> responseEntity = userFollowController.getFollowers(userId);
-
- // 验证返回的状态码和内容
- assertEquals(200, responseEntity.getStatusCodeValue());
- assertEquals("success", responseEntity.getBody().get("status"));
- assertEquals(2, responseEntity.getBody().get("total"));
- assertEquals(2, ((List<?>) responseEntity.getBody().get("followers")).size());
- }
-
- // 测试获取某个用户的关注列表接口
- @Test
- void testGetFollowing() {
- Long userId = 1L;
- Map<String, Object> mockResponse = Map.of(
- "status", "success",
- "total", 2,
- "following", List.of(
- Map.of("user_id", 2L, "username", "user2", "avatar_url", "url2"),
- Map.of("user_id", 3L, "username", "user3", "avatar_url", "url3")
- )
- );
-
- // 模拟 userFollowService.getFollowing() 返回
- when(userFollowService.getFollowing(userId)).thenReturn(mockResponse);
-
- // 调用控制器方法
- ResponseEntity<Map<String, Object>> responseEntity = userFollowController.getFollowing(userId);
-
- // 验证返回的状态码和内容
- assertEquals(200, responseEntity.getStatusCodeValue());
- assertEquals("success", responseEntity.getBody().get("status"));
- assertEquals(2, responseEntity.getBody().get("total"));
- assertEquals(2, ((List<?>) responseEntity.getBody().get("following")).size());
- }
-}
diff --git a/src/test/java/com/example/myproject/controller/UserMessageControllerTest.java b/src/test/java/com/example/myproject/controller/UserMessageControllerTest.java
deleted file mode 100644
index 4fcbcc7..0000000
--- a/src/test/java/com/example/myproject/controller/UserMessageControllerTest.java
+++ /dev/null
@@ -1,95 +0,0 @@
-package com.example.myproject.controller;
-
-import com.example.myproject.service.UserMessageService;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.springframework.http.ResponseEntity;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.mockito.Mockito.when;
-
-class UserMessageControllerTest {
-
- @InjectMocks
- private UserMessageController userMessageController;
-
- @Mock
- private UserMessageService userMessageService;
-
- @BeforeEach
- void setup() {
- MockitoAnnotations.openMocks(this);
- }
-
- @Test
- void testSendMessage() {
- // 准备测试数据
- Map<String, Object> params = new HashMap<>();
- params.put("sender_id", 1L);
- params.put("receiver_id", 2L);
- params.put("content", "测试消息");
-
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", "私信发送成功");
- mockResponse.put("message_id", 100L);
-
- // 模拟Service方法调用
- when(userMessageService.sendMessage(1L, 2L, "测试消息")).thenReturn(mockResponse);
-
- // 执行测试
- ResponseEntity<Map<String, Object>> response = userMessageController.sendMessage(params);
-
- // 验证结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("success", response.getBody().get("status"));
- assertEquals("私信发送成功", response.getBody().get("message"));
- assertEquals(100L, response.getBody().get("message_id"));
- }
-
- @Test
- void testGetUserMessages() {
- // 准备测试数据
- Long userId = 1L;
-
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("messages", new HashMap<>());
-
- // 模拟Service方法调用
- when(userMessageService.getUserMessages(userId)).thenReturn(mockResponse);
-
- // 执行测试
- ResponseEntity<Map<String, Object>> response = userMessageController.getUserMessages(userId);
-
- // 验证结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("success", response.getBody().get("status"));
- }
-
- @Test
- void testGetMessage() {
- // 准备测试数据
- Long messageId = 1L;
-
- Map<String, Object> mockResponse = new HashMap<>();
- mockResponse.put("status", "success");
- mockResponse.put("message", new HashMap<>());
-
- // 模拟Service方法调用
- when(userMessageService.getMessage(messageId)).thenReturn(mockResponse);
-
- // 执行测试
- ResponseEntity<Map<String, Object>> response = userMessageController.getMessage(messageId);
-
- // 验证结果
- assertEquals(200, response.getStatusCodeValue());
- assertEquals("success", response.getBody().get("status"));
- }
-}
\ No newline at end of file