YelinCui | 09ee07c | 2025-06-07 05:09:55 +0800 | [diff] [blame] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import cn.dev33.satoken.annotation.SaCheckLogin; |
| 4 | import cn.dev33.satoken.stp.StpUtil; |
| 5 | import com.example.myproject.common.base.Result; |
| 6 | import com.example.myproject.dto.PromotionCreateDTO; |
| 7 | import com.example.myproject.dto.TorrentUpdateDTO; |
YelinCui | d6e8d43 | 2025-06-08 22:10:16 +0800 | [diff] [blame^] | 8 | import com.example.myproject.dto.vo.TorrentVO; |
YelinCui | 09ee07c | 2025-06-07 05:09:55 +0800 | [diff] [blame] | 9 | import com.example.myproject.entity.Promotion; |
YelinCui | d6e8d43 | 2025-06-08 22:10:16 +0800 | [diff] [blame^] | 10 | import com.example.myproject.entity.TorrentEntity; |
YelinCui | 09ee07c | 2025-06-07 05:09:55 +0800 | [diff] [blame] | 11 | import com.example.myproject.service.PromotionService; |
| 12 | import com.example.myproject.service.TorrentService; |
| 13 | import com.example.myproject.service.UserService; |
| 14 | import com.example.myproject.repository.UserRepository; |
| 15 | import io.swagger.v3.oas.annotations.Operation; |
| 16 | import lombok.RequiredArgsConstructor; |
| 17 | import lombok.extern.slf4j.Slf4j; |
| 18 | import org.springframework.beans.factory.annotation.Autowired; |
| 19 | import org.springframework.validation.annotation.Validated; |
| 20 | import org.springframework.web.bind.annotation.*; |
| 21 | |
| 22 | import java.util.List; |
| 23 | @RestController |
| 24 | @RequestMapping("/seeds") |
| 25 | @Slf4j |
| 26 | @RequiredArgsConstructor |
| 27 | public class PromotionController { |
| 28 | @Autowired |
| 29 | private TorrentService torrentService; |
| 30 | @Autowired |
| 31 | private UserRepository userRepository; |
| 32 | |
| 33 | |
| 34 | @Autowired |
| 35 | private PromotionService promotionService; |
| 36 | |
| 37 | @Autowired |
| 38 | private UserService userService; |
| 39 | @SaCheckLogin |
| 40 | @Operation(summary = "删除种子") |
| 41 | @DeleteMapping("/{torrentId}") |
| 42 | public Result deleteTorrent(@PathVariable Long torrentId) { |
| 43 | try { |
| 44 | // 验证用户权限 |
| 45 | Long userId = StpUtil.getLoginIdAsLong(); |
| 46 | if (!torrentService.canUserDeleteTorrent(torrentId, userId)) { |
| 47 | return Result.error("没有权限删除此种子"); |
| 48 | } |
| 49 | |
| 50 | torrentService.deleteTorrent(torrentId); |
| 51 | return Result.ok(); |
| 52 | } catch (Exception e) { |
| 53 | return Result.error("删除失败: " + e.getMessage()); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @SaCheckLogin |
| 58 | @Operation(summary = "修改种子信息") |
| 59 | @PutMapping("/{torrentId}") |
| 60 | public Result updateTorrent( |
| 61 | @PathVariable Long torrentId, |
| 62 | @RequestBody @Validated TorrentUpdateDTO updateDTO) { |
| 63 | try { |
| 64 | // 验证用户权限 |
| 65 | Long userId = StpUtil.getLoginIdAsLong(); |
| 66 | if (!torrentService.canUserUpdateTorrent(torrentId, userId)) { |
| 67 | return Result.error("没有权限修改此种子"); |
| 68 | } |
| 69 | |
| 70 | torrentService.updateTorrent(torrentId, updateDTO); |
| 71 | return Result.ok(); |
| 72 | } catch (Exception e) { |
| 73 | return Result.error("更新失败: " + e.getMessage()); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @SaCheckLogin |
| 78 | @Operation(summary = "创建促销活动") |
| 79 | @PostMapping("/promotions") |
| 80 | public Result createPromotion(@RequestBody @Validated PromotionCreateDTO promotionDTO) { |
| 81 | try { |
| 82 | // 验证用户权限(只有管理员可以创建促销) |
| 83 | // if (!StpUtil.hasRole("admin")) { |
| 84 | // return Result.error("没有权限创建促销活动"); |
| 85 | // } |
| 86 | // |
| 87 | Promotion promotion = promotionService.createPromotion(promotionDTO); |
| 88 | return Result.ok(promotion); |
| 89 | } catch (Exception e) { |
| 90 | return Result.error("创建促销失败: " + e.getMessage()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @SaCheckLogin |
| 95 | @Operation(summary = "获取促销活动列表") |
| 96 | @GetMapping("/promotions") |
| 97 | public Result getPromotions() { |
| 98 | try { |
| 99 | List<Promotion> promotions = promotionService.getAllActivePromotions(); |
| 100 | return Result.ok(promotions); |
| 101 | } catch (Exception e) { |
| 102 | return Result.error("获取促销列表失败: " + e.getMessage()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @SaCheckLogin |
| 107 | @Operation(summary = "获取促销详情") |
| 108 | @GetMapping("/promotions/{promotionId}") |
| 109 | public Result getPromotionDetails(@PathVariable Long promotionId) { |
| 110 | try { |
| 111 | Promotion promotion = promotionService.getPromotionById(promotionId); |
| 112 | if (promotion == null) { |
| 113 | return Result.error("促销活动不存在"); |
| 114 | } |
| 115 | return Result.ok(promotion); |
| 116 | } catch (Exception e) { |
| 117 | return Result.error("获取促销详情失败: " + e.getMessage()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @SaCheckLogin |
| 122 | @Operation(summary = "删除促销活动") |
| 123 | @DeleteMapping("/promotions/{promotionId}") |
| 124 | public Result deletePromotion(@PathVariable Long promotionId) { |
| 125 | try { |
| 126 | // 验证用户权限(只有管理员可以删除促销) |
| 127 | // if (!StpUtil.hasRole("admin")) { |
| 128 | // return Result.error("没有权限删除促销活动"); |
| 129 | // } |
| 130 | Long userId = StpUtil.getLoginIdAsLong(); |
| 131 | // 查询用户 |
| 132 | var userOpt = userRepository.findById(userId); |
| 133 | if (userOpt.isEmpty() || !"admin".equals(userOpt.get().getRole())) { |
| 134 | return Result.error("没有权限删除促销活动"); |
| 135 | } |
| 136 | |
| 137 | promotionService.deletePromotion(promotionId); |
| 138 | return Result.ok(); |
| 139 | } catch (Exception e) { |
| 140 | return Result.error("删除促销失败: " + e.getMessage()); |
| 141 | } |
| 142 | } |
YelinCui | d6e8d43 | 2025-06-08 22:10:16 +0800 | [diff] [blame^] | 143 | @SaCheckLogin |
| 144 | @Operation(summary = "查看冷门资源") |
| 145 | @GetMapping("/cold") |
| 146 | public Result getColdResources() { |
| 147 | try { |
| 148 | List<TorrentEntity> coldList = torrentService.getColdTorrents(10); // 例如 views < 10 |
| 149 | return Result.ok(coldList); |
| 150 | } catch (Exception e) { |
| 151 | return Result.error("获取冷门资源失败: " + e.getMessage()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
YelinCui | 09ee07c | 2025-06-07 05:09:55 +0800 | [diff] [blame] | 156 | } |