公告修改
This commit is contained in:
@@ -6,11 +6,13 @@ import com.gameplatform.server.model.dto.admin.AnnouncementResponse;
|
||||
import com.gameplatform.server.model.dto.common.PageResult;
|
||||
import com.gameplatform.server.model.entity.admin.Announcement;
|
||||
import com.gameplatform.server.service.admin.AnnouncementService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@@ -24,8 +26,8 @@ public class AnnouncementController {
|
||||
private AnnouncementService announcementService;
|
||||
|
||||
@PostMapping
|
||||
@Operation(summary = "创建公告", description = "创建新的系统公告")
|
||||
public ResponseEntity<Object> createAnnouncement(@RequestBody AnnouncementRequest request) {
|
||||
@Operation(summary = "创建公告", description = "创建新的系统公告,belongId会自动从JWT token中获取")
|
||||
public ResponseEntity<Object> createAnnouncement(@RequestBody AnnouncementRequest request, Authentication authentication) {
|
||||
if (request.getTitle() == null || request.getTitle().trim().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(new Object() {
|
||||
public final boolean success = false;
|
||||
@@ -44,6 +46,33 @@ public class AnnouncementController {
|
||||
request.setEnabled(false); // 默认禁用
|
||||
}
|
||||
|
||||
// 从JWT token中获取当前用户ID
|
||||
if (authentication == null) {
|
||||
return ResponseEntity.badRequest().body(new Object() {
|
||||
public final boolean success = false;
|
||||
public final String message = "用户未认证";
|
||||
});
|
||||
}
|
||||
|
||||
Claims claims = (Claims) authentication.getDetails();
|
||||
if (claims == null) {
|
||||
return ResponseEntity.badRequest().body(new Object() {
|
||||
public final boolean success = false;
|
||||
public final String message = "无法获取用户信息";
|
||||
});
|
||||
}
|
||||
|
||||
Long userId = claims.get("userId", Long.class);
|
||||
if (userId == null) {
|
||||
return ResponseEntity.badRequest().body(new Object() {
|
||||
public final boolean success = false;
|
||||
public final String message = "无法获取用户ID";
|
||||
});
|
||||
}
|
||||
|
||||
// 设置belongId为当前用户ID
|
||||
request.setBelongId(userId.intValue());
|
||||
|
||||
Announcement announcement = AnnouncementConverter.toEntity(request);
|
||||
boolean success = announcementService.createAnnouncement(announcement);
|
||||
final boolean finalSuccess = success;
|
||||
@@ -60,13 +89,20 @@ public class AnnouncementController {
|
||||
public ResponseEntity<PageResult<AnnouncementResponse>> getAnnouncementList(
|
||||
@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page,
|
||||
@Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") int size,
|
||||
@Parameter(description = "按启用状态筛选,不传则获取全部") @RequestParam(required = false) Boolean enabled) {
|
||||
@Parameter(description = "按启用状态筛选,不传则获取全部") @RequestParam(required = false) Boolean enabled,
|
||||
@Parameter(description = "按归属ID筛选") @RequestParam(required = false) Integer belongId) {
|
||||
|
||||
int offset = (page - 1) * size;
|
||||
List<Announcement> announcements;
|
||||
long total;
|
||||
|
||||
if (enabled != null) {
|
||||
if (belongId != null && enabled != null) {
|
||||
announcements = announcementService.getAnnouncementsByBelongIdAndEnabled(belongId, enabled, size, offset);
|
||||
total = announcementService.getAnnouncementCountByBelongIdAndEnabled(belongId, enabled);
|
||||
} else if (belongId != null) {
|
||||
announcements = announcementService.getAnnouncementsByBelongId(belongId, size, offset);
|
||||
total = announcementService.getAnnouncementCountByBelongId(belongId);
|
||||
} else if (enabled != null) {
|
||||
announcements = announcementService.getAnnouncementsByEnabled(enabled, size, offset);
|
||||
total = announcementService.getAnnouncementCountByEnabled(enabled);
|
||||
} else {
|
||||
@@ -99,10 +135,11 @@ public class AnnouncementController {
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@Operation(summary = "更新公告", description = "更新指定ID的公告信息")
|
||||
@Operation(summary = "更新公告", description = "更新指定ID的公告信息,belongId会自动从JWT token中获取")
|
||||
public ResponseEntity<Object> updateAnnouncement(
|
||||
@Parameter(description = "公告ID", example = "1") @PathVariable Long id,
|
||||
@RequestBody AnnouncementRequest request) {
|
||||
@RequestBody AnnouncementRequest request,
|
||||
Authentication authentication) {
|
||||
|
||||
// 检查公告是否存在
|
||||
if (!announcementService.announcementExists(id)) {
|
||||
@@ -123,6 +160,17 @@ public class AnnouncementController {
|
||||
});
|
||||
}
|
||||
|
||||
// 从JWT token中获取当前用户ID
|
||||
if (authentication != null) {
|
||||
Claims claims = (Claims) authentication.getDetails();
|
||||
if (claims != null) {
|
||||
Long userId = claims.get("userId", Long.class);
|
||||
if (userId != null) {
|
||||
request.setBelongId(userId.intValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Announcement announcement = AnnouncementConverter.toEntity(request);
|
||||
announcement.setId(id);
|
||||
boolean success = announcementService.updateAnnouncement(announcement);
|
||||
@@ -178,8 +226,14 @@ public class AnnouncementController {
|
||||
|
||||
@GetMapping("/enabled")
|
||||
@Operation(summary = "获取启用的公告", description = "获取所有启用状态的公告,用于前端显示")
|
||||
public ResponseEntity<List<AnnouncementResponse>> getEnabledAnnouncements() {
|
||||
List<Announcement> announcements = announcementService.getEnabledAnnouncements();
|
||||
public ResponseEntity<List<AnnouncementResponse>> getEnabledAnnouncements(
|
||||
@Parameter(description = "按归属ID筛选") @RequestParam(required = false) Integer belongId) {
|
||||
List<Announcement> announcements;
|
||||
if (belongId != null) {
|
||||
announcements = announcementService.getEnabledAnnouncementsByBelongId(belongId);
|
||||
} else {
|
||||
announcements = announcementService.getEnabledAnnouncements();
|
||||
}
|
||||
List<AnnouncementResponse> responses = announcements.stream()
|
||||
.map(AnnouncementConverter::toResponse)
|
||||
.toList();
|
||||
|
||||
Reference in New Issue
Block a user