init: 优化代码

This commit is contained in:
bao
2025-12-24 21:34:13 +08:00
parent 6a51eb48e4
commit 7331553528
3 changed files with 15 additions and 14 deletions

View File

@@ -8,7 +8,8 @@ public enum ResultCode {
UNAUTHORIZED(401, "未登录或 Token 失效"), UNAUTHORIZED(401, "未登录或 Token 失效"),
FORBIDDEN(403, "无权限"), FORBIDDEN(403, "无权限"),
NOT_FOUND(404, "数据不存在"), NOT_FOUND(404, "数据不存在"),
SYSTEM_ERROR(500, "系统异常"); SYSTEM_ERROR(500, "系统异常"),
FAIL(500, "操作失败");
private final int code; private final int code;
private final String msg; private final String msg;

View File

@@ -1,17 +1,17 @@
package com.bao.dating.controller; package com.bao.dating.controller;
import com.bao.dating.common.Comments; import com.bao.dating.common.Comments;
import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.service.CommentsService; import com.bao.dating.service.CommentsService;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/comments") @RequestMapping("/comments")
@CrossOrigin // 解决跨域问题(前后端分离必加) @CrossOrigin
public class CommentController { public class CommentController {
@Resource @Resource
private CommentsService commentsService; private CommentsService commentsService;
@@ -21,9 +21,11 @@ public class CommentController {
* @param comment 评论对象含content、userId、dynamicId * @param comment 评论对象含content、userId、dynamicId
*/ */
@PostMapping("/add") @PostMapping("/add")
public Map<String, Object> addComment(@RequestBody Comments comment) { public Result<String> addComment(@RequestBody Comments comment) {
boolean success = commentsService.addComment(comment); boolean success = commentsService.addComment(comment);
return success ? Map.of("code", 200, "msg", "评论成功") : Map.of("code", 500, "msg", "评论失败"); return success ?
Result.success(ResultCode.SUCCESS, "评论成功") :
Result.error(ResultCode.FAIL, "评论失败");
} }
/** /**
@@ -31,9 +33,11 @@ public class CommentController {
* @param user_id 评论ID * @param user_id 评论ID
*/ */
@DeleteMapping("/delete/{user_id}") @DeleteMapping("/delete/{user_id}")
public Map<String, Object> deleteComment(@PathVariable Long user_id) { public Result<String> deleteComment(@PathVariable Long user_id) {
boolean success = commentsService.deleteComment(user_id); boolean success = commentsService.deleteComment(user_id);
return success ? Map.of("code", 200, "msg", "删除成功") : Map.of("code", 500, "msg", "删除失败"); return success ?
Result.success(ResultCode.SUCCESS, "删除成功") :
Result.error(ResultCode.FAIL, "删除失败");
} }
/** /**
@@ -41,8 +45,8 @@ public class CommentController {
* @param post_id 动态ID * @param post_id 动态ID
*/ */
@GetMapping("/list/{post_id}") @GetMapping("/list/{post_id}")
public Map<String, Object> getCommentList(@PathVariable Long post_id) { public Result<List<Comments>> getCommentList(@PathVariable Long post_id) {
List<Comments> commentList = commentsService.getCommentByPostId(post_id); List<Comments> commentList = commentsService.getCommentByPostId(post_id);
return Map.of("code", 200, "data", commentList); return Result.success(ResultCode.SUCCESS, commentList);
} }
} }

View File

@@ -1,4 +0,0 @@
package com.bao.dating.controller;
public class text {
}