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