blob: a4193cbf627c86ff53bf8901a641a6684bf4873b [file] [log] [blame]
夜雨声烦451d71c2025-05-20 00:58:36 +08001package com.example.g8backend.controller;
2
夜雨声烦70f4c652025-05-20 01:52:41 +08003import com.example.g8backend.dto.ApiResponse;
夜雨声烦7affa472025-05-20 19:27:16 +08004import com.example.g8backend.entity.Post;
夜雨声烦70f4c652025-05-20 01:52:41 +08005import com.example.g8backend.entity.Report;
夜雨声烦451d71c2025-05-20 00:58:36 +08006import com.example.g8backend.service.AdminService;
夜雨声烦7affa472025-05-20 19:27:16 +08007import com.example.g8backend.service.IPostService;
夜雨声烦70f4c652025-05-20 01:52:41 +08008import com.example.g8backend.service.IReportService;
夜雨声烦451d71c2025-05-20 00:58:36 +08009import org.springframework.beans.factory.annotation.Autowired;
夜雨声烦7affa472025-05-20 19:27:16 +080010import org.springframework.http.ResponseEntity;
夜雨声烦451d71c2025-05-20 00:58:36 +080011import org.springframework.security.access.prepost.PreAuthorize;
夜雨声烦70f4c652025-05-20 01:52:41 +080012import org.springframework.security.core.context.SecurityContextHolder;
13import org.springframework.web.bind.annotation.*;
14
15import java.util.List;
夜雨声烦451d71c2025-05-20 00:58:36 +080016
17@RestController
18@RequestMapping("/admin")
19public class AdminController {
20 @Autowired
21 private AdminService adminService;
夜雨声烦70f4c652025-05-20 01:52:41 +080022 private IReportService reportService;
夜雨声烦7affa472025-05-20 19:27:16 +080023 @Autowired
24 private IPostService postService;
夜雨声烦451d71c2025-05-20 00:58:36 +080025 @PostMapping("/grant-vip/{userId}")
26 @PreAuthorize("hasRole('ADMIN')") // 仅允许管理员访问
27 public String grantVip(@PathVariable Long userId) {
28 boolean success = adminService.grantVip(userId);
29 return success ? "VIP授予成功" : "操作失败(用户不存在)";
30 }
夜雨声烦70f4c652025-05-20 01:52:41 +080031 // 获取举报记录(支持按状态过滤)
32 @GetMapping("/reports")
33 @PreAuthorize("hasRole('ADMIN')")
34 public ApiResponse<List<Report>> getReports(
35 @RequestParam(required = false) String status) {
夜雨声烦70f4c652025-05-20 01:52:41 +080036 Long adminId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
37 return ApiResponse.success(reportService.getReports(status, adminId));
38 }
39 // 处理举报
40 @PutMapping("/reports/{reportId}")
41 @PreAuthorize("hasRole('ADMIN')")
42 public ApiResponse<String> resolveReport(
43 @PathVariable Long reportId,
44 @RequestParam String status,
45 @RequestParam(required = false) String notes) {
夜雨声烦45c25dd2025-05-20 11:59:03 +080046 Long adminId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();//这里之前不太对,service改了
夜雨声烦363e5362025-05-20 02:01:13 +080047 reportService.resolveReport(reportId, adminId, status, notes);
夜雨声烦70f4c652025-05-20 01:52:41 +080048 return ApiResponse.success("举报处理完成");
49 }
50
夜雨声烦7affa472025-05-20 19:27:16 +080051
52 // 封禁用户
53 @PostMapping("/users/{userId}/ban")
54 @PreAuthorize("hasRole('ADMIN')")
55 public ApiResponse<String> banUser(
56 @PathVariable Long userId,
57 @RequestParam String reason) {
58 Long adminId = getCurrentAdminId();
59 boolean success = adminService.banUser(userId, reason, adminId);
60 return success ?
61 ApiResponse.success("用户封禁成功") :
62 ApiResponse.error(400, "操作失败");
63 }
64
65 // 解封用户
66 @PostMapping("/users/{userId}/unban")
67 @PreAuthorize("hasRole('ADMIN')")
68 public ApiResponse<String> unbanUser(@PathVariable Long userId) {
69 Long adminId = getCurrentAdminId();
70 boolean success = adminService.unbanUser(userId, adminId);
71 return success ?
72 ApiResponse.success("用户解封成功") :
73 ApiResponse.error(400, "操作失败");
74 }
75
76 // 锁定帖子
77 @PostMapping("/posts/{postId}/lock")
78 @PreAuthorize("hasRole('ADMIN')")
79 public ApiResponse<String> lockPost(
80 @PathVariable Long postId,
81 @RequestParam String reason) {
82 Long adminId = getCurrentAdminId();
83 boolean success = adminService.lockPost(postId, reason, adminId);
84 return success ?
85 ApiResponse.success("帖子已锁定") :
86 ApiResponse.error(400, "操作失败");
87 }
88
89 // 解锁帖子
90 @PostMapping("/posts/{postId}/unlock")
91 @PreAuthorize("hasRole('ADMIN')")
92 public ApiResponse<String> unlockPost(@PathVariable Long postId) {
93 Long adminId = getCurrentAdminId();
94 boolean success = adminService.unlockPost(postId, adminId);
95 return success ?
96 ApiResponse.success("帖子已解锁") :
97 ApiResponse.error(400, "操作失败");
98 }
99 @DeleteMapping("/{postId}")
100 public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) {
101 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
102 Post post = postService.getById(postId);
103 if (post == null) {
104 return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
105 }
106 postService.removeById(postId);
107 return ResponseEntity.ok(ApiResponse.message("Post deleted successfully."));
108 }
109
110 private Long getCurrentAdminId() {
111 return (Long) SecurityContextHolder.getContext()
112 .getAuthentication().getPrincipal();
113 }
114
夜雨声烦451d71c2025-05-20 00:58:36 +0800115}