Skip to content

Commit b8b223d

Browse files
committed
refactor(system): 解耦后端多个控制器统一返回参数格式
解耦后端多个控制器统一返回参数格式
1 parent 6cad552 commit b8b223d

54 files changed

Lines changed: 2034 additions & 1518 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/src/main/java/xyz/lingview/dimstack/controller/AttachmentManagementController.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import org.springframework.web.bind.annotation.*;
66
import xyz.lingview.dimstack.annotation.RequiresPermission;
77
import xyz.lingview.dimstack.common.ApiResponse;
8-
import xyz.lingview.dimstack.mapper.UserInformationMapper;
98
import xyz.lingview.dimstack.service.AttachmentManagementService;
109
import xyz.lingview.dimstack.service.CurrentUserService;
10+
import xyz.lingview.dimstack.service.UserService;
1111

1212
import java.util.Map;
1313

@@ -25,7 +25,7 @@ public class AttachmentManagementController {
2525
private AttachmentManagementService attachmentManagementService;
2626

2727
@Autowired
28-
private UserInformationMapper userInformationMapper;
28+
private UserService userService;
2929

3030
@Autowired
3131
private CurrentUserService currentUserService;
@@ -114,7 +114,7 @@ public ApiResponse<Map<String, Object>> getAttachmentsByUuid(
114114
@RequestParam(defaultValue = "10") int size,
115115
HttpServletRequest request) {
116116
String username = currentUserService.getCurrentUsername();
117-
String uuid = userInformationMapper.selectUserUUID(username);
117+
String uuid = userService.getUserUUID(username);
118118

119119
Map<String, Object> result = attachmentManagementService.getPageByUuid(uuid, page, size);
120120
return ApiResponse.success(result);
@@ -130,7 +130,7 @@ public ApiResponse<Map<String, Object>> getDeletedAttachmentsByUuidOnly(
130130
@RequestParam(defaultValue = "10") int size,
131131
HttpServletRequest request) {
132132
String username = currentUserService.getCurrentUsername();
133-
String uuid = userInformationMapper.selectUserUUID(username);
133+
String uuid = userService.getUserUUID(username);
134134

135135
Map<String, Object> result = attachmentManagementService.getDeletedPageByUuidOnly(uuid, page, size);
136136
return ApiResponse.success(result);
@@ -146,7 +146,7 @@ public ApiResponse<Map<String, Object>> getAttachmentsByUuidWithDeleted(
146146
@RequestParam(defaultValue = "10") int size,
147147
HttpServletRequest request) {
148148
String username = currentUserService.getCurrentUsername();
149-
String uuid = userInformationMapper.selectUserUUID(username);
149+
String uuid = userService.getUserUUID(username);
150150

151151
Map<String, Object> result = attachmentManagementService.getPageByUuidWithRecentDeleted(uuid, page, size);
152152
return ApiResponse.success(result);
@@ -195,7 +195,7 @@ public ApiResponse<Void> deleteAttachment(
195195
HttpServletRequest request) {
196196

197197
String username = currentUserService.getCurrentUsername();
198-
String userUuid = userInformationMapper.selectUserUUID(username);
198+
String userUuid = userService.getUserUUID(username);
199199

200200
String attachmentOwnerUuid = attachmentManagementService.getAttachmentOwnerUuid(attachmentId);
201201
if (attachmentOwnerUuid == null) {
@@ -224,7 +224,7 @@ public ApiResponse<Void> restoreAttachment(
224224
HttpServletRequest request) {
225225

226226
String username = currentUserService.getCurrentUsername();
227-
String userUuid = userInformationMapper.selectUserUUID(username);
227+
String userUuid = userService.getUserUUID(username);
228228

229229
String attachmentOwnerUuid = attachmentManagementService.getAttachmentOwnerUuid(attachmentId);
230230
if (attachmentOwnerUuid == null) {

backend/src/main/java/xyz/lingview/dimstack/controller/CommentController.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ public class CommentController {
2424

2525
// 获取文章的评论列表
2626
@GetMapping("/article/{articleAlias}")
27-
public List<CommentDTO> getCommentsByArticle(@PathVariable String articleAlias) {
27+
public ApiResponse<List<CommentDTO>> getCommentsByArticle(@PathVariable String articleAlias) {
2828
String username = currentUserService.getCurrentUsername();
29-
return commentService.getCommentsByArticleAlias(articleAlias, username);
29+
List<CommentDTO> comments = commentService.getCommentsByArticleAlias(articleAlias, username);
30+
return ApiResponse.success(comments);
3031
}
3132

3233
// 添加评论
@@ -46,17 +47,19 @@ public ApiResponse<Void> addComment(@RequestBody AddCommentRequestDTO request) {
4647
@PostMapping("/{commentId}/like")
4748
@RequiresPermission({"comments:like", "comments:edit"})
4849
@RateLimit(window = 60, maxRequests = 5)
49-
public void likeComment(@PathVariable String commentId) {
50+
public ApiResponse<Void> likeComment(@PathVariable String commentId) {
5051
String username = currentUserService.getCurrentUsername();
5152
commentService.likeComment(username, commentId);
53+
return ApiResponse.success("点赞成功");
5254
}
5355

5456
// 删除评论
5557
@DeleteMapping("/{commentId}")
5658
@RateLimit(window = 60, maxRequests = 5)
5759
@RequiresPermission({"comments:delete", "comments:edit"})
58-
public void deleteComment(@PathVariable String commentId) {
60+
public ApiResponse<Void> deleteComment(@PathVariable String commentId) {
5961
String username = currentUserService.getCurrentUsername();
6062
commentService.deleteComment(username, commentId);
63+
return ApiResponse.success("删除成功");
6164
}
62-
}
65+
}

backend/src/main/java/xyz/lingview/dimstack/controller/CustomPageController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import xyz.lingview.dimstack.common.ApiResponse;
88
import xyz.lingview.dimstack.dto.request.CustomPageRequest;
99
import xyz.lingview.dimstack.dto.response.CustomPageResponse;
10-
import xyz.lingview.dimstack.mapper.UserInformationMapper;
1110
import xyz.lingview.dimstack.service.CurrentUserService;
1211
import xyz.lingview.dimstack.service.CustomPageService;
12+
import xyz.lingview.dimstack.service.UserService;
1313

1414
import java.util.List;
1515

@@ -27,7 +27,7 @@ public class CustomPageController {
2727
private CustomPageService customPageService;
2828

2929
@Autowired
30-
private UserInformationMapper userInformationMapper;
30+
private UserService userService;
3131

3232
@Autowired
3333
private CurrentUserService currentUserService;
@@ -126,7 +126,7 @@ public ApiResponse<List<CustomPageResponse>> getAllCustomPages(HttpServletReques
126126
private String getCurrentUserUuid(HttpServletRequest request) {
127127
String username = currentUserService.getCurrentUsername();
128128
if (username != null) {
129-
return userInformationMapper.selectUserUUID(username);
129+
return userService.getUserUUID(username);
130130
}
131131
return null;
132132
}

backend/src/main/java/xyz/lingview/dimstack/controller/FaviconController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import org.springframework.web.bind.annotation.GetMapping;
99
import org.springframework.web.bind.annotation.RestController;
1010
import org.springframework.web.client.RestTemplate;
11+
import xyz.lingview.dimstack.service.SiteConfigService;
1112

1213
import java.io.ByteArrayOutputStream;
1314
import java.io.IOException;
1415
import java.net.URI;
1516

1617
import net.coobird.thumbnailator.Thumbnails;
17-
import xyz.lingview.dimstack.mapper.SiteConfigMapper;
1818

1919
@Slf4j
2020
@RestController
@@ -23,11 +23,11 @@ public class FaviconController {
2323
private final RestTemplate restTemplate = new RestTemplate();
2424

2525
@Autowired
26-
private SiteConfigMapper siteConfigMapper;
26+
private SiteConfigService siteConfigService;
2727

2828
@GetMapping(value = "/favicon.ico", produces = "image/png")
2929
public ResponseEntity<ByteArrayResource> getFavicon(HttpServletRequest request) throws IOException {
30-
String iconUrl = siteConfigMapper.getSiteIcon();
30+
String iconUrl = siteConfigService.getSiteIcon();
3131
if (iconUrl == null || iconUrl.trim().isEmpty()) {
3232
return ResponseEntity.notFound().build();
3333
}

backend/src/main/java/xyz/lingview/dimstack/controller/FileAccessController.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import org.springframework.web.bind.annotation.RequestParam;
1515
import org.springframework.web.bind.annotation.RestController;
1616
import xyz.lingview.dimstack.domain.UploadAttachment;
17-
import xyz.lingview.dimstack.mapper.UploadMapper;
1817
import xyz.lingview.dimstack.service.ImageCompressionService;
1918
import xyz.lingview.dimstack.service.SiteConfigService;
19+
import xyz.lingview.dimstack.service.UploadService;
2020

2121
import java.io.UnsupportedEncodingException;
2222
import java.net.URLEncoder;
@@ -35,7 +35,7 @@
3535
public class FileAccessController {
3636

3737
@Autowired
38-
private UploadMapper uploadMapper;
38+
private UploadService uploadService;
3939

4040
@Autowired
4141
private ImageCompressionService imageCompressionService;
@@ -62,7 +62,7 @@ public ResponseEntity<Resource> getFile(
6262
try {
6363
log.debug("尝试访问文件,访问键: {}, 下载模式: {}", accessKey, download);
6464

65-
UploadAttachment attachment = uploadMapper.selectByAccessKey(accessKey);
65+
UploadAttachment attachment = uploadService.selectByAccessKey(accessKey);
6666
if (attachment == null) {
6767
log.warn("未找到访问键对应的文件: {}", accessKey);
6868
return ResponseEntity.notFound().build();
@@ -83,11 +83,11 @@ public ResponseEntity<Resource> getFile(
8383

8484
boolean isImage = contentType.startsWith("image/");
8585
Resource resource;
86-
86+
8787
if (isImage && !download) {
8888
Integer enableCompression = siteConfigService.getEnableImageCompression();
8989
boolean shouldCompress = enableCompression != null && enableCompression == 1;
90-
90+
9191
if (shouldCompress) {
9292
Path compressedPath = getCompressedImagePath(filePath);
9393
if (Files.exists(compressedPath)) {
@@ -141,14 +141,14 @@ private Path getCompressedImagePath(Path originalPath) {
141141
Path parentDir = originalPath.getParent();
142142
String fileName = originalPath.getFileName().toString();
143143
int dotIndex = fileName.lastIndexOf('.');
144-
144+
145145
String baseName;
146146
if (dotIndex > 0) {
147147
baseName = fileName.substring(0, dotIndex);
148148
} else {
149149
baseName = fileName;
150150
}
151-
151+
152152
String compressedFileName = baseName + "_compressed.jpg";
153153
return parentDir.resolve("compressor").resolve(compressedFileName);
154154
}

backend/src/main/java/xyz/lingview/dimstack/controller/HomeController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.extern.slf4j.Slf4j;
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.web.bind.annotation.*;
6+
import xyz.lingview.dimstack.common.ApiResponse;
67
import xyz.lingview.dimstack.service.ArticleService;
78
import xyz.lingview.dimstack.dto.request.PageRequest;
89
import xyz.lingview.dimstack.dto.request.PageResult;
@@ -17,17 +18,17 @@ public class HomeController {
1718
private ArticleService articleService;
1819

1920
@GetMapping("/articles")
20-
public PageResult<ArticleDTO> getHomeArticles(PageRequest pageRequest) {
21+
public ApiResponse<PageResult<ArticleDTO>> getHomeArticles(PageRequest pageRequest) {
2122
log.info("接收到获取文章列表请求: page={}, size={}, category={}",
2223
pageRequest.getPage(), pageRequest.getSize(), pageRequest.getCategory());
2324

2425
try {
2526
PageResult<ArticleDTO> result = articleService.getArticlesForHomePage(pageRequest);
2627
log.info("成功返回文章列表,共{}条记录", result.getTotal());
27-
return result;
28+
return ApiResponse.success(result);
2829
} catch (Exception e) {
2930
log.error("获取文章列表时发生错误", e);
3031
throw e;
3132
}
3233
}
33-
}
34+
}

backend/src/main/java/xyz/lingview/dimstack/controller/HotArticleController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package xyz.lingview.dimstack.controller;
22

3+
import xyz.lingview.dimstack.common.ApiResponse;
34
import xyz.lingview.dimstack.dto.request.HotArticleDTO;
45
import xyz.lingview.dimstack.service.HotArticleService;
56
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,7 +17,8 @@ public class HotArticleController {
1617
private HotArticleService hotArticleService;
1718

1819
@GetMapping("/articles")
19-
public List<HotArticleDTO> getHotArticles() {
20-
return hotArticleService.getHotArticles();
20+
public ApiResponse<List<HotArticleDTO>> getHotArticles() {
21+
List<HotArticleDTO> articles = hotArticleService.getHotArticles();
22+
return ApiResponse.success(articles);
2123
}
22-
}
24+
}

0 commit comments

Comments
 (0)