|
| 1 | +package com.involutionhell.backend.events.controller; |
| 2 | + |
| 3 | +import cn.dev33.satoken.annotation.SaCheckRole; |
| 4 | +import cn.dev33.satoken.stp.StpUtil; |
| 5 | +import com.involutionhell.backend.common.api.ApiResponse; |
| 6 | +import com.involutionhell.backend.events.dto.EventRequest; |
| 7 | +import com.involutionhell.backend.events.dto.EventView; |
| 8 | +import com.involutionhell.backend.events.model.Event; |
| 9 | +import com.involutionhell.backend.events.service.EventService; |
| 10 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 11 | +import org.springframework.web.bind.annotation.GetMapping; |
| 12 | +import org.springframework.web.bind.annotation.PathVariable; |
| 13 | +import org.springframework.web.bind.annotation.PostMapping; |
| 14 | +import org.springframework.web.bind.annotation.PutMapping; |
| 15 | +import org.springframework.web.bind.annotation.RequestBody; |
| 16 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 17 | +import org.springframework.web.bind.annotation.RestController; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +/** |
| 23 | + * 活动管理接口(需要 admin 角色)。 |
| 24 | + * |
| 25 | + * 路由(全部需要 admin): |
| 26 | + * - GET /api/admin/events 全量列表(含 draft / cancelled) |
| 27 | + * - GET /api/admin/events/{id} 单条详情(管理员可看 draft) |
| 28 | + * - POST /api/admin/events 创建 |
| 29 | + * - PUT /api/admin/events/{id} 更新 |
| 30 | + * - DELETE /api/admin/events/{id} 删除(级联删 event_interests) |
| 31 | + * |
| 32 | + * 使用 Sa-Token 的 @SaCheckRole("admin") 做整个类级别保护。拦截器链上会先做登录校验, |
| 33 | + * 再做角色校验,所以匿名访问返回 401,已登录非 admin 返回 403。 |
| 34 | + */ |
| 35 | +@RestController |
| 36 | +@RequestMapping("/api/admin/events") |
| 37 | +@SaCheckRole("admin") |
| 38 | +public class EventAdminController { |
| 39 | + |
| 40 | + private final EventService eventService; |
| 41 | + |
| 42 | + public EventAdminController(EventService eventService) { |
| 43 | + this.eventService = eventService; |
| 44 | + } |
| 45 | + |
| 46 | + @GetMapping |
| 47 | + public ApiResponse<List<EventView>> list() { |
| 48 | + List<Event> events = eventService.listAllForAdmin(); |
| 49 | + List<EventView> views = events.stream() |
| 50 | + .map(e -> EventView.from(e, eventService.countInterest(e.id()))) |
| 51 | + .toList(); |
| 52 | + return ApiResponse.ok(views); |
| 53 | + } |
| 54 | + |
| 55 | + @GetMapping("/{id}") |
| 56 | + public ApiResponse<EventView> detail(@PathVariable Long id) { |
| 57 | + Optional<Event> maybe = eventService.findById(id); |
| 58 | + if (maybe.isEmpty()) return new ApiResponse<>(false, "活动不存在", null); |
| 59 | + long interest = eventService.countInterest(id); |
| 60 | + return ApiResponse.ok(EventView.from(maybe.get(), interest)); |
| 61 | + } |
| 62 | + |
| 63 | + @PostMapping |
| 64 | + public ApiResponse<EventView> create(@RequestBody EventRequest req) { |
| 65 | + String validationError = validate(req); |
| 66 | + if (validationError != null) return new ApiResponse<>(false, validationError, null); |
| 67 | + |
| 68 | + long organizerId = StpUtil.getLoginIdAsLong(); |
| 69 | + Event draft = req.toEvent(null, organizerId, null, null); |
| 70 | + Event created = eventService.create(draft); |
| 71 | + return ApiResponse.ok("活动已创建", EventView.from(created, 0)); |
| 72 | + } |
| 73 | + |
| 74 | + @PutMapping("/{id}") |
| 75 | + public ApiResponse<EventView> update(@PathVariable Long id, @RequestBody EventRequest req) { |
| 76 | + String validationError = validate(req); |
| 77 | + if (validationError != null) return new ApiResponse<>(false, validationError, null); |
| 78 | + |
| 79 | + Optional<Event> existing = eventService.findById(id); |
| 80 | + if (existing.isEmpty()) return new ApiResponse<>(false, "活动不存在", null); |
| 81 | + |
| 82 | + // 保留原 organizerId(不允许更新时转让组织方;要转让走独立接口,避免误操作) |
| 83 | + Long originalOrganizer = existing.get().organizerId(); |
| 84 | + Event updated = req.toEvent(id, originalOrganizer, existing.get().createdAt(), null); |
| 85 | + Event saved = eventService.update(updated); |
| 86 | + long interest = eventService.countInterest(id); |
| 87 | + return ApiResponse.ok("活动已更新", EventView.from(saved, interest)); |
| 88 | + } |
| 89 | + |
| 90 | + @DeleteMapping("/{id}") |
| 91 | + public ApiResponse<Void> delete(@PathVariable Long id) { |
| 92 | + Optional<Event> existing = eventService.findById(id); |
| 93 | + if (existing.isEmpty()) return new ApiResponse<>(false, "活动不存在", null); |
| 94 | + eventService.delete(id); |
| 95 | + return ApiResponse.okMessage("活动已删除"); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * 基础字段校验。返回 null 表示校验通过,否则返回错误信息。 |
| 100 | + * 不用 @Valid + JSR-380 是因为项目当前没引入 spring-boot-starter-validation, |
| 101 | + * 避免为这一个模块引进新依赖。 |
| 102 | + */ |
| 103 | + private String validate(EventRequest req) { |
| 104 | + if (req == null) return "请求体不能为空"; |
| 105 | + if (req.title() == null || req.title().isBlank()) return "title 不能为空"; |
| 106 | + if (req.status() != null && !List.of("draft", "published", "archived", "cancelled").contains(req.status())) { |
| 107 | + return "status 必须是 draft / published / archived / cancelled 之一"; |
| 108 | + } |
| 109 | + if (req.startTime() != null && req.endTime() != null && req.endTime().isBefore(req.startTime())) { |
| 110 | + return "endTime 不能早于 startTime"; |
| 111 | + } |
| 112 | + return null; |
| 113 | + } |
| 114 | +} |
0 commit comments