feat: 增强用户端配置管理功能
主要修改: 1. 在SystemConfigController中完善用户端配置获取和批量更新接口的实现。 2. 优化SystemConfigService,增强配置值验证逻辑,确保配置的准确性和有效性。 技术细节: - 新增的功能提升了用户端配置的管理灵活性,支持更高效的批量操作。
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
package com.gameplatform.server.controller.admin;
|
||||
|
||||
import com.gameplatform.server.model.dto.admin.AnnouncementConverter;
|
||||
import com.gameplatform.server.model.dto.admin.AnnouncementRequest;
|
||||
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.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.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/announcement")
|
||||
@Tag(name = "公告管理", description = "系统公告的增删改查接口")
|
||||
public class AnnouncementController {
|
||||
|
||||
@Autowired
|
||||
private AnnouncementService announcementService;
|
||||
|
||||
@PostMapping
|
||||
@Operation(summary = "创建公告", description = "创建新的系统公告")
|
||||
public ResponseEntity<Object> createAnnouncement(@RequestBody AnnouncementRequest request) {
|
||||
if (request.getTitle() == null || request.getTitle().trim().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(new Object() {
|
||||
public final boolean success = false;
|
||||
public final String message = "公告标题不能为空";
|
||||
});
|
||||
}
|
||||
|
||||
if (request.getContent() == null || request.getContent().trim().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(new Object() {
|
||||
public final boolean success = false;
|
||||
public final String message = "公告内容不能为空";
|
||||
});
|
||||
}
|
||||
|
||||
if (request.getEnabled() == null) {
|
||||
request.setEnabled(false); // 默认禁用
|
||||
}
|
||||
|
||||
Announcement announcement = AnnouncementConverter.toEntity(request);
|
||||
boolean success = announcementService.createAnnouncement(announcement);
|
||||
final boolean finalSuccess = success;
|
||||
|
||||
return ResponseEntity.ok(new Object() {
|
||||
public final boolean success = finalSuccess;
|
||||
public final String message = finalSuccess ? "公告创建成功" : "公告创建失败";
|
||||
public final Long id = finalSuccess ? announcement.getId() : null;
|
||||
});
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "获取公告列表", description = "分页获取所有系统公告")
|
||||
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) {
|
||||
|
||||
int offset = (page - 1) * size;
|
||||
List<Announcement> announcements;
|
||||
long total;
|
||||
|
||||
if (enabled != null) {
|
||||
announcements = announcementService.getAnnouncementsByEnabled(enabled, size, offset);
|
||||
total = announcementService.getAnnouncementCountByEnabled(enabled);
|
||||
} else {
|
||||
announcements = announcementService.getAllAnnouncements(size, offset);
|
||||
total = announcementService.getAnnouncementCount();
|
||||
}
|
||||
|
||||
List<AnnouncementResponse> responses = announcements.stream()
|
||||
.map(AnnouncementConverter::toResponse)
|
||||
.toList();
|
||||
|
||||
PageResult<AnnouncementResponse> result = new PageResult<>();
|
||||
result.setItems(responses);
|
||||
result.setTotal(total);
|
||||
result.setPage(page);
|
||||
result.setSize(size);
|
||||
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@Operation(summary = "获取公告详情", description = "根据ID获取公告详细信息")
|
||||
public ResponseEntity<AnnouncementResponse> getAnnouncementById(
|
||||
@Parameter(description = "公告ID", example = "1") @PathVariable Long id) {
|
||||
Announcement announcement = announcementService.getAnnouncementById(id);
|
||||
if (announcement == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
return ResponseEntity.ok(AnnouncementConverter.toResponse(announcement));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@Operation(summary = "更新公告", description = "更新指定ID的公告信息")
|
||||
public ResponseEntity<Object> updateAnnouncement(
|
||||
@Parameter(description = "公告ID", example = "1") @PathVariable Long id,
|
||||
@RequestBody AnnouncementRequest request) {
|
||||
|
||||
// 检查公告是否存在
|
||||
if (!announcementService.announcementExists(id)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
if (request.getTitle() != null && request.getTitle().trim().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(new Object() {
|
||||
public final boolean success = false;
|
||||
public final String message = "公告标题不能为空";
|
||||
});
|
||||
}
|
||||
|
||||
if (request.getContent() != null && request.getContent().trim().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(new Object() {
|
||||
public final boolean success = false;
|
||||
public final String message = "公告内容不能为空";
|
||||
});
|
||||
}
|
||||
|
||||
Announcement announcement = AnnouncementConverter.toEntity(request);
|
||||
announcement.setId(id);
|
||||
boolean success = announcementService.updateAnnouncement(announcement);
|
||||
final boolean finalSuccess = success;
|
||||
|
||||
return ResponseEntity.ok(new Object() {
|
||||
public final boolean success = finalSuccess;
|
||||
public final String message = finalSuccess ? "公告更新成功" : "公告更新失败";
|
||||
});
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@Operation(summary = "删除公告", description = "删除指定ID的公告")
|
||||
public ResponseEntity<Object> deleteAnnouncement(
|
||||
@Parameter(description = "公告ID", example = "1") @PathVariable Long id) {
|
||||
|
||||
// 检查公告是否存在
|
||||
if (!announcementService.announcementExists(id)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
boolean success = announcementService.deleteAnnouncement(id);
|
||||
final boolean finalSuccess = success;
|
||||
|
||||
return ResponseEntity.ok(new Object() {
|
||||
public final boolean success = finalSuccess;
|
||||
public final String message = finalSuccess ? "公告删除成功" : "公告删除失败";
|
||||
});
|
||||
}
|
||||
|
||||
@PutMapping("/{id}/enabled")
|
||||
@Operation(summary = "更新公告启用状态", description = "启用或禁用指定公告")
|
||||
public ResponseEntity<Object> updateAnnouncementEnabled(
|
||||
@Parameter(description = "公告ID", example = "1") @PathVariable Long id,
|
||||
@Parameter(description = "启用状态", example = "true") @RequestParam Boolean enabled) {
|
||||
|
||||
// 检查公告是否存在
|
||||
if (!announcementService.announcementExists(id)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
boolean success = announcementService.updateAnnouncementEnabled(id, enabled);
|
||||
final boolean finalSuccess = success;
|
||||
final Boolean finalEnabled = enabled;
|
||||
|
||||
return ResponseEntity.ok(new Object() {
|
||||
public final boolean success = finalSuccess;
|
||||
public final String message = finalSuccess ?
|
||||
(finalEnabled ? "公告已启用" : "公告已禁用") :
|
||||
"状态更新失败";
|
||||
});
|
||||
}
|
||||
|
||||
@GetMapping("/enabled")
|
||||
@Operation(summary = "获取启用的公告", description = "获取所有启用状态的公告,用于前端显示")
|
||||
public ResponseEntity<List<AnnouncementResponse>> getEnabledAnnouncements() {
|
||||
List<Announcement> announcements = announcementService.getEnabledAnnouncements();
|
||||
List<AnnouncementResponse> responses = announcements.stream()
|
||||
.map(AnnouncementConverter::toResponse)
|
||||
.toList();
|
||||
return ResponseEntity.ok(responses);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user