blob: ed90bf78ce865a39564085931c5fab5adb0d65b4 [file] [log] [blame]
package com.example.myproject.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.example.myproject.common.base.Result;
import com.example.myproject.dto.PromotionCreateDTO;
import com.example.myproject.dto.TorrentUpdateDTO;
import com.example.myproject.dto.vo.TorrentVO;
import com.example.myproject.entity.Promotion;
import com.example.myproject.entity.TorrentEntity;
import com.example.myproject.service.PromotionService;
import com.example.myproject.service.TorrentService;
import com.example.myproject.service.UserService;
import com.example.myproject.repository.UserRepository;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/seeds")
@Slf4j
@RequiredArgsConstructor
public class PromotionController {
@Autowired
private TorrentService torrentService;
@Autowired
private UserRepository userRepository;
@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 = "创建促销活动")
@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("没有权限删除促销活动");
// }
Long userId = StpUtil.getLoginIdAsLong();
// 查询用户
var userOpt = userRepository.findById(userId);
if (userOpt.isEmpty() || !"admin".equals(userOpt.get().getRole())) {
return Result.error("没有权限删除促销活动");
}
promotionService.deletePromotion(promotionId);
return Result.ok();
} catch (Exception e) {
return Result.error("删除促销失败: " + e.getMessage());
}
}
@SaCheckLogin
@Operation(summary = "查看冷门资源")
@GetMapping("/cold")
public Result getColdResources() {
try {
List<TorrentEntity> coldList = torrentService.getColdTorrents(10); // 例如 views < 10
return Result.ok(coldList);
} catch (Exception e) {
return Result.error("获取冷门资源失败: " + e.getMessage());
}
}
}