添加按类别创建促销
Change-Id: If9a0d78bfd14030782c24a0fa07b66cddea69d1e
diff --git a/src/main/java/com/example/myproject/controller/PromotionController.java b/src/main/java/com/example/myproject/controller/PromotionController.java
index ed90bf7..145692c 100644
--- a/src/main/java/com/example/myproject/controller/PromotionController.java
+++ b/src/main/java/com/example/myproject/controller/PromotionController.java
@@ -34,45 +34,7 @@
@Autowired
private PromotionService promotionService;
- @Autowired
- private UserService userService;
- @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 = "创建促销活动")
@@ -90,6 +52,39 @@
return Result.error("创建促销失败: " + e.getMessage());
}
}
+ @SaCheckLogin
+ @Operation(summary = "根据分类创建促销活动")
+ @PostMapping("/promotions/category")
+ public Result createPromotionByCategory(@RequestParam String category, @RequestBody @Validated PromotionCreateDTO promotionDTO) {
+ try {
+ // 校验权限
+// Long userId = StpUtil.getLoginIdAsLong();
+// var userOpt = userRepository.findById(userId);
+// if (userOpt.isEmpty() || !"admin".equals(userOpt.get().getRole())) {
+// return Result.error("没有权限创建促销活动");
+// }
+
+ // 查询该分类下的所有种子
+ List<TorrentEntity> torrents = torrentService.getTorrentsByCategory(category);
+ if (torrents.isEmpty()) {
+ return Result.error("分类【" + category + "】下没有可用的资源");
+ }
+
+ // 提取所有种子的 ID
+ List<Long> ids = torrents.stream().map(TorrentEntity::getId).toList();
+
+ // 设置到 DTO 中
+ promotionDTO.setApplicableTorrentIds(ids);
+
+ // 创建促销
+ Promotion promotion = promotionService.createPromotion(promotionDTO);
+
+ return Result.ok("成功创建促销,适用于分类【" + category + "】的 " + ids.size() + " 个资源");
+ } catch (Exception e) {
+ return Result.error("分类创建促销失败: " + e.getMessage());
+ }
+ }
+
@SaCheckLogin
@Operation(summary = "获取促销活动列表")